GeocodeLibTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. $ReflectionClass = new ReflectionClass('GeocodeLib');
  210. $Method = $ReflectionClass->getMethod('_url');
  211. $Method->setAccessible(true);
  212. $is = $Method->invoke($this->Geocode);
  213. $this->assertPattern('#https://maps.googleapis.com/maps/api/geocode/json#', $is);
  214. }
  215. /**
  216. * GeocodeLibTest::testSetParams()
  217. *
  218. * @return void
  219. */
  220. public function testSetParams() {
  221. }
  222. /**
  223. * GeocodeLibTest::testSetOptions()
  224. *
  225. * @return void
  226. */
  227. public function testSetOptions() {
  228. $this->Geocode->setOptions(array('host' => 'maps.google.it'));
  229. // should now be ".it"
  230. $ReflectionClass = new ReflectionClass('GeocodeLib');
  231. $Method = $ReflectionClass->getMethod('_url');
  232. $Method->setAccessible(true);
  233. $result = $Method->invoke($this->Geocode);
  234. $this->assertTextContains('maps.google.it', $result);
  235. }
  236. /**
  237. * GeocodeLibTest::testGeocode()
  238. *
  239. * @return void
  240. */
  241. public function testGeocode() {
  242. $address = '74523 Deutschland';
  243. //echo '<h2>'.$address.'</h2>';
  244. $is = $this->Geocode->geocode($address);
  245. //debug($is);
  246. $this->assertTrue($is);
  247. $is = $this->Geocode->getResult();
  248. //debug($is);
  249. $this->assertTrue(!empty($is));
  250. $is = $this->Geocode->error();
  251. //debug($is);
  252. $this->assertTrue(empty($is));
  253. $address = 'Leopoldstraße 100, München';
  254. //echo '<h2>'.$address.'</h2>';
  255. $is = $this->Geocode->geocode($address);
  256. //debug($is);
  257. $this->assertTrue($is);
  258. //pr($this->Geocode->debug());
  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 = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  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. }
  278. /**
  279. * GeocodeLibTest::testGeocodeBadApiKey()
  280. *
  281. * @return void
  282. */
  283. public function testGeocodeBadApiKey() {
  284. $address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  285. $result = $this->Geocode->geocode($address, array('sensor' => false, 'key' => 'testingBadApiKey'));
  286. $this->assertFalse($result);
  287. $result = $this->Geocode->error();
  288. $this->assertEquals('Error REQUEST_DENIED (The provided API key is invalid.)', $result);
  289. }
  290. /**
  291. * GeocodeLibTest::testGeocodeInvalid()
  292. *
  293. * @return void
  294. */
  295. public function testGeocodeInvalid() {
  296. $address = 'Hjfjosdfhosj, 78878 Mdfkufsdfk';
  297. $result = $this->Geocode->geocode($address);
  298. $this->assertFalse($result);
  299. $result = $this->Geocode->error();
  300. $this->assertTrue(!empty($result));
  301. }
  302. /**
  303. * GeocodeLibTest::testGetMaxAddress()
  304. *
  305. * @return void
  306. */
  307. public function testGetMaxAddress() {
  308. $ReflectionClass = new ReflectionClass('GeocodeLib');
  309. $Method = $ReflectionClass->getMethod('_getMaxAccuracy');
  310. $Method->setAccessible(true);
  311. $result = $Method->invoke($this->Geocode, array('street_address' => 'abc'));
  312. $this->assertSame(GeocodeLib::ACC_STREET, $result);
  313. $result = $Method->invoke($this->Geocode, array('intersection' => 'abc'));
  314. $this->assertSame(GeocodeLib::ACC_INTERSEC, $result);
  315. $result = $Method->invoke($this->Geocode, array('route' => 'abc'));
  316. $this->assertSame(GeocodeLib::ACC_ROUTE, $result);
  317. $result = $Method->invoke($this->Geocode, array('sublocality' => 'abc'));
  318. $this->assertSame(GeocodeLib::ACC_SUBLOC, $result);
  319. $result = $Method->invoke($this->Geocode, array('locality' => 'abc'));
  320. $this->assertSame(GeocodeLib::ACC_LOC, $result);
  321. $result = $Method->invoke($this->Geocode, array('postal_code' => 'abc'));
  322. $this->assertSame(GeocodeLib::ACC_POSTAL, $result);
  323. $result = $Method->invoke($this->Geocode, array('country' => 'abc'));
  324. $this->assertSame(GeocodeLib::ACC_COUNTRY, $result);
  325. $result = $Method->invoke($this->Geocode, array());
  326. $this->assertSame(null, $result);
  327. // mixed
  328. $result = $Method->invoke($this->Geocode, array(
  329. 'country' => 'aa',
  330. 'postal_code' => 'abc',
  331. 'locality' => '',
  332. 'street_address' => '',
  333. ));
  334. $this->assertSame(GeocodeLib::ACC_POSTAL, $result);
  335. }
  336. /**
  337. * GeocodeLibTest::testGeocodeMinAcc()
  338. *
  339. * @return void
  340. */
  341. public function testGeocodeMinAcc() {
  342. // address = postal_code, minimum = street level
  343. $address = 'Deutschland';
  344. $this->Geocode->setOptions(array('min_accuracy' => GeocodeLib::ACC_STREET));
  345. $is = $this->Geocode->geocode($address);
  346. $this->assertFalse($is);
  347. $is = $this->Geocode->error();
  348. $this->assertTrue(!empty($is));
  349. }
  350. /**
  351. * GeocodeLibTest::testTransformData()
  352. *
  353. * @return void
  354. */
  355. public function testTransformData() {
  356. $ReflectionClass = new ReflectionClass('GeocodeLib');
  357. $Method = $ReflectionClass->getMethod('_transformData');
  358. $Method->setAccessible(true);
  359. // non-full records
  360. $data = array('types' => array());
  361. $this->assertEquals($data, $Method->invoke($this->Geocode, $data));
  362. $data = array();
  363. $this->assertEquals($data, $Method->invoke($this->Geocode, $data));
  364. // Full record
  365. $ReflectionClass = new ReflectionClass('GeocodeLib');
  366. $Method = $ReflectionClass->getMethod('_transform');
  367. $Method->setAccessible(true);
  368. $data = json_decode($this->apiMockupReverseGeocode40206['raw'], true);
  369. $expected = array(
  370. 'results' => array(
  371. array(
  372. 'formatted_address' => 'Louisville, KY 40206, USA',
  373. // organized location components
  374. 'country' => 'United States',
  375. 'country_code' => 'US',
  376. 'country_province' => 'Kentucky',
  377. 'country_province_code' => 'KY',
  378. 'postal_code' => '40206',
  379. 'locality' => 'Louisville',
  380. 'sublocality' => '',
  381. 'route' => '',
  382. // vetted "types"
  383. 'types' => array(
  384. 'postal_code',
  385. ),
  386. // simple lat/lng
  387. 'lat' => 38.264357800000013,
  388. 'lng' => -85.699978899999991,
  389. 'location_type' => 'APPROXIMATE',
  390. 'viewport' => array(
  391. 'sw' => array(
  392. 'lat' => 38.239565800000001,
  393. 'lng' => -85.744800999999995,
  394. ),
  395. 'ne' => array(
  396. 'lat' => 38.285255800000002,
  397. 'lng' => -85.664309000000003,
  398. ),
  399. ),
  400. 'bounds' => array(
  401. 'sw' => array(
  402. 'lat' => 38.239565800000001,
  403. 'lng' => -85.744800999999995,
  404. ),
  405. 'ne' => array(
  406. 'lat' => 38.285255800000002,
  407. 'lng' => -85.664309000000003,
  408. ),
  409. ),
  410. 'address_components' => array(
  411. array(
  412. 'long_name' => '40206',
  413. 'short_name' => '40206',
  414. 'types' => array(
  415. 'postal_code',
  416. ),
  417. ),
  418. array(
  419. 'long_name' => 'Louisville',
  420. 'short_name' => 'Louisville',
  421. 'types' => array(
  422. 'locality',
  423. 'political',
  424. ),
  425. ),
  426. array(
  427. 'long_name' => 'Kentucky',
  428. 'short_name' => 'KY',
  429. 'types' => array(
  430. 'administrative_area_level_1',
  431. 'political',
  432. ),
  433. ),
  434. array(
  435. 'long_name' => 'United States',
  436. 'short_name' => 'US',
  437. 'types' => array(
  438. 'country',
  439. 'political',
  440. ),
  441. ),
  442. ),
  443. 'valid_type' => true,
  444. 'accuracy' => 4,
  445. 'accuracy_name' => 'postal_code',
  446. ),
  447. ),
  448. 'status' => 'OK',
  449. );
  450. $result = $Method->invoke($this->Geocode, $data);
  451. $this->assertEquals($expected, $result);
  452. }
  453. }