5 require_once(CONST_BasePath.'/lib/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 'short_name:default' => 'short_name:default',
179 'name:default' => 'name:default',
180 'short_name' => 'short_name',
183 'official_name:default' => 'official_name:default',
184 'official_name' => 'official_name',
187 ), $oParams->getPreferredLanguages('default'));
189 $oParams = new ParameterParser(array('accept-language' => 'de,en'));
190 $this->assertSame(array(
191 'short_name:de' => 'short_name:de',
192 'name:de' => 'name:de',
193 'short_name:en' => 'short_name:en',
194 'name:en' => 'name:en',
195 'short_name' => 'short_name',
198 'official_name:de' => 'official_name:de',
199 'official_name:en' => 'official_name:en',
200 'official_name' => 'official_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 'short_name:fr-ca' => 'short_name:fr-ca',
208 'name:fr-ca' => 'name:fr-ca',
209 'short_name:fr' => 'short_name:fr',
210 'name:fr' => 'name:fr',
211 'short_name:en-ca' => 'short_name:en-ca',
212 'name:en-ca' => 'name:en-ca',
213 'short_name:en' => 'short_name:en',
214 'name:en' => 'name:en',
215 'short_name' => 'short_name',
218 'official_name:fr-ca' => 'official_name:fr-ca',
219 'official_name:fr' => 'official_name:fr',
220 'official_name:en-ca' => 'official_name:en-ca',
221 'official_name:en' => 'official_name:en',
222 'official_name' => 'official_name',
225 ), $oParams->getPreferredLanguages('default'));