]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/ParameterParserTest.php
PHP 8 behaves slightly different with in_array and usort
[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         $this->expectException(\Exception::class);
141         $this->expectExceptionMessage("Parameter 'val3' must be one of: foo, bar");
142
143         $oParams = new ParameterParser(array(
144                                         'val1' => 'foo',
145                                         'val2' => '',
146                                         'val3' => 0
147                                        ));
148
149         $this->assertSame(false, $oParams->getSet('non-exists', array('foo', 'bar')));
150         $this->assertSame('default', $oParams->getSet('non-exists', array('foo', 'bar'), 'default'));
151         $this->assertSame('foo', $oParams->getSet('val1', array('foo', 'bar')));
152
153         $this->assertSame(false, $oParams->getSet('val2', array('foo', 'bar')));
154         $oParams->getSet('val3', array('foo', 'bar'));
155     }
156
157
158     public function testGetSetWithValueNotInSet()
159     {
160         $this->expectException(\Exception::class);
161         $this->expectExceptionMessage("Parameter 'val4' must be one of: foo, bar");
162
163         (new ParameterParser(array('val4' => 'faz')))->getSet('val4', array('foo', 'bar'));
164     }
165
166
167     public function testGetStringList()
168     {
169         $oParams = new ParameterParser(array(
170                                         'list1' => ',a,b,c,,c,d',
171                                         'list2' => 'a',
172                                         'list3' => '',
173                                         'list4' => '0'
174                                        ));
175
176         $this->assertSame(false, $oParams->getStringList('non-exists'));
177         $this->assertSame(array('a', 'b'), $oParams->getStringList('non-exists', array('a', 'b')));
178         $this->assertSame(array('a', 'b', 'c', 'c', 'd'), $oParams->getStringList('list1'));
179         $this->assertSame(array('a'), $oParams->getStringList('list2'));
180         $this->assertSame(false, $oParams->getStringList('list3'));
181         $this->assertSame(false, $oParams->getStringList('list4'));
182     }
183
184
185     public function testGetPreferredLanguages()
186     {
187         $oParams = new ParameterParser(array('accept-language' => ''));
188         $this->assertSame(array(
189                            'name:default' => 'name:default',
190                            '_place_name:default' => '_place_name:default',
191                            'name' => 'name',
192                            '_place_name' => '_place_name'
193                           ), array_slice($oParams->getPreferredLanguages('default'), 0, 4));
194
195         $oParams = new ParameterParser(array('accept-language' => 'de,en'));
196         $this->assertSame(array(
197                            'name:de' => 'name:de',
198                            '_place_name:de' => '_place_name:de',
199                            'name:en' => 'name:en',
200                            '_place_name:en' => '_place_name:en',
201                            'name' => 'name',
202                            '_place_name' => '_place_name'
203                           ), array_slice($oParams->getPreferredLanguages('default'), 0, 6));
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                            '_place_name:fr-ca' => '_place_name:fr-ca',
209                            'name:fr' => 'name:fr',
210                            '_place_name:fr' => '_place_name:fr',
211                            'name:en-ca' => 'name:en-ca',
212                            '_place_name:en-ca' => '_place_name:en-ca',
213                            'name:en' => 'name:en',
214                            '_place_name:en' => '_place_name:en',
215                            'name' => 'name',
216                            '_place_name' => '_place_name'
217                           ), array_slice($oParams->getPreferredLanguages('default'), 0, 10));
218
219         $oParams = new ParameterParser(array('accept-language' => 'ja_rm,zh_pinyin'));
220         $this->assertSame(array(
221                            'name:ja_rm' => 'name:ja_rm',
222                            '_place_name:ja_rm' => '_place_name:ja_rm',
223                            'name:zh_pinyin' => 'name:zh_pinyin',
224                            '_place_name:zh_pinyin' => '_place_name:zh_pinyin',
225                            'name:ja' => 'name:ja',
226                            '_place_name:ja' => '_place_name:ja',
227                            'name:zh' => 'name:zh',
228                            '_place_name:zh' => '_place_name:zh',
229                            'name' => 'name',
230                            '_place_name' => '_place_name'
231                           ), array_slice($oParams->getPreferredLanguages('default'), 0, 10));
232     }
233
234     public function testHasSetAny()
235     {
236         $oParams = new ParameterParser(array(
237                                         'one' => '',
238                                         'two' => 0,
239                                         'three' => '0',
240                                         'four' => '1',
241                                         'five' => 'anystring'
242         ));
243         $this->assertFalse($oParams->hasSetAny(array()));
244         $this->assertFalse($oParams->hasSetAny(array('')));
245         $this->assertFalse($oParams->hasSetAny(array('unknown')));
246         $this->assertFalse($oParams->hasSetAny(array('one', 'two', 'three')));
247         $this->assertTrue($oParams->hasSetAny(array('one', 'four')));
248         $this->assertTrue($oParams->hasSetAny(array('four')));
249         $this->assertTrue($oParams->hasSetAny(array('five')));
250     }
251 }