GeocodeLibTest.php 14 KB

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