GeocodeLibTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <?php
  2. App::uses('GeocodeLib', 'Tools.Lib');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. App::uses('HttpSocketResponse', 'Network/Http');
  5. # google maps
  6. Configure::write('Google', array(
  7. 'key' => 'ABQIAAAAk-aSeht5vBRyVc9CjdBKLRRnhS8GMCOqu88EXp1O-QqtMSdzHhQM4y1gkHFQdUvwiZgZ6jaKlW40kw', //local
  8. 'api' => '2.x',
  9. 'zoom' => 16,
  10. 'lat' => null,
  11. 'lng' => null,
  12. 'type' => 'G_NORMAL_MAP'
  13. ));
  14. class GeocodeLibTest extends MyCakeTestCase {
  15. public $apiMockupReverseGeocode40206 = array(
  16. 'reverseGeocode' => array(
  17. 'lat' => '38.2643',
  18. 'lng' => '-85.6999',
  19. 'params' => array(
  20. 'address' => '40206',
  21. 'latlng' => '',
  22. 'region' => '',
  23. 'language' => 'en',
  24. 'bounds' => '',
  25. 'sensor' => 'false',
  26. 'key' => 'AIzaSyAcQWSeMp_RF9W2_g2vOfLlUNCieHtHfFA',
  27. 'result_type' => 'sublocality'
  28. )
  29. ),
  30. '_fetch' => 'https://maps.googleapis.com/maps/api/geocode/json?address=40206&latlng=38.2643%2C-85.6999&language=en&sensor=false',
  31. 'raw' => '{
  32. "results" : [
  33. {
  34. "address_components" : [
  35. { "long_name" : "40206", "short_name" : "40206", "types" : [ "postal_code" ] },
  36. { "long_name" : "Louisville", "short_name" : "Louisville", "types" : [ "locality", "political" ] },
  37. { "long_name" : "Kentucky", "short_name" : "KY", "types" : [ "administrative_area_level_1", "political" ] },
  38. { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] }
  39. ],
  40. "formatted_address" : "Louisville, KY 40206, USA",
  41. "geometry" : {
  42. "bounds" : {
  43. "northeast" : { "lat" : 38.2852558, "lng" : -85.664309 },
  44. "southwest" : { "lat" : 38.2395658, "lng" : -85.744801 }
  45. },
  46. "location" : { "lat" : 38.26435780000001, "lng" : -85.69997889999999 },
  47. "location_type" : "APPROXIMATE",
  48. "viewport" : {
  49. "northeast" : { "lat" : 38.2852558, "lng" : -85.664309 },
  50. "southwest" : { "lat" : 38.2395658, "lng" : -85.744801 }
  51. }
  52. },
  53. "types" : [ "postal_code" ]
  54. }
  55. ],
  56. "status" : "OK"
  57. }',
  58. );
  59. public function setUp() {
  60. parent::setUp();
  61. $this->Geocode = new GeocodeLib();
  62. $this->mockFilePath = CakePlugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'google' . DS;
  63. }
  64. public function tearDown() {
  65. parent::tearDown();
  66. unset($this->Geocode);
  67. }
  68. public function testObject() {
  69. $this->assertTrue(is_object($this->Geocode));
  70. $this->assertInstanceOf('GeocodeLib', $this->Geocode);
  71. }
  72. /**
  73. * GeocodeLibTest::testReverseGeocode()
  74. *
  75. * @return void
  76. */
  77. public function testReverseGeocode() {
  78. $coords = array(
  79. array(-34.594445, -58.37446, 'Florida 1134-1200, Buenos Aires'),
  80. array(48.8934, 8.70492, 'B294, 75175 Pforzheim, Deutschland')
  81. );
  82. foreach ($coords as $k => $coord) {
  83. if (!$this->isDebug()) {
  84. $this->_getMock('reverse' . $k);
  85. }
  86. $is = $this->Geocode->reverseGeocode($coord[0], $coord[1]);
  87. $this->assertTrue($is);
  88. $is = $this->Geocode->getResult();
  89. $this->assertTrue(!empty($is));
  90. //debug($is);
  91. $address = isset($is[0]) ? $is[0]['formatted_address'] : $is['formatted_address'];
  92. $this->assertTextContains($coord[2], $address);
  93. }
  94. }
  95. /**
  96. * Seems to return
  97. * - 'Bibersfelder Besen Weinstube, Luckenbacher Straße 1, 74523 Schwäbisch Hall, Deutschland'
  98. * - point_of_interest, school, establishment
  99. * - 'Bibersfeld, 74523 Schwäbisch Hall, Deutschland'
  100. * - sublocality, political
  101. *
  102. * @return void
  103. */
  104. public function testGeocodeInconclusive() {
  105. $address = 'Bibersfeld';
  106. if (!$this->isDebug()) {
  107. $this->_getMock('inconclusive', 2);
  108. }
  109. $this->Geocode->setOptions(array('allow_inconclusive' => true, 'min_accuracy' => GeocodeLib::ACC_POSTAL));
  110. $is = $this->Geocode->geocode($address);
  111. $this->assertTrue($is);
  112. $res = $this->Geocode->getResult();
  113. $this->assertNotEmpty($res);
  114. $is = $this->Geocode->isInconclusive();
  115. $this->assertFalse($is);
  116. // Fake inconclusive here by adding an additional type
  117. $this->Geocode->accuracyTypes[99] = 'point_of_interest';
  118. $this->Geocode->setOptions(array('allow_inconclusive' => false));
  119. $is = $this->Geocode->geocode($address);
  120. $this->assertFalse($is);
  121. $is = $this->Geocode->isInconclusive();
  122. $this->assertTrue($is);
  123. $res = $this->Geocode->getResult();
  124. $this->assertSame(2, $res['valid_results']);
  125. }
  126. /**
  127. * GeocodeLibTest::testInvalid()
  128. *
  129. * @return void
  130. */
  131. public function testInvalid() {
  132. // Dont mock in debug mode (live query), otherwise mock it out
  133. if (!$this->isDebug()) {
  134. $this->_getMock('invalid');
  135. }
  136. $this->Geocode->setOptions(array('allow_inconclusive' => false));
  137. $result = $this->Geocode->geocode('204 HWY 287 SOUTH, CACTUS, TX, 79013');
  138. $this->assertFalse($result);
  139. }
  140. /**
  141. * With lower min accuracy
  142. *
  143. * @return void
  144. */
  145. public function testGeocodeInconclusiveMinAccuracy() {
  146. $address = 'Bibersfeld';
  147. if (!$this->isDebug()) {
  148. $this->_getMock('inconclusive');
  149. }
  150. $this->Geocode->setOptions(array('allow_inconclusive' => true, 'min_accuracy' => GeocodeLib::ACC_STREET));
  151. $is = $this->Geocode->geocode($address);
  152. $this->assertFalse($is);
  153. }
  154. /**
  155. * Seems to return
  156. * - 'Bibersfelder Besen Weinstube, Luckenbacher Straße 1, 74523 Schwäbisch Hall, Deutschland'
  157. * - point_of_interest, school, establishment
  158. * - 'Bibersfeld, 74523 Schwäbisch Hall, Deutschland'
  159. * - sublocality, political
  160. *
  161. * @return void
  162. */
  163. public function testGeocodeExpect() {
  164. $address = 'Bibersfeld';
  165. if (!$this->isDebug()) {
  166. $this->_getMock('inconclusive');
  167. }
  168. $this->Geocode->setOptions(array(
  169. 'allow_inconclusive' => true,
  170. 'expect' => array(GeocodeLib::ACC_POSTAL, GeocodeLib::ACC_LOC, GeocodeLib::ACC_SUBLOC)));
  171. $is = $this->Geocode->geocode($address);
  172. $this->assertTrue($is);
  173. $this->Geocode->setOptions(array(
  174. 'allow_inconclusive' => true,
  175. 'expect' => array(GeocodeLib::ACC_POSTAL, GeocodeLib::ACC_LOC)));
  176. $is = $this->Geocode->geocode($address);
  177. $this->assertFalse($is);
  178. }
  179. /**
  180. * GeocodeLibTest::testDistance()
  181. *
  182. * @return void
  183. */
  184. public function testDistance() {
  185. $coords = array(
  186. 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),
  187. 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),
  188. array('name' => 'MUC/NewYork (--- road, ---h)', 'x' => array('lat' => 48.1391, 'lng' => 11.5802), 'y' => array('lat' => 40.700943, 'lng' => -73.853531), 'd' => 6479)
  189. );
  190. foreach ($coords as $coord) {
  191. $is = $this->Geocode->distance($coord['x'], $coord['y']);
  192. $this->assertEquals($coord['d'], $is);
  193. }
  194. $is = $this->Geocode->distance($coords[0]['x'], $coords[0]['y'], GeocodeLib::UNIT_MILES);
  195. $this->assertEquals(142, $is);
  196. // String directly
  197. $is = $this->Geocode->distance($coords[0]['x'], $coords[0]['y'], 'F');
  198. $this->assertEquals(747236, $is);
  199. }
  200. /**
  201. * GeocodeLibTest::testBlur()
  202. *
  203. * @return void
  204. */
  205. public function testBlur() {
  206. $coords = array(
  207. array(48.1391, 1, 0.002), //'y'=>array('lat'=>48.8934, 'lng'=>8.70492), 'd'=>228),
  208. array(11.5802, 1, 0.002),
  209. );
  210. foreach ($coords as $coord) {
  211. $is = $this->Geocode->blur($coord[0], $coord[1]);
  212. //pr('is: '.$is.' - expected: '.$coord[0].' +- '.$coord[2]);
  213. $this->assertWithinMargin($is, $coord[0], $coord[2]);
  214. $this->assertNotWithinMargin($is, $coord[0], $coord[2] / 4);
  215. }
  216. }
  217. /**
  218. * GeocodeLibTest::testConvert()
  219. *
  220. * @return void
  221. */
  222. public function testConvert() {
  223. $values = array(
  224. array(3, 'M', 'K', 4.828032),
  225. array(3, 'K', 'M', 1.86411358),
  226. array(100000, 'I', 'K', 2.54),
  227. );
  228. foreach ($values as $value) {
  229. $is = $this->Geocode->convert($value[0], $value[1], $value[2]);
  230. $this->assertEquals($value[3], round($is, 8));
  231. }
  232. }
  233. /**
  234. * GeocodeLibTest::testUrl()
  235. *
  236. * @return void
  237. */
  238. public function testUrl() {
  239. $ReflectionClass = new ReflectionClass('GeocodeLib');
  240. $Method = $ReflectionClass->getMethod('_url');
  241. $Method->setAccessible(true);
  242. $is = $Method->invoke($this->Geocode);
  243. $this->assertPattern('#https://maps.googleapis.com/maps/api/geocode/json#', $is);
  244. }
  245. /**
  246. * GeocodeLibTest::testSetParams()
  247. *
  248. * @return void
  249. */
  250. public function testSetParams() {
  251. }
  252. /**
  253. * GeocodeLibTest::testSetOptions()
  254. *
  255. * @return void
  256. */
  257. public function testSetOptions() {
  258. $this->Geocode->setOptions(array('host' => 'maps.google.it'));
  259. // should now be ".it"
  260. $ReflectionClass = new ReflectionClass('GeocodeLib');
  261. $Method = $ReflectionClass->getMethod('_url');
  262. $Method->setAccessible(true);
  263. $result = $Method->invoke($this->Geocode);
  264. $this->assertTextContains('maps.google.it', $result);
  265. }
  266. /**
  267. * GeocodeLibTest::testGeocode()
  268. *
  269. * @return void
  270. */
  271. public function testGeocode() {
  272. if (!$this->isDebug()) {
  273. $this->_getMock('geocode0');
  274. }
  275. $address = '74523 Deutschland';
  276. //echo '<h2>'.$address.'</h2>';
  277. $is = $this->Geocode->geocode($address);
  278. //debug($is);
  279. $this->assertTrue($is);
  280. $is = $this->Geocode->getResult();
  281. //debug($is);
  282. $this->assertTrue(!empty($is));
  283. $is = $this->Geocode->error();
  284. //debug($is);
  285. $this->assertTrue(empty($is));
  286. if (!$this->isDebug()) {
  287. $this->_getMock('geocode1');
  288. }
  289. $address = 'Leopoldstraße 100, München';
  290. //echo '<h2>'.$address.'</h2>';
  291. $is = $this->Geocode->geocode($address);
  292. //debug($is);
  293. $this->assertTrue($is);
  294. //pr($this->Geocode->debug());
  295. $is = $this->Geocode->getResult();
  296. //debug($is);
  297. $this->assertTrue(!empty($is));
  298. $is = $this->Geocode->error();
  299. //debug($is);
  300. $this->assertTrue(empty($is));
  301. if (!$this->isDebug()) {
  302. $this->_getMock('geocode2');
  303. }
  304. $address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  305. //echo '<h2>'.$address.'</h2>';
  306. $is = $this->Geocode->geocode($address);
  307. //debug($is);
  308. $this->assertTrue($is);
  309. //pr($this->Geocode->debug());
  310. $is = $this->Geocode->getResult();
  311. //debug($is);
  312. $this->assertTrue(!empty($is));
  313. $is = $this->Geocode->error();
  314. //debug($is);
  315. $this->assertTrue(empty($is));
  316. }
  317. /**
  318. * GeocodeLibTest::testGeocodeReachedQueryLimit()
  319. *
  320. * @return void
  321. */
  322. public function testGeocodeReachedQueryLimit() {
  323. $this->Geocode->reachedQueryLimit = true;
  324. $address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  325. $result = $this->Geocode->geocode($address);
  326. $this->assertFalse($result);
  327. $result = $this->Geocode->error();
  328. $this->assertEquals('Over Query Limit - abort', $result);
  329. }
  330. /**
  331. * GeocodeLibTest::testGeocodeBadApiKey()
  332. *
  333. * @return void
  334. */
  335. public function testGeocodeBadApiKey() {
  336. if (!$this->isDebug()) {
  337. $this->_getMock('apikey');
  338. }
  339. $address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  340. $result = $this->Geocode->geocode($address, array('sensor' => false, 'key' => 'testingBadApiKey'));
  341. $this->assertFalse($result);
  342. $result = $this->Geocode->error();
  343. $this->assertEquals('Error REQUEST_DENIED (The provided API key is invalid.)', $result);
  344. }
  345. /**
  346. * GeocodeLibTest::testGeocodeInvalid()
  347. *
  348. * @return void
  349. */
  350. public function testGeocodeInvalid() {
  351. if (!$this->isDebug()) {
  352. $this->_getMock('zero');
  353. }
  354. $address = 'Hjfjosdfhosj, 78878 Mdfkufsdfk';
  355. $result = $this->Geocode->geocode($address);
  356. $this->assertFalse($result);
  357. $result = $this->Geocode->error();
  358. $this->assertTrue(!empty($result));
  359. }
  360. /**
  361. * GeocodeLibTest::testGetMaxAddress()
  362. *
  363. * @return void
  364. */
  365. public function testGetMaxAddress() {
  366. $ReflectionClass = new ReflectionClass('GeocodeLib');
  367. $Method = $ReflectionClass->getMethod('_getMaxAccuracy');
  368. $Method->setAccessible(true);
  369. $result = $Method->invoke($this->Geocode, array('street_address' => 'abc'));
  370. $this->assertSame(GeocodeLib::ACC_STREET, $result);
  371. $result = $Method->invoke($this->Geocode, array('intersection' => 'abc'));
  372. $this->assertSame(GeocodeLib::ACC_INTERSEC, $result);
  373. $result = $Method->invoke($this->Geocode, array('route' => 'abc'));
  374. $this->assertSame(GeocodeLib::ACC_ROUTE, $result);
  375. $result = $Method->invoke($this->Geocode, array('sublocality' => 'abc'));
  376. $this->assertSame(GeocodeLib::ACC_SUBLOC, $result);
  377. $result = $Method->invoke($this->Geocode, array('locality' => 'abc'));
  378. $this->assertSame(GeocodeLib::ACC_LOC, $result);
  379. $result = $Method->invoke($this->Geocode, array('postal_code' => 'abc'));
  380. $this->assertSame(GeocodeLib::ACC_POSTAL, $result);
  381. $result = $Method->invoke($this->Geocode, array('country' => 'abc'));
  382. $this->assertSame(GeocodeLib::ACC_COUNTRY, $result);
  383. $result = $Method->invoke($this->Geocode, array());
  384. $this->assertSame(null, $result);
  385. // mixed
  386. $result = $Method->invoke($this->Geocode, array(
  387. 'country' => 'aa',
  388. 'postal_code' => 'abc',
  389. 'locality' => '',
  390. 'street_address' => '',
  391. ));
  392. $this->assertSame(GeocodeLib::ACC_POSTAL, $result);
  393. }
  394. /**
  395. * GeocodeLibTest::testGeocodeMinAcc()
  396. *
  397. * @return void
  398. */
  399. public function testGeocodeMinAcc() {
  400. if (!$this->isDebug()) {
  401. $this->_getMock('minacc');
  402. }
  403. // address = postal_code, minimum = street level
  404. $address = 'Deutschland';
  405. $this->Geocode->setOptions(array('min_accuracy' => GeocodeLib::ACC_STREET));
  406. $is = $this->Geocode->geocode($address);
  407. $this->assertFalse($is);
  408. $is = $this->Geocode->error();
  409. $this->assertTrue(!empty($is));
  410. }
  411. /**
  412. * GeocodeLibTest::testTransformData()
  413. *
  414. * @return void
  415. */
  416. public function testTransformData() {
  417. $ReflectionClass = new ReflectionClass('GeocodeLib');
  418. $Method = $ReflectionClass->getMethod('_transformData');
  419. $Method->setAccessible(true);
  420. // non-full records
  421. $data = array('types' => array());
  422. $this->assertEquals($data, $Method->invoke($this->Geocode, $data));
  423. $data = array();
  424. $this->assertEquals($data, $Method->invoke($this->Geocode, $data));
  425. // Full record
  426. $ReflectionClass = new ReflectionClass('GeocodeLib');
  427. $Method = $ReflectionClass->getMethod('_transform');
  428. $Method->setAccessible(true);
  429. $data = json_decode($this->apiMockupReverseGeocode40206['raw'], true);
  430. $expected = array(
  431. 'results' => array(
  432. array(
  433. 'formatted_address' => 'Louisville, KY 40206, USA',
  434. // organized location components
  435. 'country' => 'United States',
  436. 'country_code' => 'US',
  437. 'country_province' => 'Kentucky',
  438. 'country_province_code' => 'KY',
  439. 'postal_code' => '40206',
  440. 'locality' => 'Louisville',
  441. 'sublocality' => '',
  442. 'route' => '',
  443. // vetted "types"
  444. 'types' => array(
  445. 'postal_code',
  446. ),
  447. // simple lat/lng
  448. 'lat' => 38.264357800000013,
  449. 'lng' => -85.699978899999991,
  450. 'location_type' => 'APPROXIMATE',
  451. 'viewport' => array(
  452. 'sw' => array(
  453. 'lat' => 38.239565800000001,
  454. 'lng' => -85.744800999999995,
  455. ),
  456. 'ne' => array(
  457. 'lat' => 38.285255800000002,
  458. 'lng' => -85.664309000000003,
  459. ),
  460. ),
  461. 'bounds' => array(
  462. 'sw' => array(
  463. 'lat' => 38.239565800000001,
  464. 'lng' => -85.744800999999995,
  465. ),
  466. 'ne' => array(
  467. 'lat' => 38.285255800000002,
  468. 'lng' => -85.664309000000003,
  469. ),
  470. ),
  471. 'address_components' => array(
  472. array(
  473. 'long_name' => '40206',
  474. 'short_name' => '40206',
  475. 'types' => array(
  476. 'postal_code',
  477. ),
  478. ),
  479. array(
  480. 'long_name' => 'Louisville',
  481. 'short_name' => 'Louisville',
  482. 'types' => array(
  483. 'locality',
  484. 'political',
  485. ),
  486. ),
  487. array(
  488. 'long_name' => 'Kentucky',
  489. 'short_name' => 'KY',
  490. 'types' => array(
  491. 'administrative_area_level_1',
  492. 'political',
  493. ),
  494. ),
  495. array(
  496. 'long_name' => 'United States',
  497. 'short_name' => 'US',
  498. 'types' => array(
  499. 'country',
  500. 'political',
  501. ),
  502. ),
  503. ),
  504. 'valid_type' => true,
  505. 'accuracy' => 4,
  506. 'accuracy_name' => 'postal_code',
  507. ),
  508. ),
  509. 'status' => 'OK',
  510. );
  511. $result = $Method->invoke($this->Geocode, $data);
  512. $this->assertEquals($expected, $result);
  513. }
  514. protected function _getMock($type, $count = 1) {
  515. $this->Geocode->HttpSocket = $this->getMock('HttpSocket', array('get'));
  516. $responseContent = file_get_contents($this->mockFilePath . $type . '.json');
  517. $response = new HttpSocketResponse();
  518. $response->body = $responseContent;
  519. $response->code = 200;
  520. $this->Geocode->HttpSocket->response = $response;
  521. for ($i = 0; $i < $count; $i++) {
  522. $this->Geocode->HttpSocket->expects($this->at($i))
  523. ->method('get')
  524. ->will($this->returnValue($response));
  525. }
  526. }
  527. }