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