7 require_once('../../lib/ParameterParser.php');
10 function userError($sError)
12 throw new Exception($sError);
15 class ParameterParserTest extends \PHPUnit_Framework_TestCase
19 public function testGetBool()
21 $oParams = new ParameterParser(array(
29 $this->assertSame(false, $oParams->getBool('non-exists'));
30 $this->assertSame(true, $oParams->getBool('non-exists', true));
31 $this->assertSame(true, $oParams->getBool('bool1'));
32 $this->assertSame(false, $oParams->getBool('bool2'));
33 $this->assertSame(true, $oParams->getBool('bool3'));
34 $this->assertSame(true, $oParams->getBool('bool4'));
35 $this->assertSame(false, $oParams->getBool('bool5'));
39 public function testGetInt()
41 $oParams = new ParameterParser(array(
47 $this->assertSame(false, $oParams->getInt('non-exists'));
48 $this->assertSame(999, $oParams->getInt('non-exists', 999));
49 $this->assertSame(5, $oParams->getInt('int1'));
51 $this->assertSame(-1, $oParams->getInt('int2'));
52 $this->assertSame(0, $oParams->getInt('int3'));
56 public function testGetIntWithNonNumber()
58 $this->setExpectedException(Exception::class, "Integer number expected for parameter 'int4'");
59 (new ParameterParser(array('int4' => 'a')))->getInt('int4');
63 public function testGetIntWithEmpytString()
65 $this->setExpectedException(Exception::class, "Integer number expected for parameter 'int5'");
66 (new ParameterParser(array('int5' => '')))->getInt('int5');
70 public function testGetFloat()
73 $oParams = new ParameterParser(array(
79 $this->assertSame(false, $oParams->getFloat('non-exists'));
80 $this->assertSame(999, $oParams->getFloat('non-exists', 999));
81 $this->assertSame(1.0, $oParams->getFloat('float1'));
82 $this->assertSame(-5.0, $oParams->getFloat('float2'));
83 $this->assertSame(0.0, $oParams->getFloat('float3'));
86 public function testGetFloatWithEmptyString()
88 $this->setExpectedException(Exception::class, "Floating-point number expected for parameter 'float4'");
89 (new ParameterParser(array('float4' => '')))->getFloat('float4');
92 public function testGetFloatWithTextString()
94 $this->setExpectedException(Exception::class, "Floating-point number expected for parameter 'float5'");
95 (new ParameterParser(array('float5' => 'a')))->getFloat('float5');
99 public function testGetFloatWithInvalidNumber()
101 $this->setExpectedException(Exception::class, "Floating-point number expected for parameter 'float6'");
102 (new ParameterParser(array('float6' => '-55.')))->getFloat('float6');
106 public function testGetString()
108 $oParams = new ParameterParser(array(
114 $this->assertSame(false, $oParams->getString('non-exists'));
115 $this->assertSame('default', $oParams->getString('non-exists', 'default'));
116 $this->assertSame('abc', $oParams->getString('str1'));
117 $this->assertSame(false, $oParams->getStringList('str2'));
118 $this->assertSame(false, $oParams->getStringList('str3')); // sadly PHP magic treats 0 as false when returned
122 public function testGetSet()
124 $oParams = new ParameterParser(array(
130 $this->assertSame(false, $oParams->getSet('non-exists', array('foo', 'bar')));
131 $this->assertSame('default', $oParams->getSet('non-exists', array('foo', 'bar'), 'default'));
132 $this->assertSame('foo', $oParams->getSet('val1', array('foo', 'bar')));
134 $this->assertSame(false, $oParams->getSet('val2', array('foo', 'bar')));
135 $this->assertSame(0, $oParams->getSet('val3', array('foo', 'bar')));
139 public function testGetSetWithValueNotInSet()
141 $this->setExpectedException(Exception::class, "Parameter 'val4' must be one of: foo, bar");
142 (new ParameterParser(array('val4' => 'faz')))->getSet('val4', array('foo', 'bar'));
146 public function testGetStringList()
148 $oParams = new ParameterParser(array(
149 'list1' => ',a,b,c,,c,d',
155 $this->assertSame(false, $oParams->getStringList('non-exists'));
156 $this->assertSame(array('a', 'b'), $oParams->getStringList('non-exists', array('a', 'b')));
157 $this->assertSame(array('a', 'b', 'c', 'c', 'd'), $oParams->getStringList('list1'));
158 $this->assertSame(array('a'), $oParams->getStringList('list2'));
159 $this->assertSame(false, $oParams->getStringList('list3'));
160 $this->assertSame(false, $oParams->getStringList('list4'));
164 public function testGetPreferredLanguages()
166 $oParams = new ParameterParser(array('accept-language' => ''));
167 $this->assertSame(array(
168 'short_name:default' => 'short_name:default',
169 'name:default' => 'name:default',
170 'short_name' => 'short_name',
173 'official_name:default' => 'official_name:default',
174 'official_name' => 'official_name',
177 ), $oParams->getPreferredLanguages('default'));
179 $oParams = new ParameterParser(array('accept-language' => 'de,en'));
180 $this->assertSame(array(
181 'short_name:de' => 'short_name:de',
182 'name:de' => 'name:de',
183 'short_name:en' => 'short_name:en',
184 'name:en' => 'name:en',
185 'short_name' => 'short_name',
188 'official_name:de' => 'official_name:de',
189 'official_name:en' => 'official_name:en',
190 'official_name' => 'official_name',
193 ), $oParams->getPreferredLanguages('default'));
195 $oParams = new ParameterParser(array('accept-language' => 'fr-ca,fr;q=0.8,en-ca;q=0.5,en;q=0.3'));
196 $this->assertSame(array(
197 'short_name:fr-ca' => 'short_name:fr-ca',
198 'name:fr-ca' => 'name:fr-ca',
199 'short_name:fr' => 'short_name:fr',
200 'name:fr' => 'name:fr',
201 'short_name:en-ca' => 'short_name:en-ca',
202 'name:en-ca' => 'name:en-ca',
203 'short_name:en' => 'short_name:en',
204 'name:en' => 'name:en',
205 'short_name' => 'short_name',
208 'official_name:fr-ca' => 'official_name:fr-ca',
209 'official_name:fr' => 'official_name:fr',
210 'official_name:en-ca' => 'official_name:en-ca',
211 'official_name:en' => 'official_name:en',
212 'official_name' => 'official_name',
215 ), $oParams->getPreferredLanguages('default'));