]> git.openstreetmap.org Git - nominatim.git/blob - tests-php/Nominatim/NominatimTest.php
Merge pull request #533 from mtmail/PSR2-php-tests
[nominatim.git] / tests-php / Nominatim / NominatimTest.php
1 <?php
2
3 namespace Nominatim;
4
5 require '../lib/lib.php';
6
7 class NominatimTest extends \PHPUnit_Framework_TestCase
8 {
9
10
11     protected function setUp()
12     {
13     }
14
15
16     public function testGetClassTypesWithImportance()
17     {
18         $aClasses = getClassTypesWithImportance();
19
20         $this->assertGreaterThan(
21             200,
22             count($aClasses)
23         );
24
25         $this->assertEquals(
26             array(
27              'label' => "Country",
28              'frequency' => 0,
29              'icon' => "poi_boundary_administrative",
30              'defzoom' => 6,
31              'defdiameter' => 15,
32              'importance' => 3
33             ),
34             $aClasses['place:country']
35         );
36     }
37
38
39     public function testGetResultDiameter()
40     {
41         $aResult = array();
42         $this->assertEquals(
43             0.0001,
44             getResultDiameter($aResult)
45         );
46
47         $aResult = array('class' => 'place', 'type' => 'country');
48         $this->assertEquals(
49             15,
50             getResultDiameter($aResult)
51         );
52
53         $aResult = array('class' => 'boundary', 'type' => 'administrative', 'admin_level' => 6);
54         $this->assertEquals(
55             0.32,
56             getResultDiameter($aResult)
57         );
58     }
59
60
61     public function testAddQuotes()
62     {
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(''));
66     }
67
68
69     public function testLooksLikeLatLonPair()
70     {
71         // no coordinates expected
72         $this->assertNull(looksLikeLatLonPair(''));
73         $this->assertNull(looksLikeLatLonPair('abc'));
74         $this->assertNull(looksLikeLatLonPair('12 34'));
75         $this->assertNull(looksLikeLatLonPair('200.1 89.9')); // because latitude > 180
76
77         // coordinates expected
78         $this->assertNotNull(looksLikeLatLonPair('0.0 -0.0'));
79
80         $this->assertEquals(
81             array( 'lat' => 12.456, 'lon' => -78.90, 'query' => 'abc   def'),
82             looksLikeLatLonPair(' abc 12.456 -78.90 def ')
83         );
84
85         $this->assertEquals(
86             array( 'lat' => 12.456, 'lon' => -78.90, 'query' => ''),
87             looksLikeLatLonPair(' [12.456,-78.90] ')
88         );
89
90         // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
91         // these all represent the same location
92         $aQueries = array(
93                      '40 26.767 N 79 58.933 W',
94                      '40° 26.767′ N 79° 58.933′ W',
95                      "40° 26.767' N 79° 58.933' W",
96                      'N 40 26.767, W 79 58.933',
97                      'N 40°26.767′, W 79°58.933′',
98                      "N 40°26.767', W 79°58.933'",
99  
100                      '40 26 46 N 79 58 56 W',
101                      '40° 26′ 46″ N 79° 58′ 56″ W',
102                      'N 40 26 46 W 79 58 56',
103                      'N 40° 26′ 46″, W 79° 58′ 56″',
104                      'N 40° 26\' 46", W 79° 58\' 56"',
105  
106                      '40.446 -79.982',
107                      '40.446,-79.982',
108                      '40.446° N 79.982° W',
109                      'N 40.446° W 79.982°',
110  
111                      '[40.446 -79.982]',
112                      '       40.446  ,   -79.982     ',
113                     );
114
115
116         foreach ($aQueries as $sQuery) {
117             $aRes = looksLikeLatLonPair($sQuery);
118             $this->assertEquals(40.446, $aRes['lat'], 'degrees decimal ' . $sQuery, 0.01);
119             $this->assertEquals(-79.982, $aRes['lon'], 'degrees decimal ' . $sQuery, 0.01);
120         }
121     }
122
123
124
125     public function testGetWordSets()
126     {
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
131
132         function serializeSets($aSets)
133         {
134             $aParts = array();
135             foreach ($aSets as $aSet) {
136                 $aParts[] = '(' . join('|', $aSet) . ')';
137             }
138             return join(',', $aParts);
139         }
140
141         $this->assertEquals(
142             array(array('')),
143             getWordSets(array(), 0)
144         );
145
146         $this->assertEquals(
147             '(a)',
148             serializeSets(getWordSets(array("a"), 0))
149         );
150
151         $this->assertEquals(
152             '(a b),(a|b)',
153             serializeSets(getWordSets(array('a', 'b'), 0))
154         );
155
156         $this->assertEquals(
157             '(a b c),(a|b c),(a|b|c),(a b|c)',
158             serializeSets(getWordSets(array('a', 'b', 'c'), 0))
159         );
160
161         $this->assertEquals(
162             '(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)',
163             serializeSets(getWordSets(array('a', 'b', 'c', 'd'), 0))
164         );
165
166
167         // Inverse
168         $this->assertEquals(
169             '(a b c),(c|a b),(c|b|a),(b c|a)',
170             serializeSets(getInverseWordSets(array('a', 'b', 'c'), 0))
171         );
172
173
174         // make sure we don't create too many sets
175         // 4 words => 8 sets
176         // 10 words => 511 sets
177         // 15 words => 12911 sets
178         // 18 words => 65536 sets
179         // 20 words => 169766 sets
180         // 22 words => 401930 sets
181         // 28 words => 3505699 sets (needs more than 4GB via 'phpunit -d memory_limit=' to run)
182         $this->assertEquals(
183             8,
184             count(getWordSets(array_fill(0, 4, 'a'), 0))
185         );
186
187
188         $this->assertEquals(
189             65536,
190             count(getWordSets(array_fill(0, 18, 'a'), 0))
191         );
192     }
193
194
195     public function testCreatePointsAroundCenter()
196     {
197         // you might say we're creating a circle
198         $aPoints = createPointsAroundCenter(0, 0, 2);
199
200         $this->assertEquals(
201             101,
202             count($aPoints)
203         );
204         $this->assertEquals(
205             array(
206              ['', 0, 2],
207              ['', 0.12558103905863, 1.9960534568565],
208              ['', 0.25066646712861, 1.984229402629]
209             ),
210             array_splice($aPoints, 0, 3)
211         );
212     }
213
214
215     public function testGeometryText2Points()
216     {
217         $fRadius = 1;
218         // invalid value
219         $this->assertEquals(
220             null,
221             geometryText2Points('', $fRadius)
222         );
223
224         // POINT
225         $aPoints = geometryText2Points('POINT(10 20)', $fRadius);
226         $this->assertEquals(
227             101,
228             count($aPoints)
229         );
230         $this->assertEquals(
231             array(
232              [10, 21],
233              [10.062790519529, 20.998026728428],
234              [10.125333233564, 20.992114701314]
235             ),
236             array_splice($aPoints, 0, 3)
237         );
238
239         // POLYGON
240         $this->assertEquals(
241             array(
242              ['30', '10'],
243              ['40', '40'],
244              ['20', '40'],
245              ['10', '20'],
246              ['30', '10']
247             ),
248             geometryText2Points('POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))', $fRadius)
249         );
250
251         // MULTIPOLYGON
252         $this->assertEquals(
253             array(
254              ['30', '20'], // first polygon only
255              ['45', '40'],
256              ['10', '40'],
257              ['30', '20'],
258             ),
259             geometryText2Points('MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))', $fRadius)
260         );
261     }
262 }