Version 1
[yaffs-website] / vendor / symfony / http-foundation / Tests / Session / Attribute / NamespacedAttributeBagTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpFoundation\Tests\Session\Attribute;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
16
17 /**
18  * Tests NamespacedAttributeBag.
19  *
20  * @author Drak <drak@zikula.org>
21  */
22 class NamespacedAttributeBagTest extends TestCase
23 {
24     /**
25      * @var array
26      */
27     private $array;
28
29     /**
30      * @var NamespacedAttributeBag
31      */
32     private $bag;
33
34     protected function setUp()
35     {
36         $this->array = array(
37             'hello' => 'world',
38             'always' => 'be happy',
39             'user.login' => 'drak',
40             'csrf.token' => array(
41                 'a' => '1234',
42                 'b' => '4321',
43             ),
44             'category' => array(
45                 'fishing' => array(
46                     'first' => 'cod',
47                     'second' => 'sole',
48                 ),
49             ),
50         );
51         $this->bag = new NamespacedAttributeBag('_sf2', '/');
52         $this->bag->initialize($this->array);
53     }
54
55     protected function tearDown()
56     {
57         $this->bag = null;
58         $this->array = array();
59     }
60
61     public function testInitialize()
62     {
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);
68
69         // should have remained the same
70         $this->assertEquals($this->array, $this->bag->all());
71     }
72
73     public function testGetStorageKey()
74     {
75         $this->assertEquals('_sf2', $this->bag->getStorageKey());
76         $attributeBag = new NamespacedAttributeBag('test');
77         $this->assertEquals('test', $attributeBag->getStorageKey());
78     }
79
80     /**
81      * @dataProvider attributesProvider
82      */
83     public function testHas($key, $value, $exists)
84     {
85         $this->assertEquals($exists, $this->bag->has($key));
86     }
87
88     /**
89      * @dataProvider attributesProvider
90      */
91     public function testGet($key, $value, $expected)
92     {
93         $this->assertEquals($value, $this->bag->get($key));
94     }
95
96     public function testGetDefaults()
97     {
98         $this->assertNull($this->bag->get('user2.login'));
99         $this->assertEquals('default', $this->bag->get('user2.login', 'default'));
100     }
101
102     /**
103      * @dataProvider attributesProvider
104      */
105     public function testSet($key, $value, $expected)
106     {
107         $this->bag->set($key, $value);
108         $this->assertEquals($value, $this->bag->get($key));
109     }
110
111     public function testAll()
112     {
113         $this->assertEquals($this->array, $this->bag->all());
114
115         $this->bag->set('hello', 'fabien');
116         $array = $this->array;
117         $array['hello'] = 'fabien';
118         $this->assertEquals($array, $this->bag->all());
119     }
120
121     public function testReplace()
122     {
123         $array = array();
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'));
131     }
132
133     public function testRemove()
134     {
135         $this->assertEquals('world', $this->bag->get('hello'));
136         $this->bag->remove('hello');
137         $this->assertNull($this->bag->get('hello'));
138
139         $this->assertEquals('be happy', $this->bag->get('always'));
140         $this->bag->remove('always');
141         $this->assertNull($this->bag->get('always'));
142
143         $this->assertEquals('drak', $this->bag->get('user.login'));
144         $this->bag->remove('user.login');
145         $this->assertNull($this->bag->get('user.login'));
146     }
147
148     public function testRemoveExistingNamespacedAttribute()
149     {
150         $this->assertSame('cod', $this->bag->remove('category/fishing/first'));
151     }
152
153     public function testRemoveNonexistingNamespacedAttribute()
154     {
155         $this->assertNull($this->bag->remove('foo/bar/baz'));
156     }
157
158     public function testClear()
159     {
160         $this->bag->clear();
161         $this->assertEquals(array(), $this->bag->all());
162     }
163
164     public function attributesProvider()
165     {
166         return array(
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),
183         );
184     }
185 }