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