7 require_once('../../lib/ParameterParser.php');
10 function userError($sError)
12 throw new Exception($sError);
15 class ParameterParserTest extends \PHPUnit_Framework_TestCase
19 protected function setUp()
24 public function testGetBool()
26 $oParams = new ParameterParser([
34 $this->assertEquals(false, $oParams->getBool('non-exists'));
35 $this->assertEquals(true, $oParams->getBool('non-exists', true));
36 $this->assertEquals(true, $oParams->getBool('bool1'));
37 $this->assertEquals(false, $oParams->getBool('bool2'));
38 $this->assertEquals(true, $oParams->getBool('bool3'));
39 $this->assertEquals(true, $oParams->getBool('bool4'));
40 $this->assertEquals(false, $oParams->getBool('bool5'));
44 public function testGetInt()
46 $oParams = new ParameterParser([
53 $this->assertEquals(false, $oParams->getInt('non-exists'));
54 $this->assertEquals(999, $oParams->getInt('non-exists', 999));
55 $this->assertEquals(5, $oParams->getInt('int1'));
57 $this->assertEquals(-1, $oParams->getInt('int2'));
58 $this->assertEquals(false, $oParams->getInt('int3'));
59 $this->assertEquals(false, $oParams->getInt('int4')); // FIXME: should be 0 instead?
63 public function testGetIntWithEmpytString()
65 $this->setExpectedException(Exception::class, "Integer number expected for parameter 'int5'");
66 (new ParameterParser(['int5' => 'a']))->getInt('int5');
70 public function testGetFloat()
73 $oParams = new ParameterParser([
80 $this->assertEquals(false, $oParams->getFloat('non-exists'));
81 $this->assertEquals(999, $oParams->getFloat('non-exists', 999));
82 $this->assertEquals(1, $oParams->getFloat('float1'));
83 $this->assertEquals(-5, $oParams->getFloat('float2'));
84 $this->assertEquals(false, $oParams->getFloat('float3'));
85 $this->assertEquals(false, $oParams->getFloat('float4')); // FIXME: should be 0 instead?
89 public function testGetFloatWithString()
91 $this->setExpectedException(Exception::class, "Floating-point number expected for parameter 'float5'");
92 (new ParameterParser(['float5' => 'a']))->getFloat('float5');
96 public function testGetFloatWithInvalidNumber()
98 $this->setExpectedException(Exception::class, "Floating-point number expected for parameter 'float6'");
99 (new ParameterParser(['float6' => '-55.']))->getFloat('float6');
103 public function testGetString()
105 $oParams = new ParameterParser([
111 $this->assertEquals(false, $oParams->getString('non-exists'));
112 $this->assertEquals('default', $oParams->getString('non-exists', 'default'));
113 $this->assertEquals('abc', $oParams->getString('str1'));
114 $this->assertEquals(false, $oParams->getStringList('str2'));
115 $this->assertEquals(false, $oParams->getStringList('str3')); // FIXME: should be 0 instead?
119 public function testGetSet()
121 $oParams = new ParameterParser([
127 $this->assertEquals(false, $oParams->getSet('non-exists', ['foo', 'bar']));
128 // FIXME: unclear if the default value has to be part of the set
129 $this->assertEquals('default', $oParams->getSet('non-exists', ['foo', 'bar'], 'default'));
130 $this->assertEquals('foo', $oParams->getSet('val1', ['foo', 'bar']));
132 $this->assertEquals(false, $oParams->getSet('val2', ['foo', 'bar']));
133 $this->assertEquals(false, $oParams->getSet('val3', ['foo', 'bar']));
137 public function testGetSetWithValueNotInSet()
139 $this->setExpectedException(Exception::class, "Parameter 'val4' must be one of: foo, bar");
140 (new ParameterParser(['val4' => 'faz']))->getSet('val4', ['foo', 'bar']);
144 public function testGetStringList()
146 $oParams = new ParameterParser([
147 'list1' => ',a,b,c,,c,d',
153 $this->assertEquals(false, $oParams->getStringList('non-exists'));
154 $this->assertEquals(['a', 'b'], $oParams->getStringList('non-exists', ['a', 'b']));
155 // FIXME: unclear if empty string items should be removed
156 $this->assertEquals(['', 'a', 'b', 'c', '', 'c', 'd'], $oParams->getStringList('list1'));
157 $this->assertEquals(['a'], $oParams->getStringList('list2'));
158 $this->assertEquals(false, $oParams->getStringList('list3'));
159 $this->assertEquals(false, $oParams->getStringList('list4'));
163 public function testGetPreferredLanguages()
165 $oParams = new ParameterParser(['accept-language' => '']);
166 $this->assertEquals([
171 'name:default' => 'name:default',
172 'short_name' => 'short_name',
173 'short_name:default' => 'short_name:default',
174 'official_name' => 'official_name',
175 'official_name:default' => 'official_name:default',
176 ], $oParams->getPreferredLanguages('default'));
178 $oParams = new ParameterParser(['accept-language' => 'de,en']);
179 $this->assertEquals([
184 'name:de' => 'name:de',
185 'name:en' => 'name:en',
186 'short_name' => 'short_name',
187 'short_name:de' => 'short_name:de',
188 'short_name:en' => 'short_name:en',
189 'official_name' => 'official_name',
190 'official_name:de' => 'official_name:de',
191 'official_name:en' => 'official_name:en',
192 ], $oParams->getPreferredLanguages('default'));
194 $oParams = new ParameterParser(['accept-language' => 'fr-ca,fr;q=0.8,en-ca;q=0.5,en;q=0.3']);
195 $this->assertEquals([
196 'short_name:fr-ca' => 'short_name:fr-ca',
197 'name:fr-ca' => 'name:fr-ca',
198 'short_name:fr' => 'short_name:fr',
199 'name:fr' => 'name:fr',
200 'short_name:en-ca' => 'short_name:en-ca',
201 'name:en-ca' => 'name:en-ca',
202 'short_name:en' => 'short_name:en',
203 'name:en' => 'name:en',
204 'short_name' => 'short_name',
207 'official_name:fr-ca' => 'official_name:fr-ca',
208 'official_name:fr' => 'official_name:fr',
209 'official_name:en-ca' => 'official_name:en-ca',
210 'official_name:en' => 'official_name:en',
211 'official_name' => 'official_name',
214 ], $oParams->getPreferredLanguages('default'));