41720e4b6fc4edf4bc01a3d31ae0f918714cb1e0
[yaffs-website] / vendor / symfony / http-foundation / Tests / Session / SessionTest.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;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Session\Session;
16 use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
17 use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
18 use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
19
20 /**
21  * SessionTest.
22  *
23  * @author Fabien Potencier <fabien@symfony.com>
24  * @author Robert Schönthal <seroscho@googlemail.com>
25  * @author Drak <drak@zikula.org>
26  */
27 class SessionTest extends TestCase
28 {
29     /**
30      * @var \Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface
31      */
32     protected $storage;
33
34     /**
35      * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
36      */
37     protected $session;
38
39     protected function setUp()
40     {
41         $this->storage = new MockArraySessionStorage();
42         $this->session = new Session($this->storage, new AttributeBag(), new FlashBag());
43     }
44
45     protected function tearDown()
46     {
47         $this->storage = null;
48         $this->session = null;
49     }
50
51     public function testStart()
52     {
53         $this->assertEquals('', $this->session->getId());
54         $this->assertTrue($this->session->start());
55         $this->assertNotEquals('', $this->session->getId());
56     }
57
58     public function testIsStarted()
59     {
60         $this->assertFalse($this->session->isStarted());
61         $this->session->start();
62         $this->assertTrue($this->session->isStarted());
63     }
64
65     public function testSetId()
66     {
67         $this->assertEquals('', $this->session->getId());
68         $this->session->setId('0123456789abcdef');
69         $this->session->start();
70         $this->assertEquals('0123456789abcdef', $this->session->getId());
71     }
72
73     public function testSetName()
74     {
75         $this->assertEquals('MOCKSESSID', $this->session->getName());
76         $this->session->setName('session.test.com');
77         $this->session->start();
78         $this->assertEquals('session.test.com', $this->session->getName());
79     }
80
81     public function testGet()
82     {
83         // tests defaults
84         $this->assertNull($this->session->get('foo'));
85         $this->assertEquals(1, $this->session->get('foo', 1));
86     }
87
88     /**
89      * @dataProvider setProvider
90      */
91     public function testSet($key, $value)
92     {
93         $this->session->set($key, $value);
94         $this->assertEquals($value, $this->session->get($key));
95     }
96
97     /**
98      * @dataProvider setProvider
99      */
100     public function testHas($key, $value)
101     {
102         $this->session->set($key, $value);
103         $this->assertTrue($this->session->has($key));
104         $this->assertFalse($this->session->has($key.'non_value'));
105     }
106
107     public function testReplace()
108     {
109         $this->session->replace(array('happiness' => 'be good', 'symfony' => 'awesome'));
110         $this->assertEquals(array('happiness' => 'be good', 'symfony' => 'awesome'), $this->session->all());
111         $this->session->replace(array());
112         $this->assertEquals(array(), $this->session->all());
113     }
114
115     /**
116      * @dataProvider setProvider
117      */
118     public function testAll($key, $value, $result)
119     {
120         $this->session->set($key, $value);
121         $this->assertEquals($result, $this->session->all());
122     }
123
124     /**
125      * @dataProvider setProvider
126      */
127     public function testClear($key, $value)
128     {
129         $this->session->set('hi', 'fabien');
130         $this->session->set($key, $value);
131         $this->session->clear();
132         $this->assertEquals(array(), $this->session->all());
133     }
134
135     public function setProvider()
136     {
137         return array(
138             array('foo', 'bar', array('foo' => 'bar')),
139             array('foo.bar', 'too much beer', array('foo.bar' => 'too much beer')),
140             array('great', 'symfony is great', array('great' => 'symfony is great')),
141         );
142     }
143
144     /**
145      * @dataProvider setProvider
146      */
147     public function testRemove($key, $value)
148     {
149         $this->session->set('hi.world', 'have a nice day');
150         $this->session->set($key, $value);
151         $this->session->remove($key);
152         $this->assertEquals(array('hi.world' => 'have a nice day'), $this->session->all());
153     }
154
155     public function testInvalidate()
156     {
157         $this->session->set('invalidate', 123);
158         $this->session->invalidate();
159         $this->assertEquals(array(), $this->session->all());
160     }
161
162     public function testMigrate()
163     {
164         $this->session->set('migrate', 321);
165         $this->session->migrate();
166         $this->assertEquals(321, $this->session->get('migrate'));
167     }
168
169     public function testMigrateDestroy()
170     {
171         $this->session->set('migrate', 333);
172         $this->session->migrate(true);
173         $this->assertEquals(333, $this->session->get('migrate'));
174     }
175
176     public function testSave()
177     {
178         $this->session->start();
179         $this->session->save();
180
181         $this->assertFalse($this->session->isStarted());
182     }
183
184     public function testGetId()
185     {
186         $this->assertEquals('', $this->session->getId());
187         $this->session->start();
188         $this->assertNotEquals('', $this->session->getId());
189     }
190
191     public function testGetFlashBag()
192     {
193         $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface', $this->session->getFlashBag());
194     }
195
196     public function testGetIterator()
197     {
198         $attributes = array('hello' => 'world', 'symfony' => 'rocks');
199         foreach ($attributes as $key => $val) {
200             $this->session->set($key, $val);
201         }
202
203         $i = 0;
204         foreach ($this->session as $key => $val) {
205             $this->assertEquals($attributes[$key], $val);
206             ++$i;
207         }
208
209         $this->assertEquals(count($attributes), $i);
210     }
211
212     public function testGetCount()
213     {
214         $this->session->set('hello', 'world');
215         $this->session->set('symfony', 'rocks');
216
217         $this->assertCount(2, $this->session);
218     }
219
220     public function testGetMeta()
221     {
222         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\MetadataBag', $this->session->getMetadataBag());
223     }
224
225     public function testIsEmpty()
226     {
227         $this->assertTrue($this->session->isEmpty());
228
229         $this->session->set('hello', 'world');
230         $this->assertFalse($this->session->isEmpty());
231
232         $this->session->remove('hello');
233         $this->assertTrue($this->session->isEmpty());
234
235         $flash = $this->session->getFlashBag();
236         $flash->set('hello', 'world');
237         $this->assertFalse($this->session->isEmpty());
238
239         $flash->get('hello');
240         $this->assertTrue($this->session->isEmpty());
241     }
242 }