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