1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\HttpFoundation\Tests\Session\Attribute;
13:
14: use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
15:
16: 17: 18: 19: 20:
21: class AttributeBagTest extends \PHPUnit_Framework_TestCase
22: {
23: 24: 25:
26: private $array;
27:
28: 29: 30:
31: private $bag;
32:
33: protected function setUp()
34: {
35: $this->array = array(
36: 'hello' => 'world',
37: 'always' => 'be happy',
38: 'user.login' => 'drak',
39: 'csrf.token' => array(
40: 'a' => '1234',
41: 'b' => '4321',
42: ),
43: 'category' => array(
44: 'fishing' => array(
45: 'first' => 'cod',
46: 'second' => 'sole',),
47: ),
48: );
49: $this->bag = new AttributeBag('_sf2');
50: $this->bag->initialize($this->array);
51: }
52:
53: protected function tearDown()
54: {
55: $this->bag = null;
56: $this->array = array();
57: }
58:
59: public function testInitialize()
60: {
61: $bag = new AttributeBag();
62: $bag->initialize($this->array);
63: $this->assertEquals($this->array, $bag->all());
64: $array = array('should' => 'change');
65: $bag->initialize($array);
66: $this->assertEquals($array, $bag->all());
67: }
68:
69: public function testGetStorageKey()
70: {
71: $this->assertEquals('_sf2', $this->bag->getStorageKey());
72: $attributeBag = new AttributeBag('test');
73: $this->assertEquals('test', $attributeBag->getStorageKey());
74: }
75:
76: public function testGetSetName()
77: {
78: $this->assertEquals('attributes', $this->bag->getName());
79: $this->bag->setName('foo');
80: $this->assertEquals('foo', $this->bag->getName());
81: }
82:
83: 84: 85:
86: public function testHas($key, $value, $exists)
87: {
88: $this->assertEquals($exists, $this->bag->has($key));
89: }
90:
91: 92: 93:
94: public function testGet($key, $value, $expected)
95: {
96: $this->assertEquals($value, $this->bag->get($key));
97: }
98:
99: public function testGetDefaults()
100: {
101: $this->assertNull($this->bag->get('user2.login'));
102: $this->assertEquals('default', $this->bag->get('user2.login', 'default'));
103: }
104:
105: 106: 107:
108: public function testSet($key, $value, $expected)
109: {
110: $this->bag->set($key, $value);
111: $this->assertEquals($value, $this->bag->get($key));
112: }
113:
114: public function testAll()
115: {
116: $this->assertEquals($this->array, $this->bag->all());
117:
118: $this->bag->set('hello', 'fabien');
119: $array = $this->array;
120: $array['hello'] = 'fabien';
121: $this->assertEquals($array, $this->bag->all());
122: }
123:
124: public function testReplace()
125: {
126: $array = array();
127: $array['name'] = 'jack';
128: $array['foo.bar'] = 'beep';
129: $this->bag->replace($array);
130: $this->assertEquals($array, $this->bag->all());
131: $this->assertNull($this->bag->get('hello'));
132: $this->assertNull($this->bag->get('always'));
133: $this->assertNull($this->bag->get('user.login'));
134: }
135:
136: public function testRemove()
137: {
138: $this->assertEquals('world', $this->bag->get('hello'));
139: $this->bag->remove('hello');
140: $this->assertNull($this->bag->get('hello'));
141:
142: $this->assertEquals('be happy', $this->bag->get('always'));
143: $this->bag->remove('always');
144: $this->assertNull($this->bag->get('always'));
145:
146: $this->assertEquals('drak', $this->bag->get('user.login'));
147: $this->bag->remove('user.login');
148: $this->assertNull($this->bag->get('user.login'));
149: }
150:
151: public function testClear()
152: {
153: $this->bag->clear();
154: $this->assertEquals(array(), $this->bag->all());
155: }
156:
157: public function attributesProvider()
158: {
159: return array(
160: array('hello', 'world', true),
161: array('always', 'be happy', true),
162: array('user.login', 'drak', true),
163: array('csrf.token', array('a' => '1234', 'b' => '4321'), true),
164: array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true),
165: array('user2.login', null, false),
166: array('never', null, false),
167: array('bye', null, false),
168: array('bye/for/now', null, false),
169: );
170: }
171:
172: 173: 174:
175: public function testGetIterator()
176: {
177: $i = 0;
178: foreach ($this->bag as $key => $val) {
179: $this->assertEquals($this->array[$key], $val);
180: $i++;
181: }
182:
183: $this->assertEquals(count($this->array), $i);
184: }
185:
186: 187: 188:
189: public function testCount()
190: {
191: $this->assertEquals(count($this->array), count($this->bag));
192: }
193: }
194: