GeocodeLibTest.php 6.1 KB

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