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\NamespacedAttributeBag;
15:
16: 17: 18: 19: 20:
21: class NamespacedAttributeBagTest 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 NamespacedAttributeBag('_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 NamespacedAttributeBag();
62: $bag->initialize($this->array);
63: $this->assertEquals($this->array, $this->bag->all());
64: $array = array('should' => 'not stick');
65: $bag->initialize($array);
66:
67:
68: $this->assertEquals($this->array, $this->bag->all());
69: }
70:
71: public function testGetStorageKey()
72: {
73: $this->assertEquals('_sf2', $this->bag->getStorageKey());
74: $attributeBag = new NamespacedAttributeBag('test');
75: $this->assertEquals('test', $attributeBag->getStorageKey());
76: }
77:
78: 79: 80:
81: public function testHas($key, $value, $exists)
82: {
83: $this->assertEquals($exists, $this->bag->has($key));
84: }
85:
86: 87: 88:
89: public function testGet($key, $value, $expected)
90: {
91: $this->assertEquals($value, $this->bag->get($key));
92: }
93:
94: public function testGetDefaults()
95: {
96: $this->assertNull($this->bag->get('user2.login'));
97: $this->assertEquals('default', $this->bag->get('user2.login', 'default'));
98: }
99:
100: 101: 102:
103: public function testSet($key, $value, $expected)
104: {
105: $this->bag->set($key, $value);
106: $this->assertEquals($value, $this->bag->get($key));
107: }
108:
109: public function testAll()
110: {
111: $this->assertEquals($this->array, $this->bag->all());
112:
113: $this->bag->set('hello', 'fabien');
114: $array = $this->array;
115: $array['hello'] = 'fabien';
116: $this->assertEquals($array, $this->bag->all());
117: }
118:
119: public function testReplace()
120: {
121: $array = array();
122: $array['name'] = 'jack';
123: $array['foo.bar'] = 'beep';
124: $this->bag->replace($array);
125: $this->assertEquals($array, $this->bag->all());
126: $this->assertNull($this->bag->get('hello'));
127: $this->assertNull($this->bag->get('always'));
128: $this->assertNull($this->bag->get('user.login'));
129: }
130:
131: public function testRemove()
132: {
133: $this->assertEquals('world', $this->bag->get('hello'));
134: $this->bag->remove('hello');
135: $this->assertNull($this->bag->get('hello'));
136:
137: $this->assertEquals('be happy', $this->bag->get('always'));
138: $this->bag->remove('always');
139: $this->assertNull($this->bag->get('always'));
140:
141: $this->assertEquals('drak', $this->bag->get('user.login'));
142: $this->bag->remove('user.login');
143: $this->assertNull($this->bag->get('user.login'));
144: }
145:
146: public function testRemoveExistingNamespacedAttribute()
147: {
148: $this->assertSame('cod', $this->bag->remove('category/fishing/first'));
149: }
150:
151: public function testRemoveNonexistingNamespacedAttribute()
152: {
153: $this->assertNull($this->bag->remove('foo/bar/baz'));
154: }
155:
156: public function testClear()
157: {
158: $this->bag->clear();
159: $this->assertEquals(array(), $this->bag->all());
160: }
161:
162: public function attributesProvider()
163: {
164: return array(
165: array('hello', 'world', true),
166: array('always', 'be happy', true),
167: array('user.login', 'drak', true),
168: array('csrf.token', array('a' => '1234', 'b' => '4321'), true),
169: array('csrf.token/a', '1234', true),
170: array('csrf.token/b', '4321', true),
171: array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true),
172: array('category/fishing', array('first' => 'cod', 'second' => 'sole'), true),
173: array('category/fishing/missing/first', null, false),
174: array('category/fishing/first', 'cod', true),
175: array('category/fishing/second', 'sole', true),
176: array('category/fishing/missing/second', null, false),
177: array('user2.login', null, false),
178: array('never', null, false),
179: array('bye', null, false),
180: array('bye/for/now', null, false),
181: );
182: }
183: }
184: