GeocodeLibTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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::testReverseGeocode()
  72. *
  73. * @return void
  74. */
  75. public function testReverseGeocode() {
  76. $coords = array(
  77. array(-34.594445, -58.37446, 'Florida 1134-1200, Buenos Aires'),
  78. array(48.8934, 8.70492, 'B294, 75175 Pforzheim, Deutschland')
  79. );
  80. foreach ($coords as $coord) {
  81. $is = $this->Geocode->reverseGeocode($coord[0], $coord[1]);
  82. $this->assertTrue($is);
  83. $is = $this->Geocode->getResult();
  84. $this->assertTrue(!empty($is));
  85. //debug($is);
  86. $address = isset($is[0]) ? $is[0]['formatted_address'] : $is['formatted_address'];
  87. $this->assertTextContains($coord[2], $address);
  88. }
  89. }
  90. /**
  91. * Seems to return
  92. * - 'Bibersfelder Besen Weinstube, Luckenbacher Straße 1, 74523 Schwäbisch Hall, Deutschland'
  93. * - point_of_interest, school, establishment
  94. * - 'Bibersfeld, 74523 Schwäbisch Hall, Deutschland'
  95. * - sublocality, political
  96. *
  97. * @return void
  98. */
  99. public function testGeocodeInconclusive() {
  100. $address = 'Bibersfeld';
  101. $this->Geocode->setOptions(array('allow_inconclusive' => true, 'min_accuracy' => GeocodeLib::ACC_POSTAL));
  102. $is = $this->Geocode->geocode($address);
  103. $this->assertTrue($is);
  104. $res = $this->Geocode->getResult();
  105. $this->assertNotEmpty($res);
  106. $is = $this->Geocode->isInconclusive();
  107. $this->assertFalse($is);
  108. // Fake inconclusive here by adding an additional type
  109. $this->Geocode->accuracyTypes[99] = 'point_of_interest';
  110. $this->Geocode->setOptions(array('allow_inconclusive' => false));
  111. $is = $this->Geocode->geocode($address);
  112. $this->assertFalse($is);
  113. $is = $this->Geocode->isInconclusive();
  114. $this->assertTrue($is);
  115. $res = $this->Geocode->getResult();
  116. $this->assertSame(2, $res['valid_results']);
  117. }
  118. /**
  119. * GeocodeLibTest::testInvalid()
  120. *
  121. * @return void
  122. */
  123. public function testInvalid() {
  124. $this->Geocode->setOptions(array('allow_inconclusive' => false));
  125. $result = $this->Geocode->geocode('204 HWY 287 SOUTH, CACTUS, TX, 79013');
  126. $this->assertFalse($result);
  127. }
  128. /**
  129. * With lower min accuracy
  130. *
  131. * @return void
  132. */
  133. public function testGeocodeInconclusiveMinAccuracy() {
  134. $address = 'Bibersfeld';
  135. $this->Geocode->setOptions(array('allow_inconclusive' => true, 'min_accuracy' => GeocodeLib::ACC_STREET));
  136. $is = $this->Geocode->geocode($address);
  137. $this->assertFalse($is);
  138. }
  139. /**
  140. * Seems to return
  141. * - 'Bibersfelder Besen Weinstube, Luckenbacher Straße 1, 74523 Schwäbisch Hall, Deutschland'
  142. * - point_of_interest, school, establishment
  143. * - 'Bibersfeld, 74523 Schwäbisch Hall, Deutschland'
  144. * - sublocality, political
  145. *
  146. * @return void
  147. */
  148. public function testGeocodeExpect() {
  149. $address = 'Bibersfeld';
  150. $this->Geocode->setOptions(array(
  151. 'allow_inconclusive' => true,
  152. 'expect' => array(GeocodeLib::ACC_POSTAL, GeocodeLib::ACC_LOC, GeocodeLib::ACC_SUBLOC)));
  153. $is = $this->Geocode->geocode($address);
  154. $this->assertTrue($is);
  155. $this->Geocode->setOptions(array(
  156. 'allow_inconclusive' => true,
  157. 'expect' => array(GeocodeLib::ACC_POSTAL, GeocodeLib::ACC_LOC)));
  158. $is = $this->Geocode->geocode($address);
  159. $this->assertFalse($is);
  160. }
  161. /**
  162. * GeocodeLibTest::testDistance()
  163. *
  164. * @return void
  165. */
  166. public function testDistance() {
  167. $coords = array(
  168. 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),
  169. 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),
  170. array('name' => 'MUC/NewYork (--- road, ---h)', 'x' => array('lat' => 48.1391, 'lng' => 11.5802), 'y' => array('lat' => 40.700943, 'lng' => -73.853531), 'd' => 6479)
  171. );
  172. foreach ($coords as $coord) {
  173. $is = $this->Geocode->distance($coord['x'], $coord['y']);
  174. $this->assertEquals($coord['d'], $is);
  175. }
  176. $is = $this->Geocode->distance($coords[0]['x'], $coords[0]['y'], GeocodeLib::UNIT_MILES);
  177. $this->assertEquals(142, $is);
  178. // String directly
  179. $is = $this->Geocode->distance($coords[0]['x'], $coords[0]['y'], 'F');
  180. $this->assertEquals(747236, $is);
  181. }
  182. /**
  183. * GeocodeLibTest::testBlur()
  184. *
  185. * @return void
  186. */
  187. public function testBlur() {
  188. $coords = array(
  189. array(48.1391, 1, 0.002), //'y'=>array('lat'=>48.8934, 'lng'=>8.70492), 'd'=>228),
  190. array(11.5802, 1, 0.002),
  191. );
  192. foreach ($coords as $coord) {
  193. $is = $this->Geocode->blur($coord[0], $coord[1]);
  194. //pr('is: '.$is.' - expected: '.$coord[0].' +- '.$coord[2]);
  195. $this->assertWithinMargin($is, $coord[0], $coord[2]);
  196. $this->assertNotWithinMargin($is, $coord[0], $coord[2] / 4);
  197. }
  198. }
  199. /**
  200. * GeocodeLibTest::testConvert()
  201. *
  202. * @return void
  203. */
  204. public function testConvert() {
  205. $values = array(
  206. array(3, 'M', 'K', 4.828032),
  207. array(3, 'K', 'M', 1.86411358),
  208. array(100000, 'I', 'K', 2.54),
  209. );
  210. foreach ($values as $value) {
  211. $is = $this->Geocode->convert($value[0], $value[1], $value[2]);
  212. $this->assertEquals($value[3], round($is, 8));
  213. }
  214. }
  215. /**
  216. * GeocodeLibTest::testUrl()
  217. *
  218. * @return void
  219. */
  220. public function testUrl() {
  221. $ReflectionClass = new ReflectionClass('GeocodeLib');
  222. $Method = $ReflectionClass->getMethod('_url');
  223. $Method->setAccessible(true);
  224. $is = $Method->invoke($this->Geocode);
  225. $this->assertPattern('#https://maps.googleapis.com/maps/api/geocode/json#', $is);
  226. }
  227. /**
  228. * GeocodeLibTest::testSetParams()
  229. *
  230. * @return void
  231. */
  232. public function testSetParams() {
  233. }
  234. /**
  235. * GeocodeLibTest::testSetOptions()
  236. *
  237. * @return void
  238. */
  239. public function testSetOptions() {
  240. $this->Geocode->setOptions(array('host' => 'maps.google.it'));
  241. // should now be ".it"
  242. $ReflectionClass = new ReflectionClass('GeocodeLib');
  243. $Method = $ReflectionClass->getMethod('_url');
  244. $Method->setAccessible(true);
  245. $result = $Method->invoke($this->Geocode);
  246. $this->assertTextContains('maps.google.it', $result);
  247. }
  248. /**
  249. * GeocodeLibTest::testGeocode()
  250. *
  251. * @return void
  252. */
  253. public function testGeocode() {
  254. $address = '74523 Deutschland';
  255. //echo '<h2>'.$address.'</h2>';
  256. $is = $this->Geocode->geocode($address);
  257. //debug($is);
  258. $this->assertTrue($is);
  259. $is = $this->Geocode->getResult();
  260. //debug($is);
  261. $this->assertTrue(!empty($is));
  262. $is = $this->Geocode->error();
  263. //debug($is);
  264. $this->assertTrue(empty($is));
  265. $address = 'Leopoldstraße 100, München';
  266. //echo '<h2>'.$address.'</h2>';
  267. $is = $this->Geocode->geocode($address);
  268. //debug($is);
  269. $this->assertTrue($is);
  270. //pr($this->Geocode->debug());
  271. $is = $this->Geocode->getResult();
  272. //debug($is);
  273. $this->assertTrue(!empty($is));
  274. $is = $this->Geocode->error();
  275. //debug($is);
  276. $this->assertTrue(empty($is));
  277. $address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  278. //echo '<h2>'.$address.'</h2>';
  279. $is = $this->Geocode->geocode($address);
  280. //debug($is);
  281. $this->assertTrue($is);
  282. //pr($this->Geocode->debug());
  283. $is = $this->Geocode->getResult();
  284. //debug($is);
  285. $this->assertTrue(!empty($is));
  286. $is = $this->Geocode->error();
  287. //debug($is);
  288. $this->assertTrue(empty($is));
  289. }
  290. /**
  291. * GeocodeLibTest::testGeocodeReachedQueryLimit()
  292. *
  293. * @return void
  294. */
  295. public function testGeocodeReachedQueryLimit() {
  296. $this->Geocode->reachedQueryLimit = true;
  297. $address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  298. $result = $this->Geocode->geocode($address);
  299. $this->assertFalse($result);
  300. $result = $this->Geocode->error();
  301. $this->assertEquals('Over Query Limit - abort', $result);
  302. }
  303. /**
  304. * GeocodeLibTest::testGeocodeBadApiKey()
  305. *
  306. * @return void
  307. */
  308. public function testGeocodeBadApiKey() {
  309. $address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  310. $result = $this->Geocode->geocode($address, array('sensor' => false, 'key' => 'testingBadApiKey'));
  311. $this->assertFalse($result);
  312. $result = $this->Geocode->error();
  313. $this->assertEquals('Error REQUEST_DENIED (The provided API key is invalid.)', $result);
  314. }
  315. /**
  316. * GeocodeLibTest::testGeocodeInvalid()
  317. *
  318. * @return void
  319. */
  320. public function testGeocodeInvalid() {
  321. $address = 'Hjfjosdfhosj, 78878 Mdfkufsdfk';
  322. $result = $this->Geocode->geocode($address);
  323. $this->assertFalse($result);
  324. $result = $this->Geocode->error();
  325. $this->assertTrue(!empty($result));
  326. }
  327. /**
  328. * GeocodeLibTest::testGetMaxAddress()
  329. *
  330. * @return void
  331. */
  332. public function testGetMaxAddress() {
  333. $ReflectionClass = new ReflectionClass('GeocodeLib');
  334. $Method = $ReflectionClass->getMethod('_getMaxAccuracy');
  335. $Method->setAccessible(true);
  336. $result = $Method->invoke($this->Geocode, array('street_address' => 'abc'));
  337. $this->assertSame(GeocodeLib::ACC_STREET, $result);
  338. $result = $Method->invoke($this->Geocode, array('intersection' => 'abc'));
  339. $this->assertSame(GeocodeLib::ACC_INTERSEC, $result);
  340. $result = $Method->invoke($this->Geocode, array('route' => 'abc'));
  341. $this->assertSame(GeocodeLib::ACC_ROUTE, $result);
  342. $result = $Method->invoke($this->Geocode, array('sublocality' => 'abc'));
  343. $this->assertSame(GeocodeLib::ACC_SUBLOC, $result);
  344. $result = $Method->invoke($this->Geocode, array('locality' => 'abc'));
  345. $this->assertSame(GeocodeLib::ACC_LOC, $result);
  346. $result = $Method->invoke($this->Geocode, array('postal_code' => 'abc'));
  347. $this->assertSame(GeocodeLib::ACC_POSTAL, $result);
  348. $result = $Method->invoke($this->Geocode, array('country' => 'abc'));
  349. $this->assertSame(GeocodeLib::ACC_COUNTRY, $result);
  350. $result = $Method->invoke($this->Geocode, array());
  351. $this->assertSame(null, $result);
  352. // mixed
  353. $result = $Method->invoke($this->Geocode, array(
  354. 'country' => 'aa',
  355. 'postal_code' => 'abc',
  356. 'locality' => '',
  357. 'street_address' => '',
  358. ));
  359. $this->assertSame(GeocodeLib::ACC_POSTAL, $result);
  360. }
  361. /**
  362. * GeocodeLibTest::testGeocodeMinAcc()
  363. *
  364. * @return void
  365. */
  366. public function testGeocodeMinAcc() {
  367. // address = postal_code, minimum = street level
  368. $address = 'Deutschland';
  369. $this->Geocode->setOptions(array('min_accuracy' => GeocodeLib::ACC_STREET));
  370. $is = $this->Geocode->geocode($address);
  371. $this->assertFalse($is);
  372. $is = $this->Geocode->error();
  373. $this->assertTrue(!empty($is));
  374. }
  375. /**
  376. * GeocodeLibTest::testTransformData()
  377. *
  378. * @return void
  379. */
  380. public function testTransformData() {
  381. $ReflectionClass = new ReflectionClass('GeocodeLib');
  382. $Method = $ReflectionClass->getMethod('_transformData');
  383. $Method->setAccessible(true);
  384. // non-full records
  385. $data = array('types' => array());
  386. $this->assertEquals($data, $Method->invoke($this->Geocode, $data));
  387. $data = array();
  388. $this->assertEquals($data, $Method->invoke($this->Geocode, $data));
  389. // Full record
  390. $ReflectionClass = new ReflectionClass('GeocodeLib');
  391. $Method = $ReflectionClass->getMethod('_transform');
  392. $Method->setAccessible(true);
  393. $data = json_decode($this->apiMockupReverseGeocode40206['raw'], true);
  394. $expected = array(
  395. 'results' => array(
  396. array(
  397. 'formatted_address' => 'Louisville, KY 40206, USA',
  398. // organized location components
  399. 'country' => 'United States',
  400. 'country_code' => 'US',
  401. 'country_province' => 'Kentucky',
  402. 'country_province_code' => 'KY',
  403. 'postal_code' => '40206',
  404. 'locality' => 'Louisville',
  405. 'sublocality' => '',
  406. 'route' => '',
  407. // vetted "types"
  408. 'types' => array(
  409. 'postal_code',
  410. ),
  411. // simple lat/lng
  412. 'lat' => 38.264357800000013,
  413. 'lng' => -85.699978899999991,
  414. 'location_type' => 'APPROXIMATE',
  415. 'viewport' => array(
  416. 'sw' => array(
  417. 'lat' => 38.239565800000001,
  418. 'lng' => -85.744800999999995,
  419. ),
  420. 'ne' => array(
  421. 'lat' => 38.285255800000002,
  422. 'lng' => -85.664309000000003,
  423. ),
  424. ),
  425. 'bounds' => array(
  426. 'sw' => array(
  427. 'lat' => 38.239565800000001,
  428. 'lng' => -85.744800999999995,
  429. ),
  430. 'ne' => array(
  431. 'lat' => 38.285255800000002,
  432. 'lng' => -85.664309000000003,
  433. ),
  434. ),
  435. 'address_components' => array(
  436. array(
  437. 'long_name' => '40206',
  438. 'short_name' => '40206',
  439. 'types' => array(
  440. 'postal_code',
  441. ),
  442. ),
  443. array(
  444. 'long_name' => 'Louisville',
  445. 'short_name' => 'Louisville',
  446. 'types' => array(
  447. 'locality',
  448. 'political',
  449. ),
  450. ),
  451. array(
  452. 'long_name' => 'Kentucky',
  453. 'short_name' => 'KY',
  454. 'types' => array(
  455. 'administrative_area_level_1',
  456. 'political',
  457. ),
  458. ),
  459. array(
  460. 'long_name' => 'United States',
  461. 'short_name' => 'US',
  462. 'types' => array(
  463. 'country',
  464. 'political',
  465. ),
  466. ),
  467. ),
  468. 'valid_type' => true,
  469. 'accuracy' => 4,
  470. 'accuracy_name' => 'postal_code',
  471. ),
  472. ),
  473. 'status' => 'OK',
  474. );
  475. $result = $Method->invoke($this->Geocode, $data);
  476. $this->assertEquals($expected, $result);
  477. }
  478. }