]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/ParameterParserTest.php
Merge branch 'fix-getcol-zero' of https://github.com/eyusupov/Nominatim into eyusupov...
[nominatim.git] / test / php / Nominatim / ParameterParserTest.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_BasePath.'/lib/ParameterParser.php');
6
7
8 function userError($sError)
9 {
10     throw new \Exception($sError);
11 }
12
13 class ParameterParserTest extends \PHPUnit\Framework\TestCase
14 {
15
16
17     public function testGetBool()
18     {
19         $oParams = new ParameterParser(array(
20                                         'bool1' => '1',
21                                         'bool2' => '0',
22                                         'bool3' => 'true',
23                                         'bool4' => 'false',
24                                         'bool5' => ''
25                                        ));
26
27         $this->assertSame(false, $oParams->getBool('non-exists'));
28         $this->assertSame(true, $oParams->getBool('non-exists', true));
29         $this->assertSame(true, $oParams->getBool('bool1'));
30         $this->assertSame(false, $oParams->getBool('bool2'));
31         $this->assertSame(true, $oParams->getBool('bool3'));
32         $this->assertSame(true, $oParams->getBool('bool4'));
33         $this->assertSame(false, $oParams->getBool('bool5'));
34     }
35
36
37     public function testGetInt()
38     {
39         $oParams = new ParameterParser(array(
40                                         'int1' => '5',
41                                         'int2' => '-1',
42                                         'int3' => 0
43                                        ));
44
45         $this->assertSame(false, $oParams->getInt('non-exists'));
46         $this->assertSame(999, $oParams->getInt('non-exists', 999));
47         $this->assertSame(5, $oParams->getInt('int1'));
48
49         $this->assertSame(-1, $oParams->getInt('int2'));
50         $this->assertSame(0, $oParams->getInt('int3'));
51     }
52
53
54     public function testGetIntWithNonNumber()
55     {
56         $this->expectException(\Exception::class);
57         $this->expectExceptionMessage("Integer number expected for parameter 'int4'");
58
59         (new ParameterParser(array('int4' => 'a')))->getInt('int4');
60     }
61
62
63     public function testGetIntWithEmpytString()
64     {
65         $this->expectException(\Exception::class);
66         $this->expectExceptionMessage("Integer number expected for parameter 'int5'");
67
68         (new ParameterParser(array('int5' => '')))->getInt('int5');
69     }
70
71
72     public function testGetFloat()
73     {
74
75         $oParams = new ParameterParser(array(
76                                         'float1' => '1.0',
77                                         'float2' => '-5',
78                                         'float3' => 0
79                                        ));
80
81         $this->assertSame(false, $oParams->getFloat('non-exists'));
82         $this->assertSame(999, $oParams->getFloat('non-exists', 999));
83         $this->assertSame(1.0, $oParams->getFloat('float1'));
84         $this->assertSame(-5.0, $oParams->getFloat('float2'));
85         $this->assertSame(0.0, $oParams->getFloat('float3'));
86     }
87
88     public function testGetFloatWithEmptyString()
89     {
90         $this->expectException(\Exception::class);
91         $this->expectExceptionMessage("Floating-point number expected for parameter 'float4'");
92
93         (new ParameterParser(array('float4' => '')))->getFloat('float4');
94     }
95
96     public function testGetFloatWithTextString()
97     {
98         $this->expectException(\Exception::class);
99         $this->expectExceptionMessage("Floating-point number expected for parameter 'float5'");
100
101         (new ParameterParser(array('float5' => 'a')))->getFloat('float5');
102     }
103
104
105     public function testGetFloatWithInvalidNumber()
106     {
107         $this->expectException(\Exception::class);
108         $this->expectExceptionMessage("Floating-point number expected for parameter 'float6'");
109
110         (new ParameterParser(array('float6' => '-55.')))->getFloat('float6');
111     }
112
113
114     public function testGetString()
115     {
116         $oParams = new ParameterParser(array(
117                                         'str1' => 'abc',
118                                         'str2' => '',
119                                         'str3' => '0'
120                                        ));
121
122         $this->assertSame(false, $oParams->getString('non-exists'));
123         $this->assertSame('default', $oParams->getString('non-exists', 'default'));
124         $this->assertSame('abc', $oParams->getString('str1'));
125         $this->assertSame(false, $oParams->getStringList('str2'));
126         $this->assertSame(false, $oParams->getStringList('str3')); // sadly PHP magic treats 0 as false when returned
127     }
128
129
130     public function testGetSet()
131     {
132         $oParams = new ParameterParser(array(
133                                         'val1' => 'foo',
134                                         'val2' => '',
135                                         'val3' => 0
136                                        ));
137
138         $this->assertSame(false, $oParams->getSet('non-exists', array('foo', 'bar')));
139         $this->assertSame('default', $oParams->getSet('non-exists', array('foo', 'bar'), 'default'));
140         $this->assertSame('foo', $oParams->getSet('val1', array('foo', 'bar')));
141
142         $this->assertSame(false, $oParams->getSet('val2', array('foo', 'bar')));
143         $this->assertSame(0, $oParams->getSet('val3', array('foo', 'bar')));
144     }
145
146
147     public function testGetSetWithValueNotInSet()
148     {
149         $this->expectException(\Exception::class);
150         $this->expectExceptionMessage("Parameter 'val4' must be one of: foo, bar");
151
152         (new ParameterParser(array('val4' => 'faz')))->getSet('val4', array('foo', 'bar'));
153     }
154
155
156     public function testGetStringList()
157     {
158         $oParams = new ParameterParser(array(
159                                         'list1' => ',a,b,c,,c,d',
160                                         'list2' => 'a',
161                                         'list3' => '',
162                                         'list4' => '0'
163                                        ));
164
165         $this->assertSame(false, $oParams->getStringList('non-exists'));
166         $this->assertSame(array('a', 'b'), $oParams->getStringList('non-exists', array('a', 'b')));
167         $this->assertSame(array('a', 'b', 'c', 'c', 'd'), $oParams->getStringList('list1'));
168         $this->assertSame(array('a'), $oParams->getStringList('list2'));
169         $this->assertSame(false, $oParams->getStringList('list3'));
170         $this->assertSame(false, $oParams->getStringList('list4'));
171     }
172
173
174     public function testGetPreferredLanguages()
175     {
176         $oParams = new ParameterParser(array('accept-language' => ''));
177         $this->assertSame(array(
178                            'name:default' => 'name:default',
179                            'name' => 'name',
180                            'brand' => 'brand',
181                            'official_name:default' => 'official_name:default',
182                            'short_name:default' => 'short_name:default',
183                            'official_name' => 'official_name',
184                            'short_name' => 'short_name',
185                            'ref' => 'ref',
186                            'type' => 'type'
187                           ), $oParams->getPreferredLanguages('default'));
188
189         $oParams = new ParameterParser(array('accept-language' => 'de,en'));
190         $this->assertSame(array(
191                            'name:de' => 'name:de',
192                            'name:en' => 'name:en',
193                            'name' => 'name',
194                            'brand' => 'brand',
195                            'official_name:de' => 'official_name:de',
196                            'short_name:de' => 'short_name:de',
197                            'official_name:en' => 'official_name:en',
198                            'short_name:en' => 'short_name:en',
199                            'official_name' => 'official_name',
200                            'short_name' => 'short_name',
201                            'ref' => 'ref',
202                            'type' => 'type'
203                           ), $oParams->getPreferredLanguages('default'));
204
205         $oParams = new ParameterParser(array('accept-language' => 'fr-ca,fr;q=0.8,en-ca;q=0.5,en;q=0.3'));
206         $this->assertSame(array(
207                            'name:fr-ca' => 'name:fr-ca',
208                            'name:fr' => 'name:fr',
209                            'name:en-ca' => 'name:en-ca',
210                            'name:en' => 'name:en',
211                            'name' => 'name',
212                            'brand' => 'brand',
213                            'official_name:fr-ca' => 'official_name:fr-ca',
214                            'short_name:fr-ca' => 'short_name:fr-ca',
215                            'official_name:fr' => 'official_name:fr',
216                            'short_name:fr' => 'short_name:fr',
217                            'official_name:en-ca' => 'official_name:en-ca',
218                            'short_name:en-ca' => 'short_name:en-ca',
219                            'official_name:en' => 'official_name:en',
220                            'short_name:en' => 'short_name:en',
221                            'official_name' => 'official_name',
222                            'short_name' => 'short_name',
223                            'ref' => 'ref',
224                            'type' => 'type',
225                           ), $oParams->getPreferredLanguages('default'));
226
227         $oParams = new ParameterParser(array('accept-language' => 'ja_rm,zh_pinyin'));
228         $this->assertSame(array(
229                            'name:ja_rm' => 'name:ja_rm',
230                            'name:zh_pinyin' => 'name:zh_pinyin',
231                            'name:ja' => 'name:ja',
232                            'name:zh' => 'name:zh',
233                            'name' => 'name',
234                            'brand' => 'brand',
235                            'official_name:ja_rm' => 'official_name:ja_rm',
236                            'short_name:ja_rm' => 'short_name:ja_rm',
237                            'official_name:zh_pinyin' => 'official_name:zh_pinyin',
238                            'short_name:zh_pinyin' => 'short_name:zh_pinyin',
239                            'official_name:ja' => 'official_name:ja',
240                            'short_name:ja' => 'short_name:ja',
241                            'official_name:zh' => 'official_name:zh',
242                            'short_name:zh' => 'short_name:zh',
243                            'official_name' => 'official_name',
244                            'short_name' => 'short_name',
245                            'ref' => 'ref',
246                            'type' => 'type',
247                           ), $oParams->getPreferredLanguages('default'));
248     }
249 }