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\AttributeBag;
20 * @author Drak <drak@zikula.org>
22 class AttributeBagTest extends TestCase
24 private $array = array();
31 protected function setUp()
35 'always' => 'be happy',
36 'user.login' => 'drak',
37 'csrf.token' => array(
48 $this->bag = new AttributeBag('_sf2');
49 $this->bag->initialize($this->array);
52 protected function tearDown()
55 $this->array = array();
58 public function testInitialize()
60 $bag = new AttributeBag();
61 $bag->initialize($this->array);
62 $this->assertEquals($this->array, $bag->all());
63 $array = array('should' => 'change');
64 $bag->initialize($array);
65 $this->assertEquals($array, $bag->all());
68 public function testGetStorageKey()
70 $this->assertEquals('_sf2', $this->bag->getStorageKey());
71 $attributeBag = new AttributeBag('test');
72 $this->assertEquals('test', $attributeBag->getStorageKey());
75 public function testGetSetName()
77 $this->assertEquals('attributes', $this->bag->getName());
78 $this->bag->setName('foo');
79 $this->assertEquals('foo', $this->bag->getName());
83 * @dataProvider attributesProvider
85 public function testHas($key, $value, $exists)
87 $this->assertEquals($exists, $this->bag->has($key));
91 * @dataProvider attributesProvider
93 public function testGet($key, $value, $expected)
95 $this->assertEquals($value, $this->bag->get($key));
98 public function testGetDefaults()
100 $this->assertNull($this->bag->get('user2.login'));
101 $this->assertEquals('default', $this->bag->get('user2.login', 'default'));
105 * @dataProvider attributesProvider
107 public function testSet($key, $value, $expected)
109 $this->bag->set($key, $value);
110 $this->assertEquals($value, $this->bag->get($key));
113 public function testAll()
115 $this->assertEquals($this->array, $this->bag->all());
117 $this->bag->set('hello', 'fabien');
118 $array = $this->array;
119 $array['hello'] = 'fabien';
120 $this->assertEquals($array, $this->bag->all());
123 public function testReplace()
126 $array['name'] = 'jack';
127 $array['foo.bar'] = 'beep';
128 $this->bag->replace($array);
129 $this->assertEquals($array, $this->bag->all());
130 $this->assertNull($this->bag->get('hello'));
131 $this->assertNull($this->bag->get('always'));
132 $this->assertNull($this->bag->get('user.login'));
135 public function testRemove()
137 $this->assertEquals('world', $this->bag->get('hello'));
138 $this->bag->remove('hello');
139 $this->assertNull($this->bag->get('hello'));
141 $this->assertEquals('be happy', $this->bag->get('always'));
142 $this->bag->remove('always');
143 $this->assertNull($this->bag->get('always'));
145 $this->assertEquals('drak', $this->bag->get('user.login'));
146 $this->bag->remove('user.login');
147 $this->assertNull($this->bag->get('user.login'));
150 public function testClear()
153 $this->assertEquals(array(), $this->bag->all());
156 public function attributesProvider()
159 array('hello', 'world', true),
160 array('always', 'be happy', true),
161 array('user.login', 'drak', true),
162 array('csrf.token', array('a' => '1234', 'b' => '4321'), true),
163 array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true),
164 array('user2.login', null, false),
165 array('never', null, false),
166 array('bye', null, false),
167 array('bye/for/now', null, false),
171 public function testGetIterator()
174 foreach ($this->bag as $key => $val) {
175 $this->assertEquals($this->array[$key], $val);
179 $this->assertEquals(count($this->array), $i);
182 public function testCount()
184 $this->assertCount(count($this->array), $this->bag);