4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests\Session\Attribute;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
18 * Tests NamespacedAttributeBag.
20 * @author Drak <drak@zikula.org>
22 class NamespacedAttributeBagTest extends TestCase
30 * @var NamespacedAttributeBag
34 protected function setUp()
38 'always' => 'be happy',
39 'user.login' => 'drak',
40 'csrf.token' => array(
51 $this->bag = new NamespacedAttributeBag('_sf2', '/');
52 $this->bag->initialize($this->array);
55 protected function tearDown()
58 $this->array = array();
61 public function testInitialize()
63 $bag = new NamespacedAttributeBag();
64 $bag->initialize($this->array);
65 $this->assertEquals($this->array, $this->bag->all());
66 $array = array('should' => 'not stick');
67 $bag->initialize($array);
69 // should have remained the same
70 $this->assertEquals($this->array, $this->bag->all());
73 public function testGetStorageKey()
75 $this->assertEquals('_sf2', $this->bag->getStorageKey());
76 $attributeBag = new NamespacedAttributeBag('test');
77 $this->assertEquals('test', $attributeBag->getStorageKey());
81 * @dataProvider attributesProvider
83 public function testHas($key, $value, $exists)
85 $this->assertEquals($exists, $this->bag->has($key));
89 * @dataProvider attributesProvider
91 public function testGet($key, $value, $expected)
93 $this->assertEquals($value, $this->bag->get($key));
96 public function testGetDefaults()
98 $this->assertNull($this->bag->get('user2.login'));
99 $this->assertEquals('default', $this->bag->get('user2.login', 'default'));
103 * @dataProvider attributesProvider
105 public function testSet($key, $value, $expected)
107 $this->bag->set($key, $value);
108 $this->assertEquals($value, $this->bag->get($key));
111 public function testAll()
113 $this->assertEquals($this->array, $this->bag->all());
115 $this->bag->set('hello', 'fabien');
116 $array = $this->array;
117 $array['hello'] = 'fabien';
118 $this->assertEquals($array, $this->bag->all());
121 public function testReplace()
124 $array['name'] = 'jack';
125 $array['foo.bar'] = 'beep';
126 $this->bag->replace($array);
127 $this->assertEquals($array, $this->bag->all());
128 $this->assertNull($this->bag->get('hello'));
129 $this->assertNull($this->bag->get('always'));
130 $this->assertNull($this->bag->get('user.login'));
133 public function testRemove()
135 $this->assertEquals('world', $this->bag->get('hello'));
136 $this->bag->remove('hello');
137 $this->assertNull($this->bag->get('hello'));
139 $this->assertEquals('be happy', $this->bag->get('always'));
140 $this->bag->remove('always');
141 $this->assertNull($this->bag->get('always'));
143 $this->assertEquals('drak', $this->bag->get('user.login'));
144 $this->bag->remove('user.login');
145 $this->assertNull($this->bag->get('user.login'));
148 public function testRemoveExistingNamespacedAttribute()
150 $this->assertSame('cod', $this->bag->remove('category/fishing/first'));
153 public function testRemoveNonexistingNamespacedAttribute()
155 $this->assertNull($this->bag->remove('foo/bar/baz'));
158 public function testClear()
161 $this->assertEquals(array(), $this->bag->all());
164 public function attributesProvider()
167 array('hello', 'world', true),
168 array('always', 'be happy', true),
169 array('user.login', 'drak', true),
170 array('csrf.token', array('a' => '1234', 'b' => '4321'), true),
171 array('csrf.token/a', '1234', true),
172 array('csrf.token/b', '4321', true),
173 array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true),
174 array('category/fishing', array('first' => 'cod', 'second' => 'sole'), true),
175 array('category/fishing/missing/first', null, false),
176 array('category/fishing/first', 'cod', true),
177 array('category/fishing/second', 'sole', true),
178 array('category/fishing/missing/second', null, false),
179 array('user2.login', null, false),
180 array('never', null, false),
181 array('bye', null, false),
182 array('bye/for/now', null, false),