GeocodeLibTest.php 14 KB

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