GeocodeLibTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 $apiMockupReverseGeocode40206 = array(
  15. 'reverseGeocode' => array(
  16. 'lat' => '38.2643',
  17. 'lng' => '-85.6999',
  18. 'params' => array(
  19. 'address' => '40206',
  20. 'latlng' => '',
  21. 'region' => '',
  22. 'language' => 'en',
  23. 'bounds' => '',
  24. 'sensor' => 'false',
  25. 'key' => 'AIzaSyAcQWSeMp_RF9W2_g2vOfLlUNCieHtHfFA',
  26. 'result_type' => 'sublocality'
  27. )
  28. ),
  29. '_fetch' => 'https://maps.googleapis.com/maps/api/geocode/json?address=40206&latlng=38.2643%2C-85.6999&language=en&sensor=false',
  30. 'raw' => '{
  31. "results" : [
  32. {
  33. "address_components" : [
  34. { "long_name" : "40206", "short_name" : "40206", "types" : [ "postal_code" ] },
  35. { "long_name" : "Louisville", "short_name" : "Louisville", "types" : [ "locality", "political" ] },
  36. { "long_name" : "Kentucky", "short_name" : "KY", "types" : [ "administrative_area_level_1", "political" ] },
  37. { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] }
  38. ],
  39. "formatted_address" : "Louisville, KY 40206, USA",
  40. "geometry" : {
  41. "bounds" : {
  42. "northeast" : { "lat" : 38.2852558, "lng" : -85.664309 },
  43. "southwest" : { "lat" : 38.2395658, "lng" : -85.744801 }
  44. },
  45. "location" : { "lat" : 38.26435780000001, "lng" : -85.69997889999999 },
  46. "location_type" : "APPROXIMATE",
  47. "viewport" : {
  48. "northeast" : { "lat" : 38.2852558, "lng" : -85.664309 },
  49. "southwest" : { "lat" : 38.2395658, "lng" : -85.744801 }
  50. }
  51. },
  52. "types" : [ "postal_code" ]
  53. }
  54. ],
  55. "status" : "OK"
  56. }',
  57. );
  58. public function setUp() {
  59. parent::setUp();
  60. $this->Geocode = new GeocodeLib();
  61. }
  62. public function tearDown() {
  63. parent::tearDown();
  64. unset($this->Geocode);
  65. }
  66. public function testObject() {
  67. $this->assertTrue(is_object($this->Geocode));
  68. $this->assertInstanceOf('GeocodeLib', $this->Geocode);
  69. }
  70. /**
  71. * GeocodeLibTest::testDistance()
  72. *
  73. * @return void
  74. */
  75. public function testDistance() {
  76. $coords = array(
  77. 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),
  78. 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),
  79. array('name' => 'MUC/NewYork (--- road, ---h)', 'x' => array('lat' => 48.1391, 'lng' => 11.5802), 'y' => array('lat' => 40.700943, 'lng' => -73.853531), 'd' => 6479)
  80. );
  81. foreach ($coords as $coord) {
  82. $is = $this->Geocode->distance($coord['x'], $coord['y']);
  83. //echo $coord['name'].':';
  84. //pr('is: '.$is.' - expected: '.$coord['d']);
  85. $this->assertEquals($coord['d'], $is);
  86. }
  87. }
  88. /**
  89. * GeocodeLibTest::testBlur()
  90. *
  91. * @return void
  92. */
  93. public function testBlur() {
  94. $coords = array(
  95. array(48.1391, 1, 0.002), //'y'=>array('lat'=>48.8934, 'lng'=>8.70492), 'd'=>228),
  96. array(11.5802, 1, 0.002),
  97. );
  98. foreach ($coords as $coord) {
  99. $is = $this->Geocode->blur($coord[0], $coord[1]);
  100. //pr('is: '.$is.' - expected: '.$coord[0].' +- '.$coord[2]);
  101. $this->assertWithinMargin($is, $coord[0], $coord[2]);
  102. $this->assertNotWithinMargin($is, $coord[0], $coord[2] / 4);
  103. }
  104. }
  105. /**
  106. * GeocodeLibTest::testConvert()
  107. *
  108. * @return void
  109. */
  110. public function testConvert() {
  111. $values = array(
  112. array(3, 'M', 'K', 4.828032),
  113. array(3, 'K', 'M', 1.86411358),
  114. array(100000, 'I', 'K', 2.54),
  115. );
  116. foreach ($values as $value) {
  117. $is = $this->Geocode->convert($value[0], $value[1], $value[2]);
  118. //echo $value[0].$value[1].' in '.$value[2].':';
  119. //pr('is: '.returns($is).' - expected: '.$value[3]);
  120. $this->assertEquals($value[3], round($is, 8));
  121. }
  122. }
  123. /**
  124. * GeocodeLibTest::testUrl()
  125. *
  126. * @return void
  127. */
  128. public function testUrl() {
  129. $is = $this->Geocode->url();
  130. $this->assertFalse(empty($is));
  131. $this->assertPattern('#https://maps.googleapis.com/maps/api/geocode/(json|xml)\?.+#', $is);
  132. }
  133. // not possible with protected method
  134. public function _testFetch() {
  135. $url = 'http://maps.google.com/maps/api/geocode/xml?sensor=false&address=74523';
  136. $is = $this->Geocode->_fetch($url);
  137. //debug($is);
  138. $this->assertTrue(!empty($is) && substr($is, 0, 38) === '<?xml version="1.0" encoding="UTF-8"?>');
  139. $url = 'http://maps.google.com/maps/api/geocode/json?sensor=false&address=74523';
  140. $is = $this->Geocode->_fetch($url);
  141. //debug($is);
  142. $this->assertTrue(!empty($is) && substr($is, 0, 1) === '{');
  143. }
  144. public function testSetParams() {
  145. }
  146. /**
  147. * @return void
  148. * @deprecated
  149. */
  150. public function testWithXml() {
  151. $this->Geocode->setOptions(array('output' => 'xml'));
  152. $address = '74523 Deutschland';
  153. //echo '<h2>'.$address.'</h2>';
  154. $is = $this->Geocode->geocode($address);
  155. $this->assertTrue($is);
  156. $is = $this->Geocode->getResult();
  157. //debug($is);
  158. $this->assertTrue(!empty($is));
  159. }
  160. /**
  161. * GeocodeLibTest::testSetOptions()
  162. *
  163. * @return void
  164. */
  165. public function testSetOptions() {
  166. // should be the default
  167. $res = $this->Geocode->url();
  168. $this->assertTextContains('maps.googleapis.com', $res);
  169. $this->Geocode->setOptions(array('host' => 'maps.google.it'));
  170. // should now be ".it"
  171. $res = $this->Geocode->url();
  172. $this->assertTextContains('maps.google.it', $res);
  173. }
  174. /**
  175. * GeocodeLibTest::testGeocode()
  176. *
  177. * @return void
  178. */
  179. public function testGeocode() {
  180. $address = '74523 Deutschland';
  181. //echo '<h2>'.$address.'</h2>';
  182. $is = $this->Geocode->geocode($address);
  183. //debug($is);
  184. $this->assertTrue($is);
  185. $is = $this->Geocode->getResult();
  186. //debug($is);
  187. $this->assertTrue(!empty($is));
  188. $is = $this->Geocode->error();
  189. //debug($is);
  190. $this->assertTrue(empty($is));
  191. $address = 'Leopoldstraße 100, München';
  192. //echo '<h2>'.$address.'</h2>';
  193. $is = $this->Geocode->geocode($address);
  194. //debug($is);
  195. $this->assertTrue($is);
  196. //pr($this->Geocode->debug());
  197. $is = $this->Geocode->getResult();
  198. //debug($is);
  199. $this->assertTrue(!empty($is));
  200. $is = $this->Geocode->error();
  201. //debug($is);
  202. $this->assertTrue(empty($is));
  203. $address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  204. //echo '<h2>'.$address.'</h2>';
  205. $is = $this->Geocode->geocode($address);
  206. //debug($is);
  207. $this->assertTrue($is);
  208. //pr($this->Geocode->debug());
  209. $is = $this->Geocode->getResult();
  210. //debug($is);
  211. $this->assertTrue(!empty($is));
  212. $is = $this->Geocode->error();
  213. //debug($is);
  214. $this->assertTrue(empty($is));
  215. }
  216. /**
  217. * GeocodeLibTest::testGeocodeBadApiKey()
  218. *
  219. * @return void
  220. */
  221. public function testGeocodeBadApiKey() {
  222. $address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  223. $is = $this->Geocode->geocode($address, array('sensor' => false, 'key' => 'testingBadApiKey'));
  224. $this->assertFalse($is);
  225. //pr($this->Geocode->debug());
  226. $is = $this->Geocode->error();
  227. $this->assertEqual('Error REQUEST_DENIED (The provided API key is invalid.)', $is);
  228. }
  229. /**
  230. * GeocodeLibTest::testGeocodeInvalid()
  231. *
  232. * @return void
  233. */
  234. public function testGeocodeInvalid() {
  235. $address = 'Hjfjosdfhosj, 78878 Mdfkufsdfk';
  236. //echo '<h2>'.$address.'</h2>';
  237. $is = $this->Geocode->geocode($address);
  238. //debug($is);
  239. $this->assertFalse($is);
  240. //pr($this->Geocode->debug());
  241. $is = $this->Geocode->error();
  242. //debug($is);
  243. $this->assertTrue(!empty($is));
  244. }
  245. /**
  246. * GeocodeLibTest::testGetMaxAddress()
  247. *
  248. * @return void
  249. */
  250. public function testGetMaxAddress() {
  251. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('street_address' => 'abc')), GeocodeLib::ACC_STREET);
  252. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('intersection' => 'abc')), GeocodeLib::ACC_INTERSEC);
  253. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('route' => 'abc')), GeocodeLib::ACC_ROUTE);
  254. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('sublocality' => 'abc')), GeocodeLib::ACC_SUBLOC);
  255. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('locality' => 'abc')), GeocodeLib::ACC_LOC);
  256. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('postal_code' => 'abc')), GeocodeLib::ACC_POSTAL);
  257. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('country' => 'aa')), GeocodeLib::ACC_COUNTRY);
  258. $this->assertEqual($this->Geocode->_getMaxAccuracy(array()), GeocodeLib::ACC_COUNTRY);
  259. // mixed
  260. $this->assertEqual($this->Geocode->_getMaxAccuracy(array(
  261. 'country' => 'aa',
  262. 'postal_code' => 'abc',
  263. 'locality' => '',
  264. 'street_address' => '',
  265. )), GeocodeLib::ACC_POSTAL);
  266. }
  267. /**
  268. * GeocodeLibTest::testGeocodeMinAcc()
  269. *
  270. * @return void
  271. */
  272. public function testGeocodeMinAcc() {
  273. // address = postal_code, minimum = street level
  274. $address = 'Deutschland';
  275. $this->Geocode->setOptions(array('min_accuracy' => GeocodeLib::ACC_STREET));
  276. $is = $this->Geocode->geocode($address);
  277. $this->assertFalse($is);
  278. $is = $this->Geocode->error();
  279. $this->assertTrue(!empty($is));
  280. }
  281. /**
  282. * GeocodeLibTest::testGeocodeInconclusive()
  283. *
  284. * @return void
  285. */
  286. public function testGeocodeInconclusive() {
  287. // seems like there is no inconclusive result anymore!!!
  288. $address = 'Neustadt';
  289. // allow_inconclusive = TRUE
  290. $this->Geocode->setOptions(array('allow_inconclusive' => true, 'min_accuracy' => GeocodeLib::ACC_POSTAL));
  291. $is = $this->Geocode->geocode($address);
  292. $this->assertTrue($is);
  293. $res = $this->Geocode->getResult();
  294. $this->assertTrue(count($res) > 4);
  295. $is = $this->Geocode->isInconclusive();
  296. $this->assertTrue($is);
  297. $this->Geocode->setOptions(array('allow_inconclusive' => false));
  298. $is = $this->Geocode->geocode($address);
  299. $this->assertFalse($is);
  300. }
  301. /**
  302. * GeocodeLibTest::testReverseGeocode()
  303. *
  304. * @return void
  305. */
  306. public function testReverseGeocode() {
  307. $coords = array(
  308. array(-34.594445, -58.37446, 'Calle Florida 1134-1200, Buenos Aires'),
  309. array(48.8934, 8.70492, 'B294, 75175 Pforzheim, Deutschland')
  310. );
  311. foreach ($coords as $coord) {
  312. $is = $this->Geocode->reverseGeocode($coord[0], $coord[1]);
  313. $this->assertTrue($is);
  314. $is = $this->Geocode->getResult();
  315. $this->assertTrue(!empty($is));
  316. //debug($is);
  317. $address = isset($is[0]) ? $is[0]['formatted_address'] : $is['formatted_address'];
  318. $this->assertTextContains($coord[2], $address);
  319. }
  320. }
  321. /**
  322. * GeocodeLibTest::testTransformData()
  323. *
  324. * @return void
  325. */
  326. public function testTransformData() {
  327. $ReflectionClass = new ReflectionClass('GeocodeLib');
  328. $Method = $ReflectionClass->getMethod('_transformData');
  329. $Method->setAccessible(true);
  330. // non-full records
  331. $data = array('record' => 'OK');
  332. $this->assertEquals($data, $Method->invoke($this->Geocode, $data));
  333. $data = array();
  334. $this->assertEquals($data, $Method->invoke($this->Geocode, $data));
  335. $data = '';
  336. $this->assertEquals($data, $Method->invoke($this->Geocode, $data));
  337. $data = 'abc';
  338. $this->assertEquals($data, $Method->invoke($this->Geocode, $data));
  339. // Full record
  340. $data = json_decode($this->apiMockupReverseGeocode40206['raw'], true);
  341. $expected = array(
  342. 'results' => array(
  343. 0 => array (
  344. 'formatted_address' => 'Louisville, KY 40206, USA',
  345. // organized location components
  346. 'country' => 'United States',
  347. 'country_code' => 'US',
  348. 'country_province' => 'Kentucky',
  349. 'country_province_code' => 'KY',
  350. 'postal_code' => '40206',
  351. 'locality' => 'Louisville',
  352. 'sublocality' => '',
  353. 'route' => '',
  354. // vetted "types"
  355. 'types' => array (
  356. 0 => 'postal_code',
  357. ),
  358. // simple lat/lng
  359. 'lat' => 38.264357800000013,
  360. 'lng' => -85.699978899999991,
  361. 'location_type' => 'APPROXIMATE',
  362. 'viewport' => array (
  363. 'sw' => array (
  364. 'lat' => 38.239565800000001,
  365. 'lng' => -85.744800999999995,
  366. ),
  367. 'ne' => array (
  368. 'lat' => 38.285255800000002,
  369. 'lng' => -85.664309000000003,
  370. ),
  371. ),
  372. 'bounds' => array (
  373. 'sw' => array (
  374. 'lat' => 38.239565800000001,
  375. 'lng' => -85.744800999999995,
  376. ),
  377. 'ne' => array (
  378. 'lat' => 38.285255800000002,
  379. 'lng' => -85.664309000000003,
  380. ),
  381. ),
  382. // injected static maxAccuracy
  383. 'maxAccuracy' => 5,
  384. ),
  385. ),
  386. 'status' => 'OK',
  387. );
  388. $this->assertEquals($expected, $Method->invoke($this->Geocode, $data));
  389. // multiple full records
  390. // TODO:...
  391. }
  392. public function testGetResult() {
  393. }
  394. }