| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\HttpFoundation\Tests;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\HttpFoundation\ParameterBag;
|
|
|
15 |
|
|
|
16 |
class ParameterBagTest extends \PHPUnit_Framework_TestCase
|
|
|
17 |
{
|
|
|
18 |
public function testConstructor()
|
|
|
19 |
{
|
|
|
20 |
$this->testAll();
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
public function testAll()
|
|
|
24 |
{
|
|
|
25 |
$bag = new ParameterBag(array('foo' => 'bar'));
|
|
|
26 |
$this->assertEquals(array('foo' => 'bar'), $bag->all(), '->all() gets all the input');
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
public function testKeys()
|
|
|
30 |
{
|
|
|
31 |
$bag = new ParameterBag(array('foo' => 'bar'));
|
|
|
32 |
$this->assertEquals(array('foo'), $bag->keys());
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
public function testAdd()
|
|
|
36 |
{
|
|
|
37 |
$bag = new ParameterBag(array('foo' => 'bar'));
|
|
|
38 |
$bag->add(array('bar' => 'bas'));
|
|
|
39 |
$this->assertEquals(array('foo' => 'bar', 'bar' => 'bas'), $bag->all());
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
public function testRemove()
|
|
|
43 |
{
|
|
|
44 |
$bag = new ParameterBag(array('foo' => 'bar'));
|
|
|
45 |
$bag->add(array('bar' => 'bas'));
|
|
|
46 |
$this->assertEquals(array('foo' => 'bar', 'bar' => 'bas'), $bag->all());
|
|
|
47 |
$bag->remove('bar');
|
|
|
48 |
$this->assertEquals(array('foo' => 'bar'), $bag->all());
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public function testReplace()
|
|
|
52 |
{
|
|
|
53 |
$bag = new ParameterBag(array('foo' => 'bar'));
|
|
|
54 |
|
|
|
55 |
$bag->replace(array('FOO' => 'BAR'));
|
|
|
56 |
$this->assertEquals(array('FOO' => 'BAR'), $bag->all(), '->replace() replaces the input with the argument');
|
|
|
57 |
$this->assertFalse($bag->has('foo'), '->replace() overrides previously set the input');
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
public function testGet()
|
|
|
61 |
{
|
|
|
62 |
$bag = new ParameterBag(array('foo' => 'bar', 'null' => null));
|
|
|
63 |
|
|
|
64 |
$this->assertEquals('bar', $bag->get('foo'), '->get() gets the value of a parameter');
|
|
|
65 |
$this->assertEquals('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined');
|
|
|
66 |
$this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public function testGetDoesNotUseDeepByDefault()
|
|
|
70 |
{
|
|
|
71 |
$bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
|
|
|
72 |
|
|
|
73 |
$this->assertNull($bag->get('foo[bar]'));
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public function testSet()
|
|
|
77 |
{
|
|
|
78 |
$bag = new ParameterBag(array());
|
|
|
79 |
|
|
|
80 |
$bag->set('foo', 'bar');
|
|
|
81 |
$this->assertEquals('bar', $bag->get('foo'), '->set() sets the value of parameter');
|
|
|
82 |
|
|
|
83 |
$bag->set('foo', 'baz');
|
|
|
84 |
$this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public function testHas()
|
|
|
88 |
{
|
|
|
89 |
$bag = new ParameterBag(array('foo' => 'bar'));
|
|
|
90 |
|
|
|
91 |
$this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
|
|
|
92 |
$this->assertFalse($bag->has('unknown'), '->has() return false if a parameter is not defined');
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
public function testGetAlpha()
|
|
|
96 |
{
|
|
|
97 |
$bag = new ParameterBag(array('word' => 'foo_BAR_012'));
|
|
|
98 |
|
|
|
99 |
$this->assertEquals('fooBAR', $bag->getAlpha('word'), '->getAlpha() gets only alphabetic characters');
|
|
|
100 |
$this->assertEquals('', $bag->getAlpha('unknown'), '->getAlpha() returns empty string if a parameter is not defined');
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
public function testGetAlnum()
|
|
|
104 |
{
|
|
|
105 |
$bag = new ParameterBag(array('word' => 'foo_BAR_012'));
|
|
|
106 |
|
|
|
107 |
$this->assertEquals('fooBAR012', $bag->getAlnum('word'), '->getAlnum() gets only alphanumeric characters');
|
|
|
108 |
$this->assertEquals('', $bag->getAlnum('unknown'), '->getAlnum() returns empty string if a parameter is not defined');
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
public function testGetDigits()
|
|
|
112 |
{
|
|
|
113 |
$bag = new ParameterBag(array('word' => 'foo_BAR_012'));
|
|
|
114 |
|
|
|
115 |
$this->assertEquals('012', $bag->getDigits('word'), '->getDigits() gets only digits as string');
|
|
|
116 |
$this->assertEquals('', $bag->getDigits('unknown'), '->getDigits() returns empty string if a parameter is not defined');
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public function testGetInt()
|
|
|
120 |
{
|
|
|
121 |
$bag = new ParameterBag(array('digits' => '0123'));
|
|
|
122 |
|
|
|
123 |
$this->assertEquals(123, $bag->getInt('digits'), '->getInt() gets a value of parameter as integer');
|
|
|
124 |
$this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined');
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
public function testFilter()
|
|
|
128 |
{
|
|
|
129 |
$bag = new ParameterBag(array(
|
|
|
130 |
'digits' => '0123ab',
|
|
|
131 |
'email' => 'example@example.com',
|
|
|
132 |
'url' => 'http://example.com/foo',
|
|
|
133 |
'dec' => '256',
|
|
|
134 |
'hex' => '0x100',
|
|
|
135 |
'array' => array('bang'),
|
|
|
136 |
));
|
|
|
137 |
|
|
|
138 |
$this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');
|
|
|
139 |
|
|
|
140 |
$this->assertEquals('0123', $bag->filter('digits', '', FILTER_SANITIZE_NUMBER_INT), '->filter() gets a value of parameter as integer filtering out invalid characters');
|
|
|
141 |
|
|
|
142 |
$this->assertEquals('example@example.com', $bag->filter('email', '', FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');
|
|
|
143 |
|
|
|
144 |
$this->assertEquals('http://example.com/foo', $bag->filter('url', '', FILTER_VALIDATE_URL, array('flags' => FILTER_FLAG_PATH_REQUIRED)), '->filter() gets a value of parameter as URL with a path');
|
|
|
145 |
|
|
|
146 |
// This test is repeated for code-coverage
|
|
|
147 |
$this->assertEquals('http://example.com/foo', $bag->filter('url', '', FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED), '->filter() gets a value of parameter as URL with a path');
|
|
|
148 |
|
|
|
149 |
$this->assertFalse($bag->filter('dec', '', FILTER_VALIDATE_INT, array(
|
|
|
150 |
'flags' => FILTER_FLAG_ALLOW_HEX,
|
|
|
151 |
'options' => array('min_range' => 1, 'max_range' => 0xff),
|
|
|
152 |
)), '->filter() gets a value of parameter as integer between boundaries');
|
|
|
153 |
|
|
|
154 |
$this->assertFalse($bag->filter('hex', '', FILTER_VALIDATE_INT, array(
|
|
|
155 |
'flags' => FILTER_FLAG_ALLOW_HEX,
|
|
|
156 |
'options' => array('min_range' => 1, 'max_range' => 0xff),
|
|
|
157 |
)), '->filter() gets a value of parameter as integer between boundaries');
|
|
|
158 |
|
|
|
159 |
$this->assertEquals(array('bang'), $bag->filter('array', ''), '->filter() gets a value of parameter as an array');
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
public function testGetIterator()
|
|
|
163 |
{
|
|
|
164 |
$parameters = array('foo' => 'bar', 'hello' => 'world');
|
|
|
165 |
$bag = new ParameterBag($parameters);
|
|
|
166 |
|
|
|
167 |
$i = 0;
|
|
|
168 |
foreach ($bag as $key => $val) {
|
|
|
169 |
++$i;
|
|
|
170 |
$this->assertEquals($parameters[$key], $val);
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
$this->assertEquals(count($parameters), $i);
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
public function testCount()
|
|
|
177 |
{
|
|
|
178 |
$parameters = array('foo' => 'bar', 'hello' => 'world');
|
|
|
179 |
$bag = new ParameterBag($parameters);
|
|
|
180 |
|
|
|
181 |
$this->assertEquals(count($parameters), count($bag));
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
public function testGetBoolean()
|
|
|
185 |
{
|
|
|
186 |
$parameters = array('string_true' => 'true', 'string_false' => 'false');
|
|
|
187 |
$bag = new ParameterBag($parameters);
|
|
|
188 |
|
|
|
189 |
$this->assertTrue($bag->getBoolean('string_true'), '->getBoolean() gets the string true as boolean true');
|
|
|
190 |
$this->assertFalse($bag->getBoolean('string_false'), '->getBoolean() gets the string false as boolean false');
|
|
|
191 |
$this->assertFalse($bag->getBoolean('unknown'), '->getBoolean() returns false if a parameter is not defined');
|
|
|
192 |
}
|
|
|
193 |
}
|