3 * SPDX-License-Identifier: GPL-2.0-only
5 * This file is part of Nominatim. (https://nominatim.org)
7 * Copyright (C) 2022 by the Nominatim developer community.
8 * For a full list of authors see the git log.
13 require_once(CONST_LibDir.'/ParameterParser.php');
16 function userError($sError)
18 throw new \Exception($sError);
21 class ParameterParserTest extends \PHPUnit\Framework\TestCase
25 public function testGetBool()
27 $oParams = new ParameterParser(array(
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'));
45 public function testGetInt()
47 $oParams = new ParameterParser(array(
53 $this->assertSame(false, $oParams->getInt('non-exists'));
54 $this->assertSame(999, $oParams->getInt('non-exists', 999));
55 $this->assertSame(5, $oParams->getInt('int1'));
57 $this->assertSame(-1, $oParams->getInt('int2'));
58 $this->assertSame(0, $oParams->getInt('int3'));
62 public function testGetIntWithNonNumber()
64 $this->expectException(\Exception::class);
65 $this->expectExceptionMessage("Integer number expected for parameter 'int4'");
67 (new ParameterParser(array('int4' => 'a')))->getInt('int4');
71 public function testGetIntWithEmpytString()
73 $this->expectException(\Exception::class);
74 $this->expectExceptionMessage("Integer number expected for parameter 'int5'");
76 (new ParameterParser(array('int5' => '')))->getInt('int5');
80 public function testGetFloat()
83 $oParams = new ParameterParser(array(
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'));
96 public function testGetFloatWithEmptyString()
98 $this->expectException(\Exception::class);
99 $this->expectExceptionMessage("Floating-point number expected for parameter 'float4'");
101 (new ParameterParser(array('float4' => '')))->getFloat('float4');
104 public function testGetFloatWithTextString()
106 $this->expectException(\Exception::class);
107 $this->expectExceptionMessage("Floating-point number expected for parameter 'float5'");
109 (new ParameterParser(array('float5' => 'a')))->getFloat('float5');
113 public function testGetFloatWithInvalidNumber()
115 $this->expectException(\Exception::class);
116 $this->expectExceptionMessage("Floating-point number expected for parameter 'float6'");
118 (new ParameterParser(array('float6' => '-55.')))->getFloat('float6');
122 public function testGetString()
124 $oParams = new ParameterParser(array(
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
138 public function testGetSet()
140 $oParams = new ParameterParser(array(
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')));
150 $this->assertSame(false, $oParams->getSet('val2', array('foo', 'bar')));
151 $this->assertSame(0, $oParams->getSet('val3', array('foo', 'bar')));
155 public function testGetSetWithValueNotInSet()
157 $this->expectException(\Exception::class);
158 $this->expectExceptionMessage("Parameter 'val4' must be one of: foo, bar");
160 (new ParameterParser(array('val4' => 'faz')))->getSet('val4', array('foo', 'bar'));
164 public function testGetStringList()
166 $oParams = new ParameterParser(array(
167 'list1' => ',a,b,c,,c,d',
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'));
182 public function testGetPreferredLanguages()
184 $oParams = new ParameterParser(array('accept-language' => ''));
185 $this->assertSame(array(
186 'name:default' => 'name:default',
189 'official_name:default' => 'official_name:default',
190 'short_name:default' => 'short_name:default',
191 'official_name' => 'official_name',
192 'short_name' => 'short_name',
195 ), $oParams->getPreferredLanguages('default'));
197 $oParams = new ParameterParser(array('accept-language' => 'de,en'));
198 $this->assertSame(array(
199 'name:de' => 'name:de',
200 'name:en' => 'name:en',
203 'official_name:de' => 'official_name:de',
204 'short_name:de' => 'short_name:de',
205 'official_name:en' => 'official_name:en',
206 'short_name:en' => 'short_name:en',
207 'official_name' => 'official_name',
208 'short_name' => 'short_name',
211 ), $oParams->getPreferredLanguages('default'));
213 $oParams = new ParameterParser(array('accept-language' => 'fr-ca,fr;q=0.8,en-ca;q=0.5,en;q=0.3'));
214 $this->assertSame(array(
215 'name:fr-ca' => 'name:fr-ca',
216 'name:fr' => 'name:fr',
217 'name:en-ca' => 'name:en-ca',
218 'name:en' => 'name:en',
221 'official_name:fr-ca' => 'official_name:fr-ca',
222 'short_name:fr-ca' => 'short_name:fr-ca',
223 'official_name:fr' => 'official_name:fr',
224 'short_name:fr' => 'short_name:fr',
225 'official_name:en-ca' => 'official_name:en-ca',
226 'short_name:en-ca' => 'short_name:en-ca',
227 'official_name:en' => 'official_name:en',
228 'short_name:en' => 'short_name:en',
229 'official_name' => 'official_name',
230 'short_name' => 'short_name',
233 ), $oParams->getPreferredLanguages('default'));
235 $oParams = new ParameterParser(array('accept-language' => 'ja_rm,zh_pinyin'));
236 $this->assertSame(array(
237 'name:ja_rm' => 'name:ja_rm',
238 'name:zh_pinyin' => 'name:zh_pinyin',
239 'name:ja' => 'name:ja',
240 'name:zh' => 'name:zh',
243 'official_name:ja_rm' => 'official_name:ja_rm',
244 'short_name:ja_rm' => 'short_name:ja_rm',
245 'official_name:zh_pinyin' => 'official_name:zh_pinyin',
246 'short_name:zh_pinyin' => 'short_name:zh_pinyin',
247 'official_name:ja' => 'official_name:ja',
248 'short_name:ja' => 'short_name:ja',
249 'official_name:zh' => 'official_name:zh',
250 'short_name:zh' => 'short_name:zh',
251 'official_name' => 'official_name',
252 'short_name' => 'short_name',
255 ), $oParams->getPreferredLanguages('default'));
258 public function testHasSetAny()
260 $oParams = new ParameterParser(array(
265 'five' => 'anystring'
267 $this->assertFalse($oParams->hasSetAny(array()));
268 $this->assertFalse($oParams->hasSetAny(array('')));
269 $this->assertFalse($oParams->hasSetAny(array('unknown')));
270 $this->assertFalse($oParams->hasSetAny(array('one', 'two', 'three')));
271 $this->assertTrue($oParams->hasSetAny(array('one', 'four')));
272 $this->assertTrue($oParams->hasSetAny(array('four')));
273 $this->assertTrue($oParams->hasSetAny(array('five')));