4 require '../lib/lib.php';
 
   7 class NominatimTest extends \PHPUnit_Framework_TestCase
 
  10     protected function setUp()
 
  15     public function test_getClassTypesWithImportance()
 
  17         $aClasses = getClassTypesWithImportance();
 
  19         $this->assertGreaterThan(
 
  28                 'icon' => "poi_boundary_administrative",
 
  33             $aClasses['place:country']
 
  38     public function test_getResultDiameter()
 
  43             getResultDiameter($aResult)
 
  46         $aResult = array('class' => 'place', 'type' => 'country');
 
  49             getResultDiameter($aResult)
 
  52         $aResult = array('class' => 'boundary', 'type' => 'administrative', 'admin_level' => 6);
 
  55             getResultDiameter($aResult)
 
  60     public function test_addQuotes()
 
  62         // FIXME: not quoting existing quote signs is probably a bug
 
  63         $this->assertSame("'St. John's'", addQuotes("St. John's"));
 
  64         $this->assertSame("''", addQuotes(''));
 
  67     public function test_looksLikeLatLonPair()
 
  69         // no coordinates expected
 
  70         $this->assertNull(looksLikeLatLonPair(''));
 
  71         $this->assertNull(looksLikeLatLonPair('abc'));
 
  72         $this->assertNull(looksLikeLatLonPair('12 34'));
 
  73         $this->assertNull(looksLikeLatLonPair('200.1 89.9')); // because latitude > 180
 
  75         // coordinates expected
 
  76         $this->assertNotNull(looksLikeLatLonPair('0.0 -0.0'));
 
  79                 array( 'lat' => 12.456, 'lon' => -78.90, 'query' => 'abc   def'),
 
  80                 looksLikeLatLonPair(' abc 12.456 -78.90 def ')
 
  84                 array( 'lat' => 12.456, 'lon' => -78.90, 'query' => ''),
 
  85                 looksLikeLatLonPair(' [12.456,-78.90] ')
 
  88         // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
 
  89         // these all represent the same location
 
  91                     '40 26.767 N 79 58.933 W',
 
  92                     '40° 26.767′ N 79° 58.933′ W',
 
  93                     "40° 26.767' N 79° 58.933' W",
 
  94                     'N 40 26.767, W 79 58.933',
 
  95                     'N 40°26.767′, W 79°58.933′',
 
  96                     "N 40°26.767', W 79°58.933'",
 
  98                     '40 26 46 N 79 58 56 W',
 
  99                     '40° 26′ 46″ N 79° 58′ 56″ W',
 
 100                     'N 40 26 46 W 79 58 56',
 
 101                     'N 40° 26′ 46″, W 79° 58′ 56″',
 
 102                     'N 40° 26\' 46", W 79° 58\' 56"',
 
 106                     '40.446° N 79.982° W',
 
 107                     'N 40.446° W 79.982°',
 
 110                     '       40.446  ,   -79.982     ',
 
 114         foreach($aQueries as $sQuery){
 
 115             $aRes = looksLikeLatLonPair($sQuery);
 
 116             $this->assertEquals( 40.446, $aRes['lat'], 'degrees decimal ' . $sQuery, 0.01);
 
 117             $this->assertEquals(-79.982, $aRes['lon'], 'degrees decimal ' . $sQuery, 0.01);
 
 124     public function test_getWordSets()
 
 127         // given an array of arrays like
 
 128         // array( array('a','b'), array('c','d') )
 
 129         // returns a summary as string: '(a|b),(c|d)'
 
 130         function serialize_sets($aSets)
 
 133             foreach($aSets as $aSet){
 
 134                 $aParts[] = '(' . join('|', $aSet) . ')';
 
 136             return join(',', $aParts);
 
 141             getWordSets(array(),0)
 
 146             serialize_sets( getWordSets(array("a"),0) )
 
 151             serialize_sets( getWordSets(array('a','b'),0) )
 
 155             '(a b c),(a|b c),(a|b|c),(a b|c)',
 
 156             serialize_sets( getWordSets(array('a','b','c'),0) )
 
 160             '(a b c d),(a|b c d),(a|b|c d),(a|b|c|d),(a|b c|d),(a b|c d),(a b|c|d),(a b c|d)',
 
 161             serialize_sets( getWordSets(array('a','b','c','d'),0) )
 
 167             '(a b c),(c|a b),(c|b|a),(b c|a)',
 
 168             serialize_sets( getInverseWordSets(array('a','b','c'),0) )
 
 172         // make sure we don't create too many sets
 
 174         // 10 words => 511 sets
 
 175         // 15 words => 12911 sets
 
 176         // 18 words => 65536 sets
 
 177         // 20 words => 169766 sets
 
 178         // 22 words => 401930 sets
 
 179         // 28 words => 3505699 sets (needs more than 4GB via 'phpunit -d memory_limit=' to run)
 
 182             count( getWordSets(array_fill( 0, 4, 'a'),0) )
 
 188             count( getWordSets(array_fill( 0, 18, 'a'),0) )
 
 194     // you might say we're creating a circle
 
 195     public function test_createPointsAroundCenter()
 
 197         $aPoints = createPointsAroundCenter(0, 0, 2);
 
 206                 ['', 0.12558103905863, 1.9960534568565],
 
 207                 ['', 0.25066646712861, 1.984229402629]
 
 209             array_splice($aPoints, 0, 3)
 
 213     public function test_geometryText2Points()
 
 219             geometryText2Points('', $fRadius)
 
 223         $aPoints = geometryText2Points('POINT(10 20)', $fRadius);
 
 231                 [10.062790519529, 20.998026728428],
 
 232                 [10.125333233564, 20.992114701314]
 
 234             array_splice($aPoints, 0,3)
 
 246             geometryText2Points('POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))', $fRadius)
 
 252                 ['30', '20'], // first polygon only
 
 257             geometryText2Points('MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))', $fRadius)