14 use PHPUnit\Framework\TestCase;
27 $this->assertEquals(array(
'foo' =>
'bar'), $bag->all(),
'->all() gets all the input');
33 $this->assertEquals(array(
'foo'), $bag->keys());
39 $bag->add(array(
'bar' =>
'bas'));
40 $this->assertEquals(array(
'foo' =>
'bar',
'bar' =>
'bas'), $bag->all());
46 $bag->add(array(
'bar' =>
'bas'));
47 $this->assertEquals(array(
'foo' =>
'bar',
'bar' =>
'bas'), $bag->all());
49 $this->assertEquals(array(
'foo' =>
'bar'), $bag->all());
56 $bag->replace(array(
'FOO' =>
'BAR'));
57 $this->assertEquals(array(
'FOO' =>
'BAR'), $bag->all(),
'->replace() replaces the input with the argument');
58 $this->assertFalse($bag->has(
'foo'),
'->replace() overrides previously set the input');
63 $bag =
new ParameterBag(array(
'foo' =>
'bar',
'null' =>
null));
65 $this->assertEquals(
'bar', $bag->get(
'foo'),
'->get() gets the value of a parameter');
66 $this->assertEquals(
'default', $bag->get(
'unknown',
'default'),
'->get() returns second argument as default if a parameter is not defined');
67 $this->assertNull($bag->get(
'null',
'default'),
'->get() returns null if null is set');
72 $bag =
new ParameterBag(array(
'foo' => array(
'bar' =>
'moo')));
74 $this->assertNull($bag->get(
'foo[bar]'));
81 $bag->set(
'foo',
'bar');
82 $this->assertEquals(
'bar', $bag->get(
'foo'),
'->set() sets the value of parameter');
84 $bag->set(
'foo',
'baz');
85 $this->assertEquals(
'baz', $bag->get(
'foo'),
'->set() overrides previously set parameter');
92 $this->assertTrue($bag->has(
'foo'),
'->has() returns true if a parameter is defined');
93 $this->assertFalse($bag->has(
'unknown'),
'->has() return false if a parameter is not defined');
100 $this->assertEquals(
'fooBAR', $bag->getAlpha(
'word'),
'->getAlpha() gets only alphabetic characters');
101 $this->assertEquals(
'', $bag->getAlpha(
'unknown'),
'->getAlpha() returns empty string if a parameter is not defined');
106 $bag =
new ParameterBag(array(
'word' =>
'foo_BAR_012'));
108 $this->assertEquals(
'fooBAR012', $bag->getAlnum(
'word'),
'->getAlnum() gets only alphanumeric characters');
109 $this->assertEquals(
'', $bag->getAlnum(
'unknown'),
'->getAlnum() returns empty string if a parameter is not defined');
114 $bag =
new ParameterBag(array(
'word' =>
'foo_BAR_012'));
116 $this->assertEquals(
'012', $bag->getDigits(
'word'),
'->getDigits() gets only digits as string');
117 $this->assertEquals(
'', $bag->getDigits(
'unknown'),
'->getDigits() returns empty string if a parameter is not defined');
124 $this->assertEquals(123, $bag->getInt(
'digits'),
'->getInt() gets a value of parameter as integer');
125 $this->assertEquals(0, $bag->getInt(
'unknown'),
'->getInt() returns zero if a parameter is not defined');
131 'digits' =>
'0123ab',
132 'email' =>
'example@example.com',
133 'url' =>
'http://example.com/foo',
136 'array' => array(
'bang'),
139 $this->assertEmpty($bag->filter(
'nokey'),
'->filter() should return empty by default if no key is found');
141 $this->assertEquals(
'0123', $bag->filter(
'digits',
'', FILTER_SANITIZE_NUMBER_INT),
'->filter() gets a value of parameter as integer filtering out invalid characters');
143 $this->assertEquals(
'example@example.com', $bag->filter(
'email',
'', FILTER_VALIDATE_EMAIL),
'->filter() gets a value of parameter as email');
145 $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');
148 $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');
150 $this->assertFalse($bag->filter(
'dec',
'', FILTER_VALIDATE_INT, array(
151 'flags' => FILTER_FLAG_ALLOW_HEX,
152 'options' => array(
'min_range' => 1,
'max_range' => 0xff),
153 )),
'->filter() gets a value of parameter as integer between boundaries');
155 $this->assertFalse($bag->filter(
'hex',
'', FILTER_VALIDATE_INT, array(
156 'flags' => FILTER_FLAG_ALLOW_HEX,
157 'options' => array(
'min_range' => 1,
'max_range' => 0xff),
158 )),
'->filter() gets a value of parameter as integer between boundaries');
160 $this->assertEquals(array(
'bang'), $bag->filter(
'array',
''),
'->filter() gets a value of parameter as an array');
165 $parameters = array(
'foo' =>
'bar',
'hello' =>
'world');
169 foreach ($bag as $key => $val) {
171 $this->assertEquals($parameters[$key], $val);
174 $this->assertEquals(count($parameters), $i);
179 $parameters = array(
'foo' =>
'bar',
'hello' =>
'world');
182 $this->assertEquals(count($parameters), count($bag));
187 $parameters = array(
'string_true' =>
'true',
'string_false' =>
'false');
190 $this->assertTrue($bag->getBoolean(
'string_true'),
'->getBoolean() gets the string true as boolean true');
191 $this->assertFalse($bag->getBoolean(
'string_false'),
'->getBoolean() gets the string false as boolean false');
192 $this->assertFalse($bag->getBoolean(
'unknown'),
'->getBoolean() returns false if a parameter is not defined');