5 require_once(CONST_LibDir.'/ParameterParser.php');
8 function userError($sError)
10 throw new \Exception($sError);
13 class ParameterParserTest extends \PHPUnit\Framework\TestCase
17 public function testGetBool()
19 $oParams = new ParameterParser(array(
27 $this->assertSame(false, $oParams->getBool('non-exists'));
28 $this->assertSame(true, $oParams->getBool('non-exists', true));
29 $this->assertSame(true, $oParams->getBool('bool1'));
30 $this->assertSame(false, $oParams->getBool('bool2'));
31 $this->assertSame(true, $oParams->getBool('bool3'));
32 $this->assertSame(true, $oParams->getBool('bool4'));
33 $this->assertSame(false, $oParams->getBool('bool5'));
37 public function testGetInt()
39 $oParams = new ParameterParser(array(
45 $this->assertSame(false, $oParams->getInt('non-exists'));
46 $this->assertSame(999, $oParams->getInt('non-exists', 999));
47 $this->assertSame(5, $oParams->getInt('int1'));
49 $this->assertSame(-1, $oParams->getInt('int2'));
50 $this->assertSame(0, $oParams->getInt('int3'));
54 public function testGetIntWithNonNumber()
56 $this->expectException(\Exception::class);
57 $this->expectExceptionMessage("Integer number expected for parameter 'int4'");
59 (new ParameterParser(array('int4' => 'a')))->getInt('int4');
63 public function testGetIntWithEmpytString()
65 $this->expectException(\Exception::class);
66 $this->expectExceptionMessage("Integer number expected for parameter 'int5'");
68 (new ParameterParser(array('int5' => '')))->getInt('int5');
72 public function testGetFloat()
75 $oParams = new ParameterParser(array(
81 $this->assertSame(false, $oParams->getFloat('non-exists'));
82 $this->assertSame(999, $oParams->getFloat('non-exists', 999));
83 $this->assertSame(1.0, $oParams->getFloat('float1'));
84 $this->assertSame(-5.0, $oParams->getFloat('float2'));
85 $this->assertSame(0.0, $oParams->getFloat('float3'));
88 public function testGetFloatWithEmptyString()
90 $this->expectException(\Exception::class);
91 $this->expectExceptionMessage("Floating-point number expected for parameter 'float4'");
93 (new ParameterParser(array('float4' => '')))->getFloat('float4');
96 public function testGetFloatWithTextString()
98 $this->expectException(\Exception::class);
99 $this->expectExceptionMessage("Floating-point number expected for parameter 'float5'");
101 (new ParameterParser(array('float5' => 'a')))->getFloat('float5');
105 public function testGetFloatWithInvalidNumber()
107 $this->expectException(\Exception::class);
108 $this->expectExceptionMessage("Floating-point number expected for parameter 'float6'");
110 (new ParameterParser(array('float6' => '-55.')))->getFloat('float6');
114 public function testGetString()
116 $oParams = new ParameterParser(array(
122 $this->assertSame(false, $oParams->getString('non-exists'));
123 $this->assertSame('default', $oParams->getString('non-exists', 'default'));
124 $this->assertSame('abc', $oParams->getString('str1'));
125 $this->assertSame(false, $oParams->getStringList('str2'));
126 $this->assertSame(false, $oParams->getStringList('str3')); // sadly PHP magic treats 0 as false when returned
130 public function testGetSet()
132 $oParams = new ParameterParser(array(
138 $this->assertSame(false, $oParams->getSet('non-exists', array('foo', 'bar')));
139 $this->assertSame('default', $oParams->getSet('non-exists', array('foo', 'bar'), 'default'));
140 $this->assertSame('foo', $oParams->getSet('val1', array('foo', 'bar')));
142 $this->assertSame(false, $oParams->getSet('val2', array('foo', 'bar')));
143 $this->assertSame(0, $oParams->getSet('val3', array('foo', 'bar')));
147 public function testGetSetWithValueNotInSet()
149 $this->expectException(\Exception::class);
150 $this->expectExceptionMessage("Parameter 'val4' must be one of: foo, bar");
152 (new ParameterParser(array('val4' => 'faz')))->getSet('val4', array('foo', 'bar'));
156 public function testGetStringList()
158 $oParams = new ParameterParser(array(
159 'list1' => ',a,b,c,,c,d',
165 $this->assertSame(false, $oParams->getStringList('non-exists'));
166 $this->assertSame(array('a', 'b'), $oParams->getStringList('non-exists', array('a', 'b')));
167 $this->assertSame(array('a', 'b', 'c', 'c', 'd'), $oParams->getStringList('list1'));
168 $this->assertSame(array('a'), $oParams->getStringList('list2'));
169 $this->assertSame(false, $oParams->getStringList('list3'));
170 $this->assertSame(false, $oParams->getStringList('list4'));
174 public function testGetPreferredLanguages()
176 $oParams = new ParameterParser(array('accept-language' => ''));
177 $this->assertSame(array(
178 'name:default' => 'name:default',
181 'official_name:default' => 'official_name:default',
182 'short_name:default' => 'short_name:default',
183 'official_name' => 'official_name',
184 'short_name' => 'short_name',
187 ), $oParams->getPreferredLanguages('default'));
189 $oParams = new ParameterParser(array('accept-language' => 'de,en'));
190 $this->assertSame(array(
191 'name:de' => 'name:de',
192 'name:en' => 'name:en',
195 'official_name:de' => 'official_name:de',
196 'short_name:de' => 'short_name:de',
197 'official_name:en' => 'official_name:en',
198 'short_name:en' => 'short_name:en',
199 'official_name' => 'official_name',
200 'short_name' => 'short_name',
203 ), $oParams->getPreferredLanguages('default'));
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 'name:fr' => 'name:fr',
209 'name:en-ca' => 'name:en-ca',
210 'name:en' => 'name:en',
213 'official_name:fr-ca' => 'official_name:fr-ca',
214 'short_name:fr-ca' => 'short_name:fr-ca',
215 'official_name:fr' => 'official_name:fr',
216 'short_name:fr' => 'short_name:fr',
217 'official_name:en-ca' => 'official_name:en-ca',
218 'short_name:en-ca' => 'short_name:en-ca',
219 'official_name:en' => 'official_name:en',
220 'short_name:en' => 'short_name:en',
221 'official_name' => 'official_name',
222 'short_name' => 'short_name',
225 ), $oParams->getPreferredLanguages('default'));
227 $oParams = new ParameterParser(array('accept-language' => 'ja_rm,zh_pinyin'));
228 $this->assertSame(array(
229 'name:ja_rm' => 'name:ja_rm',
230 'name:zh_pinyin' => 'name:zh_pinyin',
231 'name:ja' => 'name:ja',
232 'name:zh' => 'name:zh',
235 'official_name:ja_rm' => 'official_name:ja_rm',
236 'short_name:ja_rm' => 'short_name:ja_rm',
237 'official_name:zh_pinyin' => 'official_name:zh_pinyin',
238 'short_name:zh_pinyin' => 'short_name:zh_pinyin',
239 'official_name:ja' => 'official_name:ja',
240 'short_name:ja' => 'short_name:ja',
241 'official_name:zh' => 'official_name:zh',
242 'short_name:zh' => 'short_name:zh',
243 'official_name' => 'official_name',
244 'short_name' => 'short_name',
247 ), $oParams->getPreferredLanguages('default'));
250 public function testHasSetAny()
252 $oParams = new ParameterParser(array(
257 'five' => 'anystring'
259 $this->assertFalse($oParams->hasSetAny(array()));
260 $this->assertFalse($oParams->hasSetAny(array('')));
261 $this->assertFalse($oParams->hasSetAny(array('unknown')));
262 $this->assertFalse($oParams->hasSetAny(array('one', 'two', 'three')));
263 $this->assertTrue($oParams->hasSetAny(array('one', 'four')));
264 $this->assertTrue($oParams->hasSetAny(array('four')));
265 $this->assertTrue($oParams->hasSetAny(array('five')));