GeocoderBehaviorTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. App::uses('GeocoderBehavior', 'Tools.Model/Behavior');
  3. App::uses('Set', 'Utility');
  4. App::uses('AppModel', 'Model');
  5. class GeocoderBehaviorTest extends CakeTestCase {
  6. public $fixtures = array(
  7. 'core.comment', 'plugin.tools.address'
  8. );
  9. public function startTest() {
  10. $this->Comment = ClassRegistry::init('Comment');
  11. $this->Comment->Behaviors->attach('Tools.Geocoder', array('real'=>false));
  12. }
  13. public function testBasic() {
  14. echo '<h3>'.__FUNCTION__.'</h3>';
  15. $data = array(
  16. 'street' => 'Krebenweg 22',
  17. 'zip' => '74523',
  18. 'city' => 'Bibersfeld'
  19. );
  20. $res = $this->Comment->save($data);
  21. debug($res);
  22. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']) && round($res['Comment']['lat']) === 49.0 && round($res['Comment']['lng']) === 10.0);
  23. // accuracy = 4
  24. # inconclusive
  25. $data = array(
  26. //'street' => 'Leopoldstraße',
  27. 'city' => 'München'
  28. );
  29. $res = $this->Comment->save($data);
  30. $this->assertEquals('', $this->Comment->Behaviors->Geocoder->Geocode->error());
  31. debug($res);
  32. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
  33. $this->assertEquals('München, Deutschland', $res['Comment']['geocoder_result']['formatted_address']);
  34. $data = array(
  35. 'city' => 'Bibersfeld'
  36. );
  37. $res = $this->Comment->save($data);
  38. debug($res);
  39. $this->assertTrue(!empty($res));
  40. $this->assertEquals('', $this->Comment->Behaviors->Geocoder->Geocode->error());
  41. }
  42. public function testMinAccLow() {
  43. echo '<h3>'.__FUNCTION__.'</h3>';
  44. $this->Comment->Behaviors->detach('Geocoder');
  45. $this->Comment->Behaviors->attach('Tools.Geocoder', array('real'=>false, 'min_accuracy'=>0));
  46. // accuracy = 1
  47. $data = array(
  48. //'street' => 'Leopoldstraße',
  49. 'city' => 'Deutschland'
  50. );
  51. $res = $this->Comment->save($data);
  52. debug($this->Comment->Behaviors->Geocoder->Geocode->error()).BR;
  53. debug($res);
  54. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  55. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
  56. }
  57. public function testMinAccHigh() {
  58. echo '<h3>'.__FUNCTION__.'</h3>';
  59. $this->Comment->Behaviors->detach('Geocoder');
  60. $this->Comment->Behaviors->attach('Tools.Geocoder', array('real'=>false, 'min_accuracy'=>4));
  61. // accuracy = 1
  62. $data = array(
  63. //'street' => 'Leopoldstraße',
  64. 'city' => 'Deutschland'
  65. );
  66. $res = $this->Comment->save($data);
  67. debug($this->Comment->Behaviors->Geocoder->Geocode->error()).BR;
  68. debug($res);
  69. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  70. $this->assertTrue(!isset($res['Comment']['lat']) && !isset($res['Comment']['lng']));
  71. }
  72. public function testMinInc() {
  73. echo '<h3>'.__FUNCTION__.'</h3>';
  74. $this->Comment->Behaviors->detach('Geocoder');
  75. $this->Comment->Behaviors->attach('Tools.Geocoder', array('real'=>false, 'min_accuracy'=>GeocodeLib::ACC_SUBLOC));
  76. $this->assertEquals(GeocodeLib::ACC_SUBLOC, $this->Comment->Behaviors->Geocoder->settings['Comment']['min_accuracy']);
  77. // accuracy = 1
  78. $data = array(
  79. //'street' => 'Leopoldstraße',
  80. 'city' => 'Neustadt'
  81. );
  82. $res = $this->Comment->save($data);
  83. debug($this->Comment->Behaviors->Geocoder->Geocode->error()).BR;
  84. debug($this->Comment->Behaviors->Geocoder->Geocode->getResult()).BR;
  85. debug($res);
  86. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  87. $this->assertTrue(!isset($res['Comment']['lat']) && !isset($res['Comment']['lng']));
  88. }
  89. public function testMinIncAllowed() {
  90. echo '<h3>'.__FUNCTION__.'</h3>';
  91. $this->Comment->Behaviors->detach('Geocoder');
  92. $this->Comment->Behaviors->attach('Tools.Geocoder', array('real'=>false, 'allow_inconclusive'=>true));
  93. // accuracy = 1
  94. $data = array(
  95. //'street' => 'Leopoldstraße',
  96. 'city' => 'Neustadt'
  97. );
  98. $res = $this->Comment->save($data);
  99. debug($this->Comment->Behaviors->Geocoder->Geocode->error()).BR;
  100. debug($this->Comment->Behaviors->Geocoder->Geocode->url()).BR;
  101. debug($this->Comment->Behaviors->Geocoder->Geocode->getResult()).BR;
  102. debug($res);
  103. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  104. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
  105. }
  106. public function testExpect() {
  107. $this->Comment->Behaviors->detach('Geocoder');
  108. $this->Comment->Behaviors->attach('Tools.Geocoder', array('real'=>false, 'expect'=>array('postal_code')));
  109. // accuracy = 1
  110. $data = array(
  111. //'street' => 'Leopoldstraße',
  112. 'city' => 'Bibersfeld'
  113. );
  114. $res = $this->Comment->save($data);
  115. debug($this->Comment->Behaviors->Geocoder->Geocode->error()).BR;
  116. debug($res);
  117. debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  118. $this->assertTrue(empty($res['Comment']['lat']) && empty($res['Comment']['lng']));
  119. $data = array(
  120. //'street' => 'Leopoldstraße',
  121. 'city' => '74523'
  122. );
  123. $res = $this->Comment->save($data);
  124. debug($this->Comment->Behaviors->Geocoder->Geocode->error()).BR;
  125. debug($res);
  126. //debug($this->Comment->Behaviors->Geocoder->Geocode->debug());
  127. $this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
  128. }
  129. public function testDistance() {
  130. $res = $this->Comment->distance(12, 14);
  131. $expected = '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - Comment.lat)) * COS( PI()/2 - RADIANS(90 - 12)) * COS( RADIANS(Comment.lat) - RADIANS(14)) + SIN( PI()/2 - RADIANS(90 - Comment.lng)) * SIN( PI()/2 - RADIANS(90 - 12)))';
  132. $this->assertEquals($expected, $res);
  133. $this->Comment->Behaviors->detach('Geocoder');
  134. $this->Comment->Behaviors->attach('Tools.Geocoder', array('lat'=>'x', 'lng'=>'y'));
  135. $res = $this->Comment->distance(12, 14);
  136. $expected = '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - Comment.x)) * COS( PI()/2 - RADIANS(90 - 12)) * COS( RADIANS(Comment.x) - RADIANS(14)) + SIN( PI()/2 - RADIANS(90 - Comment.y)) * SIN( PI()/2 - RADIANS(90 - 12)))';
  137. $this->assertEquals($expected, $res);
  138. }
  139. public function testDistanceField() {
  140. $res = $this->Comment->distanceField(12, 14);
  141. $expected = '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - Comment.lat)) * COS( PI()/2 - RADIANS(90 - 12)) * COS( RADIANS(Comment.lat) - RADIANS(14)) + SIN( PI()/2 - RADIANS(90 - Comment.lng)) * SIN( PI()/2 - RADIANS(90 - 12))) AS Comment.distance';
  142. $this->assertEquals($expected, $res);
  143. }
  144. public function testSetDistanceAsVirtualField() {
  145. $this->Address = ClassRegistry::init('Address');
  146. $this->Address->Behaviors->attach('Tools.Geocoder');
  147. $this->Address->setDistanceAsVirtualField(13.3, 19.2);
  148. $options = array('order' => array('Address.distance' => 'ASC'));
  149. $res = $this->Address->find('all', $options);
  150. $this->assertTrue($res[0]['Address']['distance'] < $res[1]['Address']['distance']);
  151. $this->assertTrue($res[1]['Address']['distance'] < $res[2]['Address']['distance']);
  152. }
  153. }