GeocoderBehaviorTest.php 5.0 KB

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