geocode_lib.test.php 5.9 KB

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