GeocodeLibTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. parent::setUp();
  16. $this->Geocode = new GeocodeLib();
  17. }
  18. public function tearDown() {
  19. parent::tearDown();
  20. unset($this->Geocode);
  21. }
  22. public function testObject() {
  23. $this->assertTrue(is_object($this->Geocode));
  24. $this->assertInstanceOf('GeocodeLib', $this->Geocode);
  25. }
  26. public function testDistance() {
  27. $coords = array(
  28. 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),
  29. 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),
  30. array('name' => 'MUC/NewYork (--- road, ---h)', 'x' => array('lat' => 48.1391, 'lng' => 11.5802), 'y' => array('lat' => 40.700943, 'lng' => -73.853531), 'd' => 6479)
  31. );
  32. foreach ($coords as $coord) {
  33. $is = $this->Geocode->distance($coord['x'], $coord['y']);
  34. //echo $coord['name'].':';
  35. //pr('is: '.$is.' - expected: '.$coord['d']);
  36. $this->assertEquals($coord['d'], $is);
  37. }
  38. }
  39. public function testBlur() {
  40. $coords = array(
  41. array(48.1391, 1, 0.002), //'y'=>array('lat'=>48.8934, 'lng'=>8.70492), 'd'=>228),
  42. array(11.5802, 1, 0.002),
  43. );
  44. foreach ($coords as $coord) {
  45. $is = $this->Geocode->blur($coord[0], $coord[1]);
  46. //pr('is: '.$is.' - expected: '.$coord[0].' +- '.$coord[2]);
  47. $this->assertWithinMargin($is, $coord[0], $coord[2]);
  48. $this->assertNotWithinMargin($is, $coord[0], $coord[2] / 4);
  49. }
  50. }
  51. public function testConvert() {
  52. $values = array(
  53. array(3, 'M', 'K', 4.828032),
  54. array(3, 'K', 'M', 1.86411358),
  55. array(100000, 'I', 'K', 2.54),
  56. );
  57. foreach ($values as $value) {
  58. $is = $this->Geocode->convert($value[0], $value[1], $value[2]);
  59. //echo $value[0].$value[1].' in '.$value[2].':';
  60. //pr('is: '.returns($is).' - expected: '.$value[3]);
  61. $this->assertEquals($value[3], round($is, 8));
  62. }
  63. }
  64. public function testUrl() {
  65. $is = $this->Geocode->url();
  66. $this->assertFalse(empty($is));
  67. $this->assertPattern('#https://maps.googleapis.com/maps/api/geocode/(json|xml)\?.+#', $is);
  68. }
  69. // not possible with protected method
  70. public function _testFetch() {
  71. $url = 'http://maps.google.com/maps/api/geocode/xml?sensor=false&address=74523';
  72. $is = $this->Geocode->_fetch($url);
  73. //debug($is);
  74. $this->assertTrue(!empty($is) && substr($is, 0, 38) === '<?xml version="1.0" encoding="UTF-8"?>');
  75. $url = 'http://maps.google.com/maps/api/geocode/json?sensor=false&address=74523';
  76. $is = $this->Geocode->_fetch($url);
  77. //debug($is);
  78. $this->assertTrue(!empty($is) && substr($is, 0, 1) === '{');
  79. }
  80. public function testSetParams() {
  81. }
  82. public function testWithJson() {
  83. $this->Geocode->setOptions(array('output' => 'json'));
  84. $address = '74523 Deutschland';
  85. //echo '<h2>'.$address.'</h2>';
  86. $is = $this->Geocode->geocode($address);
  87. $this->assertTrue($is);
  88. $is = $this->Geocode->getResult();
  89. //debug($is);
  90. $this->assertTrue(!empty($is));
  91. }
  92. public function testSetOptions() {
  93. // should be the default
  94. $res = $this->Geocode->url();
  95. $this->assertTextContains('maps.googleapis.com', $res);
  96. $this->Geocode->setOptions(array('host' => 'maps.google.it'));
  97. // should now be ".it"
  98. $res = $this->Geocode->url();
  99. $this->assertTextContains('maps.google.it', $res);
  100. }
  101. public function testGeocode() {
  102. $address = '74523 Deutschland';
  103. //echo '<h2>'.$address.'</h2>';
  104. $is = $this->Geocode->geocode($address);
  105. //debug($is);
  106. $this->assertTrue($is);
  107. $is = $this->Geocode->getResult();
  108. //debug($is);
  109. $this->assertTrue(!empty($is));
  110. $is = $this->Geocode->error();
  111. //debug($is);
  112. $this->assertTrue(empty($is));
  113. $address = 'Leopoldstraße 100, München';
  114. //echo '<h2>'.$address.'</h2>';
  115. $is = $this->Geocode->geocode($address);
  116. //debug($is);
  117. $this->assertTrue($is);
  118. //pr($this->Geocode->debug());
  119. $is = $this->Geocode->getResult();
  120. //debug($is);
  121. $this->assertTrue(!empty($is));
  122. $is = $this->Geocode->error();
  123. //debug($is);
  124. $this->assertTrue(empty($is));
  125. $address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  126. //echo '<h2>'.$address.'</h2>';
  127. $is = $this->Geocode->geocode($address);
  128. //debug($is);
  129. $this->assertTrue($is);
  130. //pr($this->Geocode->debug());
  131. $is = $this->Geocode->getResult();
  132. //debug($is);
  133. $this->assertTrue(!empty($is));
  134. $is = $this->Geocode->error();
  135. //debug($is);
  136. $this->assertTrue(empty($is));
  137. }
  138. public function testGeocodeBadApiKey() {
  139. $address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  140. $is = $this->Geocode->geocode($address, array('sensor' => false, 'key' => 'testingBadApiKey'));
  141. $this->assertFalse($is);
  142. //pr($this->Geocode->debug());
  143. $is = $this->Geocode->error();
  144. $this->assertEqual('Error REQUEST_DENIED (The provided API key is invalid.)', $is);
  145. }
  146. public function testGeocodeInvalid() {
  147. $address = 'Hjfjosdfhosj, 78878 Mdfkufsdfk';
  148. //echo '<h2>'.$address.'</h2>';
  149. $is = $this->Geocode->geocode($address);
  150. //debug($is);
  151. $this->assertFalse($is);
  152. //pr($this->Geocode->debug());
  153. $is = $this->Geocode->error();
  154. //debug($is);
  155. $this->assertTrue(!empty($is));
  156. }
  157. public function testGetMaxAddress() {
  158. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('street_address' => 'abc')), GeocodeLib::ACC_STREET);
  159. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('intersection' => 'abc')), GeocodeLib::ACC_INTERSEC);
  160. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('route' => 'abc')), GeocodeLib::ACC_ROUTE);
  161. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('sublocality' => 'abc')), GeocodeLib::ACC_SUBLOC);
  162. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('locality' => 'abc')), GeocodeLib::ACC_LOC);
  163. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('postal_code' => 'abc')), GeocodeLib::ACC_POSTAL);
  164. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('country' => 'aa')), GeocodeLib::ACC_COUNTRY);
  165. $this->assertEqual($this->Geocode->_getMaxAccuracy(array()), GeocodeLib::ACC_COUNTRY);
  166. // mixed
  167. $this->assertEqual($this->Geocode->_getMaxAccuracy(array(
  168. 'country' => 'aa',
  169. 'postal_code' => 'abc',
  170. 'locality' => '',
  171. 'street_address' => '',
  172. )), GeocodeLib::ACC_POSTAL);
  173. }
  174. public function testGeocodeMinAcc() {
  175. // address = postal_code, minimum = street level
  176. $address = 'Deutschland';
  177. $this->Geocode->setOptions(array('min_accuracy' => GeocodeLib::ACC_STREET));
  178. $is = $this->Geocode->geocode($address);
  179. $this->assertFalse($is);
  180. $is = $this->Geocode->error();
  181. $this->assertTrue(!empty($is));
  182. }
  183. public function testGeocodeInconclusive() {
  184. // seems like there is no inconclusive result anymore!!!
  185. $address = 'Neustadt';
  186. // allow_inconclusive = TRUE
  187. $this->Geocode->setOptions(array('allow_inconclusive' => true, 'min_accuracy' => GeocodeLib::ACC_POSTAL));
  188. $is = $this->Geocode->geocode($address);
  189. $this->assertTrue($is);
  190. $res = $this->Geocode->getResult();
  191. $this->assertTrue(count($res) > 4);
  192. $is = $this->Geocode->isInconclusive();
  193. $this->assertTrue($is);
  194. $this->Geocode->setOptions(array('allow_inconclusive' => false));
  195. $is = $this->Geocode->geocode($address);
  196. $this->assertFalse($is);
  197. }
  198. public function testReverseGeocode() {
  199. $coords = array(
  200. array(-34.594445, -58.37446, 'Calle Florida 1134-1200, Buenos Aires'),
  201. array(48.8934, 8.70492, 'B294, 75175 Pforzheim, Deutschland')
  202. );
  203. foreach ($coords as $coord) {
  204. $is = $this->Geocode->reverseGeocode($coord[0], $coord[1]);
  205. $this->assertTrue($is);
  206. $is = $this->Geocode->getResult();
  207. $this->assertTrue(!empty($is));
  208. //debug($is);
  209. $address = isset($is[0]) ? $is[0]['formatted_address'] : $is['formatted_address'];
  210. $this->assertTextContains($coord[2], $address);
  211. }
  212. }
  213. public function testGetResult() {
  214. }
  215. }