1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 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: 19: 20:
21: public function testConstructor()
22: {
23: $this->testAll();
24: }
25:
26: 27: 28:
29: public function testAll()
30: {
31: $bag = new ParameterBag(array('foo' => 'bar'));
32: $this->assertEquals(array('foo' => 'bar'), $bag->all(), '->all() gets all the input');
33: }
34:
35: public function testKeys()
36: {
37: $bag = new ParameterBag(array('foo' => 'bar'));
38: $this->assertEquals(array('foo'), $bag->keys());
39: }
40:
41: public function testAdd()
42: {
43: $bag = new ParameterBag(array('foo' => 'bar'));
44: $bag->add(array('bar' => 'bas'));
45: $this->assertEquals(array('foo' => 'bar', 'bar' => 'bas'), $bag->all());
46: }
47:
48: public function testRemove()
49: {
50: $bag = new ParameterBag(array('foo' => 'bar'));
51: $bag->add(array('bar' => 'bas'));
52: $this->assertEquals(array('foo' => 'bar', 'bar' => 'bas'), $bag->all());
53: $bag->remove('bar');
54: $this->assertEquals(array('foo' => 'bar'), $bag->all());
55: }
56:
57: 58: 59:
60: public function testReplace()
61: {
62: $bag = new ParameterBag(array('foo' => 'bar'));
63:
64: $bag->replace(array('FOO' => 'BAR'));
65: $this->assertEquals(array('FOO' => 'BAR'), $bag->all(), '->replace() replaces the input with the argument');
66: $this->assertFalse($bag->has('foo'), '->replace() overrides previously set the input');
67: }
68:
69: 70: 71:
72: public function testGet()
73: {
74: $bag = new ParameterBag(array('foo' => 'bar', 'null' => null));
75:
76: $this->assertEquals('bar', $bag->get('foo'), '->get() gets the value of a parameter');
77: $this->assertEquals('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined');
78: $this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
79: }
80:
81: public function testGetDoesNotUseDeepByDefault()
82: {
83: $bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
84:
85: $this->assertNull($bag->get('foo[bar]'));
86: }
87:
88: 89: 90: 91:
92: public function testGetDeepWithInvalidPaths($path)
93: {
94: $bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
95:
96: $bag->get($path, null, true);
97: }
98:
99: public function getInvalidPaths()
100: {
101: return array(
102: array('foo[['),
103: array('foo[d'),
104: array('foo[bar]]'),
105: array('foo[bar]d'),
106: );
107: }
108:
109: public function testGetDeep()
110: {
111: $bag = new ParameterBag(array('foo' => array('bar' => array('moo' => 'boo'))));
112:
113: $this->assertEquals(array('moo' => 'boo'), $bag->get('foo[bar]', null, true));
114: $this->assertEquals('boo', $bag->get('foo[bar][moo]', null, true));
115: $this->assertEquals('default', $bag->get('foo[bar][foo]', 'default', true));
116: $this->assertEquals('default', $bag->get('bar[moo][foo]', 'default', true));
117: }
118:
119: 120: 121:
122: public function testSet()
123: {
124: $bag = new ParameterBag(array());
125:
126: $bag->set('foo', 'bar');
127: $this->assertEquals('bar', $bag->get('foo'), '->set() sets the value of parameter');
128:
129: $bag->set('foo', 'baz');
130: $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
131: }
132:
133: 134: 135:
136: public function testHas()
137: {
138: $bag = new ParameterBag(array('foo' => 'bar'));
139:
140: $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
141: $this->assertFalse($bag->has('unknown'), '->has() return false if a parameter is not defined');
142: }
143:
144: 145: 146:
147: public function testGetAlpha()
148: {
149: $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
150:
151: $this->assertEquals('fooBAR', $bag->getAlpha('word'), '->getAlpha() gets only alphabetic characters');
152: $this->assertEquals('', $bag->getAlpha('unknown'), '->getAlpha() returns empty string if a parameter is not defined');
153: }
154:
155: 156: 157:
158: public function testGetAlnum()
159: {
160: $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
161:
162: $this->assertEquals('fooBAR012', $bag->getAlnum('word'), '->getAlnum() gets only alphanumeric characters');
163: $this->assertEquals('', $bag->getAlnum('unknown'), '->getAlnum() returns empty string if a parameter is not defined');
164: }
165:
166: 167: 168:
169: public function testGetDigits()
170: {
171: $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
172:
173: $this->assertEquals('012', $bag->getDigits('word'), '->getDigits() gets only digits as string');
174: $this->assertEquals('', $bag->getDigits('unknown'), '->getDigits() returns empty string if a parameter is not defined');
175: }
176:
177: 178: 179:
180: public function testGetInt()
181: {
182: $bag = new ParameterBag(array('digits' => '0123'));
183:
184: $this->assertEquals(123, $bag->getInt('digits'), '->getInt() gets a value of parameter as integer');
185: $this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined');
186: }
187:
188: 189: 190:
191: public function testFilter()
192: {
193: $bag = new ParameterBag(array(
194: 'digits' => '0123ab',
195: 'email' => 'example@example.com',
196: 'url' => 'http://example.com/foo',
197: 'dec' => '256',
198: 'hex' => '0x100',
199: 'array' => array('bang'),
200: ));
201:
202: $this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');
203:
204: $this->assertEquals('0123', $bag->filter('digits', '', false, FILTER_SANITIZE_NUMBER_INT), '->filter() gets a value of parameter as integer filtering out invalid characters');
205:
206: $this->assertEquals('example@example.com', $bag->filter('email', '', false, FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');
207:
208: $this->assertEquals('http://example.com/foo', $bag->filter('url', '', false, FILTER_VALIDATE_URL, array('flags' => FILTER_FLAG_PATH_REQUIRED)), '->filter() gets a value of parameter as URL with a path');
209:
210:
211: $this->assertEquals('http://example.com/foo', $bag->filter('url', '', false, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED), '->filter() gets a value of parameter as URL with a path');
212:
213: $this->assertFalse($bag->filter('dec', '', false, FILTER_VALIDATE_INT, array(
214: 'flags' => FILTER_FLAG_ALLOW_HEX,
215: 'options' => array('min_range' => 1, 'max_range' => 0xff))
216: ), '->filter() gets a value of parameter as integer between boundaries');
217:
218: $this->assertFalse($bag->filter('hex', '', false, FILTER_VALIDATE_INT, array(
219: 'flags' => FILTER_FLAG_ALLOW_HEX,
220: 'options' => array('min_range' => 1, 'max_range' => 0xff))
221: ), '->filter() gets a value of parameter as integer between boundaries');
222:
223: $this->assertEquals(array('bang'), $bag->filter('array', '', false), '->filter() gets a value of parameter as an array');
224: }
225:
226: 227: 228:
229: public function testGetIterator()
230: {
231: $parameters = array('foo' => 'bar', 'hello' => 'world');
232: $bag = new ParameterBag($parameters);
233:
234: $i = 0;
235: foreach ($bag as $key => $val) {
236: $i++;
237: $this->assertEquals($parameters[$key], $val);
238: }
239:
240: $this->assertEquals(count($parameters), $i);
241: }
242:
243: 244: 245:
246: public function testCount()
247: {
248: $parameters = array('foo' => 'bar', 'hello' => 'world');
249: $bag = new ParameterBag($parameters);
250:
251: $this->assertEquals(count($parameters), count($bag));
252: }
253:
254: 255: 256:
257: public function testGetBoolean()
258: {
259: $parameters = array('string_true' => 'true', 'string_false' => 'false');
260: $bag = new ParameterBag($parameters);
261:
262: $this->assertTrue($bag->getBoolean('string_true'), '->getBoolean() gets the string true as boolean true');
263: $this->assertFalse($bag->getBoolean('string_false'), '->getBoolean() gets the string false as boolean false');
264: $this->assertFalse($bag->getBoolean('unknown'), '->getBoolean() returns false if a parameter is not defined');
265: }
266: }
267: