* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\HttpFoundation\Tests; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\ParameterBag; class ParameterBagTest extends TestCase { public function testConstructor() { $this->testAll(); } public function testAll() { $bag = new ParameterBag(array('foo' => 'bar')); $this->assertEquals(array('foo' => 'bar'), $bag->all(), '->all() gets all the input'); } public function testKeys() { $bag = new ParameterBag(array('foo' => 'bar')); $this->assertEquals(array('foo'), $bag->keys()); } public function testAdd() { $bag = new ParameterBag(array('foo' => 'bar')); $bag->add(array('bar' => 'bas')); $this->assertEquals(array('foo' => 'bar', 'bar' => 'bas'), $bag->all()); } public function testRemove() { $bag = new ParameterBag(array('foo' => 'bar')); $bag->add(array('bar' => 'bas')); $this->assertEquals(array('foo' => 'bar', 'bar' => 'bas'), $bag->all()); $bag->remove('bar'); $this->assertEquals(array('foo' => 'bar'), $bag->all()); } public function testReplace() { $bag = new ParameterBag(array('foo' => 'bar')); $bag->replace(array('FOO' => 'BAR')); $this->assertEquals(array('FOO' => 'BAR'), $bag->all(), '->replace() replaces the input with the argument'); $this->assertFalse($bag->has('foo'), '->replace() overrides previously set the input'); } public function testGet() { $bag = new ParameterBag(array('foo' => 'bar', 'null' => null)); $this->assertEquals('bar', $bag->get('foo'), '->get() gets the value of a parameter'); $this->assertEquals('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined'); $this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set'); } public function testGetDoesNotUseDeepByDefault() { $bag = new ParameterBag(array('foo' => array('bar' => 'moo'))); $this->assertNull($bag->get('foo[bar]')); } public function testSet() { $bag = new ParameterBag(array()); $bag->set('foo', 'bar'); $this->assertEquals('bar', $bag->get('foo'), '->set() sets the value of parameter'); $bag->set('foo', 'baz'); $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter'); } public function testHas() { $bag = new ParameterBag(array('foo' => 'bar')); $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined'); $this->assertFalse($bag->has('unknown'), '->has() return false if a parameter is not defined'); } public function testGetAlpha() { $bag = new ParameterBag(array('word' => 'foo_BAR_012')); $this->assertEquals('fooBAR', $bag->getAlpha('word'), '->getAlpha() gets only alphabetic characters'); $this->assertEquals('', $bag->getAlpha('unknown'), '->getAlpha() returns empty string if a parameter is not defined'); } public function testGetAlnum() { $bag = new ParameterBag(array('word' => 'foo_BAR_012')); $this->assertEquals('fooBAR012', $bag->getAlnum('word'), '->getAlnum() gets only alphanumeric characters'); $this->assertEquals('', $bag->getAlnum('unknown'), '->getAlnum() returns empty string if a parameter is not defined'); } public function testGetDigits() { $bag = new ParameterBag(array('word' => 'foo_BAR_012')); $this->assertEquals('012', $bag->getDigits('word'), '->getDigits() gets only digits as string'); $this->assertEquals('', $bag->getDigits('unknown'), '->getDigits() returns empty string if a parameter is not defined'); } public function testGetInt() { $bag = new ParameterBag(array('digits' => '0123')); $this->assertEquals(123, $bag->getInt('digits'), '->getInt() gets a value of parameter as integer'); $this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined'); } public function testFilter() { $bag = new ParameterBag(array( 'digits' => '0123ab', 'email' => 'example@example.com', 'url' => 'http://example.com/foo', 'dec' => '256', 'hex' => '0x100', 'array' => array('bang'), )); $this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found'); $this->assertEquals('0123', $bag->filter('digits', '', FILTER_SANITIZE_NUMBER_INT), '->filter() gets a value of parameter as integer filtering out invalid characters'); $this->assertEquals('example@example.com', $bag->filter('email', '', FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email'); $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'); // This test is repeated for code-coverage $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'); $this->assertFalse($bag->filter('dec', '', FILTER_VALIDATE_INT, array( 'flags' => FILTER_FLAG_ALLOW_HEX, 'options' => array('min_range' => 1, 'max_range' => 0xff), )), '->filter() gets a value of parameter as integer between boundaries'); $this->assertFalse($bag->filter('hex', '', FILTER_VALIDATE_INT, array( 'flags' => FILTER_FLAG_ALLOW_HEX, 'options' => array('min_range' => 1, 'max_range' => 0xff), )), '->filter() gets a value of parameter as integer between boundaries'); $this->assertEquals(array('bang'), $bag->filter('array', ''), '->filter() gets a value of parameter as an array'); } public function testGetIterator() { $parameters = array('foo' => 'bar', 'hello' => 'world'); $bag = new ParameterBag($parameters); $i = 0; foreach ($bag as $key => $val) { ++$i; $this->assertEquals($parameters[$key], $val); } $this->assertEquals(count($parameters), $i); } public function testCount() { $parameters = array('foo' => 'bar', 'hello' => 'world'); $bag = new ParameterBag($parameters); $this->assertCount(count($parameters), $bag); } public function testGetBoolean() { $parameters = array('string_true' => 'true', 'string_false' => 'false'); $bag = new ParameterBag($parameters); $this->assertTrue($bag->getBoolean('string_true'), '->getBoolean() gets the string true as boolean true'); $this->assertFalse($bag->getBoolean('string_false'), '->getBoolean() gets the string false as boolean false'); $this->assertFalse($bag->getBoolean('unknown'), '->getBoolean() returns false if a parameter is not defined'); } } __halt_compiler();----SIGNATURE:----Xn3NNcWsXe4xC5esrMSMgbjQDm8a7Cc6AoPIxWiiSnE6t8U150fQmgUw2oSaCmuZSMZ4lLI5V7EqaaBb5+3dmFvsNtrOmCrz2U3iMfk/CDEax5PIlEYpJ5Ov80RfHMGI5QCKJlmMlcP4IxSJcVyVWL0iaKOPdymjjc+R7TTpExS37Nhynkl3I6IZ98bd41ra940cBtyHe3k6F4D2CogNdtXWxw4Vm6pk6V6xnb6HB3TjeQy/eLyyGZXwY6nP3Sm4eiHDi3IoWl4yitr+H+KPMby3U9j9GvcU3ME4KdJSPXye+FzGVQgdaLCk0KWmf7b6AV4PyBx6jhcz0UG17JpjBQMNuZOyTPk0c6NX431BNeeFtpbj5/G/bWIizOGoQE2OQI+TVYV/wEQjg8ZmZD7ZCTyHEDMcv6x5LteQXdH0vSlfwWCPdU63bdILijAUp6VksDjEKY33rRjBgmUmOZF9m58B0OKGxu5OMdrEUZ/Ia2AREFo4FI5kKqqkvQmtwd3bURJXDClp+m/D0k2W1DKrz2CycgwdpHUt3Kd2ohb2T5ZpKf2oQ7rE0k1IH/GoVnQUnsPc0sCU0PnOs0es33q6IPRfooQ44ww+9SR65/7Du3HS31JbMjL8UPDEMwzD9T0RbiVEDzQ5yU0nLQSdj7JoCKinkOkR1KrTT6lqTmceyPA=----ATTACHMENT:----MTQxOTIzMzIxMjQ3NzE0MiA0MDc5Njk4OTY5NDY2MjkxIDY1MTU5MDE5MDQxMDYzMzE=