GeocodeLibTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 function setUp() {
  15. parent::setUp();
  16. $this->Geocode = new GeocodeLib();
  17. }
  18. public function TearDown() {
  19. unset($this->Geocode);
  20. }
  21. public function testObject() {
  22. $this->assertTrue(is_object($this->Geocode));
  23. $this->assertInstanceOf('GeocodeLib', $this->Geocode);
  24. }
  25. public function testDistance() {
  26. $coords = array(
  27. 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),
  28. 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),
  29. array('name'=>'MUC/NewYork (--- road, ---h)', 'x'=>array('lat'=>48.1391, 'lng'=>11.5802), 'y'=>array('lat'=>40.700943, 'lng'=>-73.853531), 'd'=>6479)
  30. );
  31. foreach ($coords as $coord) {
  32. $is = $this->Geocode->distance($coord['x'], $coord['y']);
  33. //echo $coord['name'].':';
  34. //pr('is: '.$is.' - expected: '.$coord['d']);
  35. $this->assertEquals($coord['d'], $is);
  36. }
  37. }
  38. public function testBlur() {
  39. $coords = array(
  40. array(48.1391, 1, 0.002), //'y'=>array('lat'=>48.8934, 'lng'=>8.70492), 'd'=>228),
  41. array(11.5802, 1, 0.002),
  42. );
  43. foreach ($coords as $coord) {
  44. $is = $this->Geocode->blur($coord[0], $coord[1]);
  45. //pr('is: '.$is.' - expected: '.$coord[0].' +- '.$coord[2]);
  46. $this->assertWithinMargin($is, $coord[0], $coord[2]);
  47. $this->assertNotWithinMargin($is, $coord[0], $coord[2] / 4);
  48. }
  49. }
  50. public function testConvert() {
  51. $values = array(
  52. array(3, 'M', 'K', 4.828032),
  53. array(3, 'K', 'M', 1.86411358),
  54. array(100000, 'I', 'K', 2.54),
  55. );
  56. foreach ($values as $value) {
  57. $is = $this->Geocode->convert($value[0], $value[1], $value[2]);
  58. //echo $value[0].$value[1].' in '.$value[2].':';
  59. //pr('is: '.returns($is).' - expected: '.$value[3]);
  60. $this->assertEquals($value[3], round($is, 8));
  61. }
  62. }
  63. public function testUrl() {
  64. $is = $this->Geocode->url();
  65. //debug($is);
  66. $this->assertTrue(!empty($is) && strpos($is, 'http://maps.googleapis.com/maps/api/geocode/xml?') === 0);
  67. }
  68. // not possible with protected method
  69. public function _testFetch() {
  70. $url = 'http://maps.google.com/maps/api/geocode/xml?sensor=false&address=74523';
  71. $is = $this->Geocode->_fetch($url);
  72. //debug($is);
  73. $this->assertTrue(!empty($is) && substr($is, 0, 38) === '<?xml version="1.0" encoding="UTF-8"?>');
  74. $url = 'http://maps.google.com/maps/api/geocode/json?sensor=false&address=74523';
  75. $is = $this->Geocode->_fetch($url);
  76. //debug($is);
  77. $this->assertTrue(!empty($is) && substr($is, 0, 1) === '{');
  78. }
  79. public function testSetParams() {
  80. }
  81. public function testWithJson() {
  82. $this->Geocode->setOptions(array('output' => 'json'));
  83. $address = '74523 Deutschland';
  84. //echo '<h2>'.$address.'</h2>';
  85. $is = $this->Geocode->geocode($address);
  86. $this->assertTrue($is);
  87. $is = $this->Geocode->getResult();
  88. //debug($is);
  89. $this->assertTrue(!empty($is));
  90. }
  91. public function testSetOptions() {
  92. # should be the default
  93. $res = $this->Geocode->url();
  94. $this->assertTextContains('maps.googleapis.com', $res);
  95. $this->Geocode->setOptions(array('host'=>'maps.google.it'));
  96. # should now be ".it"
  97. $res = $this->Geocode->url();
  98. $this->assertTextContains('maps.google.it', $res);
  99. }
  100. public function testGeocode() {
  101. $address = '74523 Deutschland';
  102. //echo '<h2>'.$address.'</h2>';
  103. $is = $this->Geocode->geocode($address);
  104. //debug($is);
  105. $this->assertTrue($is);
  106. $is = $this->Geocode->getResult();
  107. //debug($is);
  108. $this->assertTrue(!empty($is));
  109. $is = $this->Geocode->error();
  110. //debug($is);
  111. $this->assertTrue(empty($is));
  112. $address = 'Leopoldstraße 100, München';
  113. //echo '<h2>'.$address.'</h2>';
  114. $is = $this->Geocode->geocode($address);
  115. //debug($is);
  116. $this->assertTrue($is);
  117. //pr($this->Geocode->debug());
  118. $is = $this->Geocode->getResult();
  119. //debug($is);
  120. $this->assertTrue(!empty($is));
  121. $is = $this->Geocode->error();
  122. //debug($is);
  123. $this->assertTrue(empty($is));
  124. $address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
  125. //echo '<h2>'.$address.'</h2>';
  126. $is = $this->Geocode->geocode($address);
  127. //debug($is);
  128. $this->assertTrue($is);
  129. //pr($this->Geocode->debug());
  130. $is = $this->Geocode->getResult();
  131. //debug($is);
  132. $this->assertTrue(!empty($is));
  133. $is = $this->Geocode->error();
  134. //debug($is);
  135. $this->assertTrue(empty($is));
  136. }
  137. public function testGeocodeInvalid() {
  138. $address = 'Hjfjosdfhosj, 78878 Mdfkufsdfk';
  139. //echo '<h2>'.$address.'</h2>';
  140. $is = $this->Geocode->geocode($address);
  141. //debug($is);
  142. $this->assertFalse($is);
  143. //pr($this->Geocode->debug());
  144. $is = $this->Geocode->error();
  145. //debug($is);
  146. $this->assertTrue(!empty($is));
  147. }
  148. public function testGeocodeMinAcc() {
  149. $address = 'Deutschland';
  150. //echo '<h2>'.$address.'</h2>';
  151. $this->Geocode->setOptions(array('min_accuracy'=>3));
  152. $is = $this->Geocode->geocode($address);
  153. //debug($is);
  154. $this->assertFalse($is);
  155. $is = $this->Geocode->error();
  156. //debug($is);
  157. $this->assertTrue(!empty($is));
  158. }
  159. public function testGeocodeInconclusive() {
  160. // seems like there is no inconclusive result anymore!!!
  161. $address = 'Neustadt';
  162. //echo '<h2>'.$address.'</h2>';
  163. # allow_inconclusive = TRUE
  164. $this->Geocode->setOptions(array('allow_inconclusive'=>true, 'min_accuracy' => GeocodeLib::ACC_LOC));
  165. $is = $this->Geocode->geocode($address);
  166. //echo 'debug:';
  167. //pr($this->Geocode->debug());
  168. //echo 'debug end';
  169. $this->assertTrue($is);
  170. $res = $this->Geocode->getResult();
  171. //pr($res);
  172. $this->assertTrue(count($res) > 4);
  173. $is = $this->Geocode->isInconclusive();
  174. $this->assertTrue($is);
  175. $this->Geocode->setOptions(array('allow_inconclusive'=>false));
  176. $is = $this->Geocode->geocode($address);
  177. $this->assertFalse($is);
  178. }
  179. public function testReverseGeocode() {
  180. $coords = array(
  181. array(-34.594445, -58.37446, 'Calle Florida 1134-1200, Buenos Aires'),
  182. array(48.8934, 8.70492, 'B294, 75175 Pforzheim, Deutschland')
  183. );
  184. foreach ($coords as $coord) {
  185. $is = $this->Geocode->reverseGeocode($coord[0], $coord[1]);
  186. $this->assertTrue($is);
  187. $is = $this->Geocode->getResult();
  188. $this->assertTrue(!empty($is));
  189. //debug($is);
  190. $address = isset($is[0]) ? $is[0]['formatted_address'] : $is['formatted_address'];
  191. $this->assertTextContains($coord[2], $address);
  192. }
  193. }
  194. public function testGetResult() {
  195. }
  196. }