724a0b9844700d79dae4c6842e58b304c07ac3be
[yaffs-website] / vendor / symfony / http-foundation / Tests / Session / Attribute / AttributeBagTest.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\AttributeBag;
16
17 /**
18  * Tests AttributeBag.
19  *
20  * @author Drak <drak@zikula.org>
21  */
22 class AttributeBagTest extends TestCase
23 {
24     private $array = array();
25
26     /**
27      * @var AttributeBag
28      */
29     private $bag;
30
31     protected function setUp()
32     {
33         $this->array = array(
34             'hello' => 'world',
35             'always' => 'be happy',
36             'user.login' => 'drak',
37             'csrf.token' => array(
38                 'a' => '1234',
39                 'b' => '4321',
40             ),
41             'category' => array(
42                 'fishing' => array(
43                     'first' => 'cod',
44                     'second' => 'sole',
45                 ),
46             ),
47         );
48         $this->bag = new AttributeBag('_sf2');
49         $this->bag->initialize($this->array);
50     }
51
52     protected function tearDown()
53     {
54         $this->bag = null;
55         $this->array = array();
56     }
57
58     public function testInitialize()
59     {
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());
66     }
67
68     public function testGetStorageKey()
69     {
70         $this->assertEquals('_sf2', $this->bag->getStorageKey());
71         $attributeBag = new AttributeBag('test');
72         $this->assertEquals('test', $attributeBag->getStorageKey());
73     }
74
75     public function testGetSetName()
76     {
77         $this->assertEquals('attributes', $this->bag->getName());
78         $this->bag->setName('foo');
79         $this->assertEquals('foo', $this->bag->getName());
80     }
81
82     /**
83      * @dataProvider attributesProvider
84      */
85     public function testHas($key, $value, $exists)
86     {
87         $this->assertEquals($exists, $this->bag->has($key));
88     }
89
90     /**
91      * @dataProvider attributesProvider
92      */
93     public function testGet($key, $value, $expected)
94     {
95         $this->assertEquals($value, $this->bag->get($key));
96     }
97
98     public function testGetDefaults()
99     {
100         $this->assertNull($this->bag->get('user2.login'));
101         $this->assertEquals('default', $this->bag->get('user2.login', 'default'));
102     }
103
104     /**
105      * @dataProvider attributesProvider
106      */
107     public function testSet($key, $value, $expected)
108     {
109         $this->bag->set($key, $value);
110         $this->assertEquals($value, $this->bag->get($key));
111     }
112
113     public function testAll()
114     {
115         $this->assertEquals($this->array, $this->bag->all());
116
117         $this->bag->set('hello', 'fabien');
118         $array = $this->array;
119         $array['hello'] = 'fabien';
120         $this->assertEquals($array, $this->bag->all());
121     }
122
123     public function testReplace()
124     {
125         $array = array();
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'));
133     }
134
135     public function testRemove()
136     {
137         $this->assertEquals('world', $this->bag->get('hello'));
138         $this->bag->remove('hello');
139         $this->assertNull($this->bag->get('hello'));
140
141         $this->assertEquals('be happy', $this->bag->get('always'));
142         $this->bag->remove('always');
143         $this->assertNull($this->bag->get('always'));
144
145         $this->assertEquals('drak', $this->bag->get('user.login'));
146         $this->bag->remove('user.login');
147         $this->assertNull($this->bag->get('user.login'));
148     }
149
150     public function testClear()
151     {
152         $this->bag->clear();
153         $this->assertEquals(array(), $this->bag->all());
154     }
155
156     public function attributesProvider()
157     {
158         return array(
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),
168         );
169     }
170
171     public function testGetIterator()
172     {
173         $i = 0;
174         foreach ($this->bag as $key => $val) {
175             $this->assertEquals($this->array[$key], $val);
176             ++$i;
177         }
178
179         $this->assertEquals(count($this->array), $i);
180     }
181
182     public function testCount()
183     {
184         $this->assertCount(count($this->array), $this->bag);
185     }
186 }