GeocodeLibTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. public function testDistance() {
  71. $coords = array(
  72. 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),
  73. 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),
  74. array('name' => 'MUC/NewYork (--- road, ---h)', 'x' => array('lat' => 48.1391, 'lng' => 11.5802), 'y' => array('lat' => 40.700943, 'lng' => -73.853531), 'd' => 6479)
  75. );
  76. foreach ($coords as $coord) {
  77. $is = $this->Geocode->distance($coord['x'], $coord['y']);
  78. //echo $coord['name'].':';
  79. //pr('is: '.$is.' - expected: '.$coord['d']);
  80. $this->assertEquals($coord['d'], $is);
  81. }
  82. }
  83. public function testBlur() {
  84. $coords = array(
  85. array(48.1391, 1, 0.002), //'y'=>array('lat'=>48.8934, 'lng'=>8.70492), 'd'=>228),
  86. array(11.5802, 1, 0.002),
  87. );
  88. foreach ($coords as $coord) {
  89. $is = $this->Geocode->blur($coord[0], $coord[1]);
  90. //pr('is: '.$is.' - expected: '.$coord[0].' +- '.$coord[2]);
  91. $this->assertWithinMargin($is, $coord[0], $coord[2]);
  92. $this->assertNotWithinMargin($is, $coord[0], $coord[2] / 4);
  93. }
  94. }
  95. public function testConvert() {
  96. $values = array(
  97. array(3, 'M', 'K', 4.828032),
  98. array(3, 'K', 'M', 1.86411358),
  99. array(100000, 'I', 'K', 2.54),
  100. );
  101. foreach ($values as $value) {
  102. $is = $this->Geocode->convert($value[0], $value[1], $value[2]);
  103. //echo $value[0].$value[1].' in '.$value[2].':';
  104. //pr('is: '.returns($is).' - expected: '.$value[3]);
  105. $this->assertEquals($value[3], round($is, 8));
  106. }
  107. }
  108. public function testUrl() {
  109. $is = $this->Geocode->url();
  110. $this->assertFalse(empty($is));
  111. $this->assertPattern('#https://maps.googleapis.com/maps/api/geocode/(json|xml)\?.+#', $is);
  112. }
  113. // not possible with protected method
  114. public function _testFetch() {
  115. $url = 'http://maps.google.com/maps/api/geocode/xml?sensor=false&address=74523';
  116. $is = $this->Geocode->_fetch($url);
  117. //debug($is);
  118. $this->assertTrue(!empty($is) && substr($is, 0, 38) === '<?xml version="1.0" encoding="UTF-8"?>');
  119. $url = 'http://maps.google.com/maps/api/geocode/json?sensor=false&address=74523';
  120. $is = $this->Geocode->_fetch($url);
  121. //debug($is);
  122. $this->assertTrue(!empty($is) && substr($is, 0, 1) === '{');
  123. }
  124. public function testSetParams() {
  125. }
  126. public function testWithJson() {
  127. $this->Geocode->setOptions(array('output' => 'json'));
  128. $address = '74523 Deutschland';
  129. //echo '<h2>'.$address.'</h2>';
  130. $is = $this->Geocode->geocode($address);
  131. $this->assertTrue($is);
  132. $is = $this->Geocode->getResult();
  133. //debug($is);
  134. $this->assertTrue(!empty($is));
  135. }
  136. public function testSetOptions() {
  137. // should be the default
  138. $res = $this->Geocode->url();
  139. $this->assertTextContains('maps.googleapis.com', $res);
  140. $this->Geocode->setOptions(array('host' => 'maps.google.it'));
  141. // should now be ".it"
  142. $res = $this->Geocode->url();
  143. $this->assertTextContains('maps.google.it', $res);
  144. }
  145. public function testGeocode() {
  146. $address = '74523 Deutschland';
  147. //echo '<h2>'.$address.'</h2>';
  148. $is = $this->Geocode->geocode($address);
  149. //debug($is);
  150. $this->assertTrue($is);
  151. $is = $this->Geocode->getResult();
  152. //debug($is);
  153. $this->assertTrue(!empty($is));
  154. $is = $this->Geocode->error();
  155. //debug($is);
  156. $this->assertTrue(empty($is));
  157. $address = 'Leopoldstraße 100, München';
  158. //echo '<h2>'.$address.'</h2>';
  159. $is = $this->Geocode->geocode($address);
  160. //debug($is);
  161. $this->assertTrue($is);
  162. //pr($this->Geocode->debug());
  163. $is = $this->Geocode->getResult();
  164. //debug($is);
  165. $this->assertTrue(!empty($is));
  166. $is = $this->Geocode->error();
  167. //debug($is);
  168. $this->assertTrue(empty($is));
  169. $address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  170. //echo '<h2>'.$address.'</h2>';
  171. $is = $this->Geocode->geocode($address);
  172. //debug($is);
  173. $this->assertTrue($is);
  174. //pr($this->Geocode->debug());
  175. $is = $this->Geocode->getResult();
  176. //debug($is);
  177. $this->assertTrue(!empty($is));
  178. $is = $this->Geocode->error();
  179. //debug($is);
  180. $this->assertTrue(empty($is));
  181. }
  182. public function testGeocodeBadApiKey() {
  183. $address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  184. $is = $this->Geocode->geocode($address, array('sensor' => false, 'key' => 'testingBadApiKey'));
  185. $this->assertFalse($is);
  186. //pr($this->Geocode->debug());
  187. $is = $this->Geocode->error();
  188. $this->assertEqual('Error REQUEST_DENIED (The provided API key is invalid.)', $is);
  189. }
  190. public function testGeocodeInvalid() {
  191. $address = 'Hjfjosdfhosj, 78878 Mdfkufsdfk';
  192. //echo '<h2>'.$address.'</h2>';
  193. $is = $this->Geocode->geocode($address);
  194. //debug($is);
  195. $this->assertFalse($is);
  196. //pr($this->Geocode->debug());
  197. $is = $this->Geocode->error();
  198. //debug($is);
  199. $this->assertTrue(!empty($is));
  200. }
  201. public function testGetMaxAddress() {
  202. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('street_address' => 'abc')), GeocodeLib::ACC_STREET);
  203. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('intersection' => 'abc')), GeocodeLib::ACC_INTERSEC);
  204. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('route' => 'abc')), GeocodeLib::ACC_ROUTE);
  205. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('sublocality' => 'abc')), GeocodeLib::ACC_SUBLOC);
  206. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('locality' => 'abc')), GeocodeLib::ACC_LOC);
  207. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('postal_code' => 'abc')), GeocodeLib::ACC_POSTAL);
  208. $this->assertEqual($this->Geocode->_getMaxAccuracy(array('country' => 'aa')), GeocodeLib::ACC_COUNTRY);
  209. $this->assertEqual($this->Geocode->_getMaxAccuracy(array()), GeocodeLib::ACC_COUNTRY);
  210. // mixed
  211. $this->assertEqual($this->Geocode->_getMaxAccuracy(array(
  212. 'country' => 'aa',
  213. 'postal_code' => 'abc',
  214. 'locality' => '',
  215. 'street_address' => '',
  216. )), GeocodeLib::ACC_POSTAL);
  217. }
  218. public function testGeocodeMinAcc() {
  219. // address = postal_code, minimum = street level
  220. $address = 'Deutschland';
  221. $this->Geocode->setOptions(array('min_accuracy' => GeocodeLib::ACC_STREET));
  222. $is = $this->Geocode->geocode($address);
  223. $this->assertFalse($is);
  224. $is = $this->Geocode->error();
  225. $this->assertTrue(!empty($is));
  226. }
  227. public function testGeocodeInconclusive() {
  228. // seems like there is no inconclusive result anymore!!!
  229. $address = 'Neustadt';
  230. // allow_inconclusive = TRUE
  231. $this->Geocode->setOptions(array('allow_inconclusive' => true, 'min_accuracy' => GeocodeLib::ACC_POSTAL));
  232. $is = $this->Geocode->geocode($address);
  233. $this->assertTrue($is);
  234. $res = $this->Geocode->getResult();
  235. $this->assertTrue(count($res) > 4);
  236. $is = $this->Geocode->isInconclusive();
  237. $this->assertTrue($is);
  238. $this->Geocode->setOptions(array('allow_inconclusive' => false));
  239. $is = $this->Geocode->geocode($address);
  240. $this->assertFalse($is);
  241. }
  242. public function testReverseGeocode() {
  243. $coords = array(
  244. array(-34.594445, -58.37446, 'Calle Florida 1134-1200, Buenos Aires'),
  245. array(48.8934, 8.70492, 'B294, 75175 Pforzheim, Deutschland')
  246. );
  247. foreach ($coords as $coord) {
  248. $is = $this->Geocode->reverseGeocode($coord[0], $coord[1]);
  249. $this->assertTrue($is);
  250. $is = $this->Geocode->getResult();
  251. $this->assertTrue(!empty($is));
  252. //debug($is);
  253. $address = isset($is[0]) ? $is[0]['formatted_address'] : $is['formatted_address'];
  254. $this->assertTextContains($coord[2], $address);
  255. }
  256. }
  257. public function test_transformData() {
  258. // non-full records
  259. $data = array('record' => 'OK');
  260. $this->assertEqual($this->Geocode->_transformData($data), $data);
  261. $data = array();
  262. $this->assertEqual($this->Geocode->_transformData($data), $data);
  263. $data = '';
  264. $this->assertEqual($this->Geocode->_transformData($data), $data);
  265. $data = 'abc';
  266. $this->assertEqual($this->Geocode->_transformData($data), $data);
  267. // full record
  268. $expect = array(
  269. 'results' => array(
  270. 0 => array (
  271. 'formatted_address' => 'Louisville, KY 40206, USA',
  272. // organized location components
  273. 'country' => 'United States',
  274. 'country_code' => 'US',
  275. 'country_province' => 'Kentucky',
  276. 'country_province_code' => 'KY',
  277. 'postal_code' => '40206',
  278. 'locality' => 'Louisville',
  279. 'sublocality' => '',
  280. 'route' => '',
  281. // vetted "types"
  282. 'types' => array (
  283. 0 => 'postal_code',
  284. ),
  285. // simple lat/lng
  286. 'lat' => 38.264357800000013,
  287. 'lng' => -85.699978899999991,
  288. 'location_type' => 'APPROXIMATE',
  289. 'viewport' => array (
  290. 'sw' => array (
  291. 'lat' => 38.239565800000001,
  292. 'lng' => -85.744800999999995,
  293. ),
  294. 'ne' => array (
  295. 'lat' => 38.285255800000002,
  296. 'lng' => -85.664309000000003,
  297. ),
  298. ),
  299. 'bounds' => array (
  300. 'sw' => array (
  301. 'lat' => 38.239565800000001,
  302. 'lng' => -85.744800999999995,
  303. ),
  304. 'ne' => array (
  305. 'lat' => 38.285255800000002,
  306. 'lng' => -85.664309000000003,
  307. ),
  308. ),
  309. // injected static maxAccuracy
  310. 'maxAccuracy' => 5,
  311. ),
  312. ),
  313. 'status' => 'OK',
  314. );
  315. $data = json_decode($this->apiMockupReverseGeocode40206['raw'], true);
  316. $this->assertEqual($this->Geocode->_transformData($data), $expect);
  317. // multiple full records
  318. // TODO:...
  319. }
  320. public function testGetResult() {
  321. }
  322. }