GeocodeLibTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. App::uses('GeocodeLib', 'Tools.Lib');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. # google maps
  5. Configure::write('Google', array(
  6. 'key' => 'ABQIAAAAk-aSeht5vBRyVc9CjdBKLRRnhS8GMCOqu88EXp1O-QqtMSdzHhQM4y1gkHFQdUvwiZgZ6jaKlW40kw', //local
  7. 'api' => '2.x',
  8. 'zoom' => 16,
  9. 'lat' => null,
  10. 'lng' => null,
  11. 'type' => 'G_NORMAL_MAP'
  12. ));
  13. class GeocodeLibTest extends MyCakeTestCase {
  14. public function setUp() {
  15. $this->Geocode = new GeocodeLib();
  16. }
  17. public function TearDown() {
  18. unset($this->Geocode);
  19. }
  20. public function testObject() {
  21. $this->assertTrue(is_object($this->Geocode));
  22. $this->assertTrue(is_a($this->Geocode, 'GeocodeLib'));
  23. }
  24. public function testDistance() {
  25. $coords = array(
  26. array('name'=>'MUC/Pforzheim (269km road, 2:33h)', 'x'=>array('lat'=>48.1391, 'lng'=>11.5802), 'y'=>array('lat'=>48.8934, 'lng'=>8.70492), 'd'=>228),
  27. array('name'=>'MUC/London (1142km road, 11:20h)', 'x'=>array('lat'=>48.1391, 'lng'=>11.5802), 'y'=>array('lat'=>51.508, 'lng'=>-0.124688), 'd'=>919),
  28. array('name'=>'MUC/NewYork (--- road, ---h)', 'x'=>array('lat'=>48.1391, 'lng'=>11.5802), 'y'=>array('lat'=>40.700943, 'lng'=>-73.853531), 'd'=>6479)
  29. );
  30. foreach ($coords as $coord) {
  31. $is = $this->Geocode->distance($coord['x'], $coord['y']);
  32. //echo $coord['name'].':';
  33. //pr('is: '.$is.' - expected: '.$coord['d']);
  34. $this->assertEquals($coord['d'], $is);
  35. }
  36. }
  37. public function testBlur() {
  38. $coords = array(
  39. array(48.1391, 1, 0.002), //'y'=>array('lat'=>48.8934, 'lng'=>8.70492), 'd'=>228),
  40. array(11.5802, 1, 0.002),
  41. );
  42. foreach ($coords as $coord) {
  43. $is = $this->Geocode->blur($coord[0], $coord[1]);
  44. //pr('is: '.$is.' - expected: '.$coord[0].' +- '.$coord[2]);
  45. $this->assertWithinMargin($is, $coord[0], $coord[2]);
  46. $this->assertNotWithinMargin($is, $coord[0], $coord[2] / 4);
  47. }
  48. }
  49. public function testConvert() {
  50. $values = array(
  51. array(3, 'M', 'K', 4.828032),
  52. array(3, 'K', 'M', 1.86411358),
  53. array(100000, 'I', 'K', 2.54),
  54. );
  55. foreach ($values as $value) {
  56. $is = $this->Geocode->convert($value[0], $value[1], $value[2]);
  57. //echo $value[0].$value[1].' in '.$value[2].':';
  58. //pr('is: '.returns($is).' - expected: '.$value[3]);
  59. $this->assertEquals($value[3], round($is, 8));
  60. }
  61. }
  62. public function testUrl() {
  63. $is = $this->Geocode->url();
  64. //debug($is);
  65. $this->assertTrue(!empty($is) && strpos($is, 'http://maps.googleapis.com/maps/api/geocode/xml?') === 0);
  66. }
  67. // not possible with protected method
  68. public function _testFetch() {
  69. $url = 'http://maps.google.com/maps/api/geocode/xml?sensor=false&address=74523';
  70. $is = $this->Geocode->_fetch($url);
  71. //debug($is);
  72. $this->assertTrue(!empty($is) && substr($is, 0, 38) === '<?xml version="1.0" encoding="UTF-8"?>');
  73. $url = 'http://maps.google.com/maps/api/geocode/json?sensor=false&address=74523';
  74. $is = $this->Geocode->_fetch($url);
  75. //debug($is);
  76. $this->assertTrue(!empty($is) && substr($is, 0, 1) === '{');
  77. }
  78. public function testSetParams() {
  79. }
  80. public function testWithJson() {
  81. $this->Geocode->setOptions(array('output' => 'json'));
  82. $address = '74523 Deutschland';
  83. //echo '<h2>'.$address.'</h2>';
  84. $is = $this->Geocode->geocode($address);
  85. $this->assertTrue($is);
  86. $is = $this->Geocode->getResult();
  87. //debug($is);
  88. $this->assertTrue(!empty($is));
  89. }
  90. public function testSetOptions() {
  91. # should be the default
  92. $res = $this->Geocode->url();
  93. $this->assertTextContains('maps.googleapis.com', $res);
  94. $this->Geocode->setOptions(array('host'=>'maps.google.it'));
  95. # should now be ".it"
  96. $res = $this->Geocode->url();
  97. $this->assertTextContains('maps.google.it', $res);
  98. }
  99. public function testGeocode() {
  100. $address = '74523 Deutschland';
  101. //echo '<h2>'.$address.'</h2>';
  102. $is = $this->Geocode->geocode($address);
  103. //debug($is);
  104. $this->assertTrue($is);
  105. $is = $this->Geocode->getResult();
  106. //debug($is);
  107. $this->assertTrue(!empty($is));
  108. $is = $this->Geocode->error();
  109. //debug($is);
  110. $this->assertTrue(empty($is));
  111. $address = 'Leopoldstraße 100, München';
  112. //echo '<h2>'.$address.'</h2>';
  113. $is = $this->Geocode->geocode($address);
  114. //debug($is);
  115. $this->assertTrue($is);
  116. //pr($this->Geocode->debug());
  117. $is = $this->Geocode->getResult();
  118. //debug($is);
  119. $this->assertTrue(!empty($is));
  120. $is = $this->Geocode->error();
  121. //debug($is);
  122. $this->assertTrue(empty($is));
  123. $address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  124. //echo '<h2>'.$address.'</h2>';
  125. $is = $this->Geocode->geocode($address);
  126. //debug($is);
  127. $this->assertTrue($is);
  128. //pr($this->Geocode->debug());
  129. $is = $this->Geocode->getResult();
  130. //debug($is);
  131. $this->assertTrue(!empty($is));
  132. $is = $this->Geocode->error();
  133. //debug($is);
  134. $this->assertTrue(empty($is));
  135. }
  136. public function testGeocodeInvalid() {
  137. $address = 'Hjfjosdfhosj, 78878 Mdfkufsdfk';
  138. //echo '<h2>'.$address.'</h2>';
  139. $is = $this->Geocode->geocode($address);
  140. //debug($is);
  141. $this->assertFalse($is);
  142. //pr($this->Geocode->debug());
  143. $is = $this->Geocode->error();
  144. //debug($is);
  145. $this->assertTrue(!empty($is));
  146. }
  147. public function testGeocodeMinAcc() {
  148. $address = 'Deutschland';
  149. //echo '<h2>'.$address.'</h2>';
  150. $this->Geocode->setOptions(array('min_accuracy'=>3));
  151. $is = $this->Geocode->geocode($address);
  152. //debug($is);
  153. $this->assertFalse($is);
  154. $is = $this->Geocode->error();
  155. //debug($is);
  156. $this->assertTrue(!empty($is));
  157. }
  158. public function testGeocodeInconclusive() {
  159. // seems like there is no inconclusive result anymore!!!
  160. $address = 'Neustadt';
  161. //echo '<h2>'.$address.'</h2>';
  162. # allow_inconclusive = TRUE
  163. $this->Geocode->setOptions(array('allow_inconclusive'=>true, 'min_accuracy' => GeocodeLib::ACC_LOC));
  164. $is = $this->Geocode->geocode($address);
  165. //echo 'debug:';
  166. //pr($this->Geocode->debug());
  167. //echo 'debug end';
  168. $this->assertTrue($is);
  169. $res = $this->Geocode->getResult();
  170. //pr($res);
  171. $this->assertTrue(count($res) > 4);
  172. $is = $this->Geocode->isInconclusive();
  173. $this->assertTrue($is);
  174. $this->Geocode->setOptions(array('allow_inconclusive'=>false));
  175. $is = $this->Geocode->geocode($address);
  176. $this->assertFalse($is);
  177. }
  178. public function testReverseGeocode() {
  179. $coords = array(
  180. array(-34.594445, -58.37446, 'Calle Florida 1134-1200, Buenos Aires'),
  181. array(48.8934, 8.70492, 'B294, 75175 Pforzheim, Deutschland')
  182. );
  183. foreach ($coords as $coord) {
  184. $is = $this->Geocode->reverseGeocode($coord[0], $coord[1]);
  185. $this->assertTrue($is);
  186. $is = $this->Geocode->getResult();
  187. $this->assertTrue(!empty($is));
  188. //debug($is);
  189. $address = isset($is[0]) ? $is[0]['formatted_address'] : $is['formatted_address'];
  190. $this->assertTextContains($coord[2], $address);
  191. }
  192. }
  193. public function testGetResult() {
  194. }
  195. }