Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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\Attribute\AttributeBag;
16 use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
17 use Symfony\Component\HttpFoundation\Session\Session;
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 testSetIdAfterStart()
74     {
75         $this->session->start();
76         $id = $this->session->getId();
77
78         $e = null;
79         try {
80             $this->session->setId($id);
81         } catch (\Exception $e) {
82         }
83
84         $this->assertNull($e);
85
86         try {
87             $this->session->setId('different');
88         } catch (\Exception $e) {
89         }
90
91         $this->assertInstanceOf('\LogicException', $e);
92     }
93
94     public function testSetName()
95     {
96         $this->assertEquals('MOCKSESSID', $this->session->getName());
97         $this->session->setName('session.test.com');
98         $this->session->start();
99         $this->assertEquals('session.test.com', $this->session->getName());
100     }
101
102     public function testGet()
103     {
104         // tests defaults
105         $this->assertNull($this->session->get('foo'));
106         $this->assertEquals(1, $this->session->get('foo', 1));
107     }
108
109     /**
110      * @dataProvider setProvider
111      */
112     public function testSet($key, $value)
113     {
114         $this->session->set($key, $value);
115         $this->assertEquals($value, $this->session->get($key));
116     }
117
118     /**
119      * @dataProvider setProvider
120      */
121     public function testHas($key, $value)
122     {
123         $this->session->set($key, $value);
124         $this->assertTrue($this->session->has($key));
125         $this->assertFalse($this->session->has($key.'non_value'));
126     }
127
128     public function testReplace()
129     {
130         $this->session->replace(array('happiness' => 'be good', 'symfony' => 'awesome'));
131         $this->assertEquals(array('happiness' => 'be good', 'symfony' => 'awesome'), $this->session->all());
132         $this->session->replace(array());
133         $this->assertEquals(array(), $this->session->all());
134     }
135
136     /**
137      * @dataProvider setProvider
138      */
139     public function testAll($key, $value, $result)
140     {
141         $this->session->set($key, $value);
142         $this->assertEquals($result, $this->session->all());
143     }
144
145     /**
146      * @dataProvider setProvider
147      */
148     public function testClear($key, $value)
149     {
150         $this->session->set('hi', 'fabien');
151         $this->session->set($key, $value);
152         $this->session->clear();
153         $this->assertEquals(array(), $this->session->all());
154     }
155
156     public function setProvider()
157     {
158         return array(
159             array('foo', 'bar', array('foo' => 'bar')),
160             array('foo.bar', 'too much beer', array('foo.bar' => 'too much beer')),
161             array('great', 'symfony is great', array('great' => 'symfony is great')),
162         );
163     }
164
165     /**
166      * @dataProvider setProvider
167      */
168     public function testRemove($key, $value)
169     {
170         $this->session->set('hi.world', 'have a nice day');
171         $this->session->set($key, $value);
172         $this->session->remove($key);
173         $this->assertEquals(array('hi.world' => 'have a nice day'), $this->session->all());
174     }
175
176     public function testInvalidate()
177     {
178         $this->session->set('invalidate', 123);
179         $this->session->invalidate();
180         $this->assertEquals(array(), $this->session->all());
181     }
182
183     public function testMigrate()
184     {
185         $this->session->set('migrate', 321);
186         $this->session->migrate();
187         $this->assertEquals(321, $this->session->get('migrate'));
188     }
189
190     public function testMigrateDestroy()
191     {
192         $this->session->set('migrate', 333);
193         $this->session->migrate(true);
194         $this->assertEquals(333, $this->session->get('migrate'));
195     }
196
197     public function testSave()
198     {
199         $this->session->start();
200         $this->session->save();
201
202         $this->assertFalse($this->session->isStarted());
203     }
204
205     public function testGetId()
206     {
207         $this->assertEquals('', $this->session->getId());
208         $this->session->start();
209         $this->assertNotEquals('', $this->session->getId());
210     }
211
212     public function testGetFlashBag()
213     {
214         $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface', $this->session->getFlashBag());
215     }
216
217     public function testGetIterator()
218     {
219         $attributes = array('hello' => 'world', 'symfony' => 'rocks');
220         foreach ($attributes as $key => $val) {
221             $this->session->set($key, $val);
222         }
223
224         $i = 0;
225         foreach ($this->session as $key => $val) {
226             $this->assertEquals($attributes[$key], $val);
227             ++$i;
228         }
229
230         $this->assertEquals(\count($attributes), $i);
231     }
232
233     public function testGetCount()
234     {
235         $this->session->set('hello', 'world');
236         $this->session->set('symfony', 'rocks');
237
238         $this->assertCount(2, $this->session);
239     }
240
241     public function testGetMeta()
242     {
243         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\MetadataBag', $this->session->getMetadataBag());
244     }
245
246     public function testIsEmpty()
247     {
248         $this->assertTrue($this->session->isEmpty());
249
250         $this->session->set('hello', 'world');
251         $this->assertFalse($this->session->isEmpty());
252
253         $this->session->remove('hello');
254         $this->assertTrue($this->session->isEmpty());
255
256         $flash = $this->session->getFlashBag();
257         $flash->set('hello', 'world');
258         $this->assertFalse($this->session->isEmpty());
259
260         $flash->get('hello');
261         $this->assertTrue($this->session->isEmpty());
262     }
263 }