5 require_once '../../lib/lib.php';
 
   7 class NominatimTest extends \PHPUnit_Framework_TestCase
 
  11     protected function setUp()
 
  16     public function testGetClassTypesWithImportance()
 
  18         $aClasses = getClassTypesWithImportance();
 
  20         $this->assertGreaterThan(
 
  29              'icon' => "poi_boundary_administrative",
 
  34             $aClasses['place:country']
 
  39     public function testGetResultDiameter()
 
  44             getResultDiameter($aResult)
 
  47         $aResult = array('class' => 'place', 'type' => 'country');
 
  50             getResultDiameter($aResult)
 
  53         $aResult = array('class' => 'boundary', 'type' => 'administrative', 'admin_level' => 6);
 
  56             getResultDiameter($aResult)
 
  61     public function testAddQuotes()
 
  63         // FIXME: not quoting existing quote signs is probably a bug
 
  64         $this->assertSame("'St. John's'", addQuotes("St. John's"));
 
  65         $this->assertSame("''", addQuotes(''));
 
  69     public function testGetWordSets()
 
  71         // given an array of arrays like
 
  72         // array( array('a','b'), array('c','d') )
 
  73         // returns a summary as string: '(a|b),(c|d)'
 
  76         function serializeSets($aSets)
 
  79             foreach ($aSets as $aSet) {
 
  80                 $aParts[] = '(' . join('|', $aSet) . ')';
 
  82             return join(',', $aParts);
 
  87             getWordSets(array(), 0)
 
  92             serializeSets(getWordSets(array("a"), 0))
 
  97             serializeSets(getWordSets(array('a', 'b'), 0))
 
 101             '(a b c),(a|b c),(a|b|c),(a b|c)',
 
 102             serializeSets(getWordSets(array('a', 'b', 'c'), 0))
 
 106             '(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)',
 
 107             serializeSets(getWordSets(array('a', 'b', 'c', 'd'), 0))
 
 113             '(a b c),(c|a b),(c|b|a),(b c|a)',
 
 114             serializeSets(getInverseWordSets(array('a', 'b', 'c'), 0))
 
 118         // make sure we don't create too many sets
 
 120         // 10 words => 511 sets
 
 121         // 15 words => 12911 sets
 
 122         // 18 words => 65536 sets
 
 123         // 20 words => 169766 sets
 
 124         // 22 words => 401930 sets
 
 125         // 28 words => 3505699 sets (needs more than 4GB via 'phpunit -d memory_limit=' to run)
 
 128             count(getWordSets(array_fill(0, 4, 'a'), 0))
 
 134             count(getWordSets(array_fill(0, 18, 'a'), 0))
 
 139     public function testCreatePointsAroundCenter()
 
 141         // you might say we're creating a circle
 
 142         $aPoints = createPointsAroundCenter(0, 0, 2);
 
 151              ['', 0.12558103905863, 1.9960534568565],
 
 152              ['', 0.25066646712861, 1.984229402629]
 
 154             array_splice($aPoints, 0, 3)
 
 159     public function testGeometryText2Points()
 
 165             geometryText2Points('', $fRadius)
 
 169         $aPoints = geometryText2Points('POINT(10 20)', $fRadius);
 
 177              [10.062790519529, 20.998026728428],
 
 178              [10.125333233564, 20.992114701314]
 
 180             array_splice($aPoints, 0, 3)
 
 192             geometryText2Points('POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))', $fRadius)
 
 198              ['30', '20'], // first polygon only
 
 203             geometryText2Points('MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))', $fRadius)
 
 207     public function testParseLatLon()
 
 209         // no coordinates expected
 
 210         $this->assertFalse(parseLatLon(''));
 
 211         $this->assertFalse(parseLatLon('abc'));
 
 212         $this->assertFalse(parseLatLon('12 34'));
 
 214         // coordinates expected
 
 215         $this->assertNotNull(parseLatLon('0.0 -0.0'));
 
 217         $aRes = parseLatLon(' abc 12.456 -78.90 def ');
 
 218         $this->assertEquals($aRes[1], 12.456);
 
 219         $this->assertEquals($aRes[2], -78.90);
 
 220         $this->assertEquals($aRes[0], ' 12.456 -78.90 ');
 
 222         $aRes = parseLatLon(' [12.456,-78.90] ');
 
 223         $this->assertEquals($aRes[1], 12.456);
 
 224         $this->assertEquals($aRes[2], -78.90);
 
 225         $this->assertEquals($aRes[0], ' [12.456,-78.90] ');
 
 227         $aRes = parseLatLon(' -12.456,-78.90 ');
 
 228         $this->assertEquals($aRes[1], -12.456);
 
 229         $this->assertEquals($aRes[2], -78.90);
 
 230         $this->assertEquals($aRes[0], ' -12.456,-78.90 ');
 
 232         // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
 
 233         // these all represent the same location
 
 235                      '40 26.767 N 79 58.933 W',
 
 236                      '40° 26.767′ N 79° 58.933′ W',
 
 237                      "40° 26.767' N 79° 58.933' W",
 
 238                      'N 40 26.767, W 79 58.933',
 
 239                      'N 40°26.767′, W 79°58.933′',
 
 240                      "N 40°26.767', W 79°58.933'",
 
 242                      '40 26 46 N 79 58 56 W',
 
 243                      '40° 26′ 46″ N 79° 58′ 56″ W',
 
 244                      'N 40 26 46 W 79 58 56',
 
 245                      'N 40° 26′ 46″, W 79° 58′ 56″',
 
 246                      'N 40° 26\' 46", W 79° 58\' 56"',
 
 250                      '40.446° N 79.982° W',
 
 251                      'N 40.446° W 79.982°',
 
 254                      '       40.446  ,   -79.982     ',
 
 258         foreach ($aQueries as $sQuery) {
 
 259             $aRes = parseLatLon($sQuery);
 
 260             $this->assertEquals(40.446, $aRes[1], 'degrees decimal ' . $sQuery, 0.01);
 
 261             $this->assertEquals(-79.982, $aRes[2], 'degrees decimal ' . $sQuery, 0.01);
 
 262             $this->assertEquals($sQuery, $aRes[0]);