4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests\Session;
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;
23 * @author Fabien Potencier <fabien@symfony.com>
24 * @author Robert Schönthal <seroscho@googlemail.com>
25 * @author Drak <drak@zikula.org>
27 class SessionTest extends TestCase
30 * @var \Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface
35 * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
39 protected function setUp()
41 $this->storage = new MockArraySessionStorage();
42 $this->session = new Session($this->storage, new AttributeBag(), new FlashBag());
45 protected function tearDown()
47 $this->storage = null;
48 $this->session = null;
51 public function testStart()
53 $this->assertEquals('', $this->session->getId());
54 $this->assertTrue($this->session->start());
55 $this->assertNotEquals('', $this->session->getId());
58 public function testIsStarted()
60 $this->assertFalse($this->session->isStarted());
61 $this->session->start();
62 $this->assertTrue($this->session->isStarted());
65 public function testSetId()
67 $this->assertEquals('', $this->session->getId());
68 $this->session->setId('0123456789abcdef');
69 $this->session->start();
70 $this->assertEquals('0123456789abcdef', $this->session->getId());
73 public function testSetIdAfterStart()
75 $this->session->start();
76 $id = $this->session->getId();
80 $this->session->setId($id);
81 } catch (\Exception $e) {
84 $this->assertNull($e);
87 $this->session->setId('different');
88 } catch (\Exception $e) {
91 $this->assertInstanceOf('\LogicException', $e);
94 public function testSetName()
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());
102 public function testGet()
105 $this->assertNull($this->session->get('foo'));
106 $this->assertEquals(1, $this->session->get('foo', 1));
110 * @dataProvider setProvider
112 public function testSet($key, $value)
114 $this->session->set($key, $value);
115 $this->assertEquals($value, $this->session->get($key));
119 * @dataProvider setProvider
121 public function testHas($key, $value)
123 $this->session->set($key, $value);
124 $this->assertTrue($this->session->has($key));
125 $this->assertFalse($this->session->has($key.'non_value'));
128 public function testReplace()
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());
137 * @dataProvider setProvider
139 public function testAll($key, $value, $result)
141 $this->session->set($key, $value);
142 $this->assertEquals($result, $this->session->all());
146 * @dataProvider setProvider
148 public function testClear($key, $value)
150 $this->session->set('hi', 'fabien');
151 $this->session->set($key, $value);
152 $this->session->clear();
153 $this->assertEquals(array(), $this->session->all());
156 public function setProvider()
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')),
166 * @dataProvider setProvider
168 public function testRemove($key, $value)
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());
176 public function testInvalidate()
178 $this->session->set('invalidate', 123);
179 $this->session->invalidate();
180 $this->assertEquals(array(), $this->session->all());
183 public function testMigrate()
185 $this->session->set('migrate', 321);
186 $this->session->migrate();
187 $this->assertEquals(321, $this->session->get('migrate'));
190 public function testMigrateDestroy()
192 $this->session->set('migrate', 333);
193 $this->session->migrate(true);
194 $this->assertEquals(333, $this->session->get('migrate'));
197 public function testSave()
199 $this->session->start();
200 $this->session->save();
202 $this->assertFalse($this->session->isStarted());
205 public function testGetId()
207 $this->assertEquals('', $this->session->getId());
208 $this->session->start();
209 $this->assertNotEquals('', $this->session->getId());
212 public function testGetFlashBag()
214 $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface', $this->session->getFlashBag());
217 public function testGetIterator()
219 $attributes = array('hello' => 'world', 'symfony' => 'rocks');
220 foreach ($attributes as $key => $val) {
221 $this->session->set($key, $val);
225 foreach ($this->session as $key => $val) {
226 $this->assertEquals($attributes[$key], $val);
230 $this->assertEquals(\count($attributes), $i);
233 public function testGetCount()
235 $this->session->set('hello', 'world');
236 $this->session->set('symfony', 'rocks');
238 $this->assertCount(2, $this->session);
241 public function testGetMeta()
243 $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\MetadataBag', $this->session->getMetadataBag());
246 public function testIsEmpty()
248 $this->assertTrue($this->session->isEmpty());
250 $this->session->set('hello', 'world');
251 $this->assertFalse($this->session->isEmpty());
253 $this->session->remove('hello');
254 $this->assertTrue($this->session->isEmpty());
256 $flash = $this->session->getFlashBag();
257 $flash->set('hello', 'world');
258 $this->assertFalse($this->session->isEmpty());
260 $flash->get('hello');
261 $this->assertTrue($this->session->isEmpty());