]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/ParameterParserTest.php
Merge branch 'updatePHP' of https://github.com/ThomasBarris/Nominatim into ThomasBarr...
[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                            'short_name:default' => 'short_name:default',
179                            'name:default' => 'name:default',
180                            'short_name' => 'short_name',
181                            'name' => 'name',
182                            'brand' => 'brand',
183                            'official_name:default' => 'official_name:default',
184                            'official_name' => 'official_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                            'short_name:de' => 'short_name:de',
192                            'name:de' => 'name:de',
193                            'short_name:en' => 'short_name:en',
194                            'name:en' => 'name:en',
195                            'short_name' => 'short_name',
196                            'name' => 'name',
197                            'brand' => 'brand',
198                            'official_name:de' => 'official_name:de',
199                            'official_name:en' => 'official_name:en',
200                            'official_name' => 'official_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                            'short_name:fr-ca' => 'short_name:fr-ca',
208                            'name:fr-ca' => 'name:fr-ca',
209                            'short_name:fr' => 'short_name:fr',
210                            'name:fr' => 'name:fr',
211                            'short_name:en-ca' => 'short_name:en-ca',
212                            'name:en-ca' => 'name:en-ca',
213                            'short_name:en' => 'short_name:en',
214                            'name:en' => 'name:en',
215                            'short_name' => 'short_name',
216                            'name' => 'name',
217                            'brand' => 'brand',
218                            'official_name:fr-ca' => 'official_name:fr-ca',
219                            'official_name:fr' => 'official_name:fr',
220                            'official_name:en-ca' => 'official_name:en-ca',
221                            'official_name:en' => 'official_name:en',
222                            'official_name' => 'official_name',
223                            'ref' => 'ref',
224                            'type' => 'type',
225                           ), $oParams->getPreferredLanguages('default'));
226     }
227 }