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\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;
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 testSetName()
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());
81 public function testGet()
84 $this->assertNull($this->session->get('foo'));
85 $this->assertEquals(1, $this->session->get('foo', 1));
89 * @dataProvider setProvider
91 public function testSet($key, $value)
93 $this->session->set($key, $value);
94 $this->assertEquals($value, $this->session->get($key));
98 * @dataProvider setProvider
100 public function testHas($key, $value)
102 $this->session->set($key, $value);
103 $this->assertTrue($this->session->has($key));
104 $this->assertFalse($this->session->has($key.'non_value'));
107 public function testReplace()
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());
116 * @dataProvider setProvider
118 public function testAll($key, $value, $result)
120 $this->session->set($key, $value);
121 $this->assertEquals($result, $this->session->all());
125 * @dataProvider setProvider
127 public function testClear($key, $value)
129 $this->session->set('hi', 'fabien');
130 $this->session->set($key, $value);
131 $this->session->clear();
132 $this->assertEquals(array(), $this->session->all());
135 public function setProvider()
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')),
145 * @dataProvider setProvider
147 public function testRemove($key, $value)
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());
155 public function testInvalidate()
157 $this->session->set('invalidate', 123);
158 $this->session->invalidate();
159 $this->assertEquals(array(), $this->session->all());
162 public function testMigrate()
164 $this->session->set('migrate', 321);
165 $this->session->migrate();
166 $this->assertEquals(321, $this->session->get('migrate'));
169 public function testMigrateDestroy()
171 $this->session->set('migrate', 333);
172 $this->session->migrate(true);
173 $this->assertEquals(333, $this->session->get('migrate'));
176 public function testSave()
178 $this->session->start();
179 $this->session->save();
181 $this->assertFalse($this->session->isStarted());
184 public function testGetId()
186 $this->assertEquals('', $this->session->getId());
187 $this->session->start();
188 $this->assertNotEquals('', $this->session->getId());
191 public function testGetFlashBag()
193 $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface', $this->session->getFlashBag());
196 public function testGetIterator()
198 $attributes = array('hello' => 'world', 'symfony' => 'rocks');
199 foreach ($attributes as $key => $val) {
200 $this->session->set($key, $val);
204 foreach ($this->session as $key => $val) {
205 $this->assertEquals($attributes[$key], $val);
209 $this->assertEquals(count($attributes), $i);
212 public function testGetCount()
214 $this->session->set('hello', 'world');
215 $this->session->set('symfony', 'rocks');
217 $this->assertCount(2, $this->session);
220 public function testGetMeta()
222 $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\MetadataBag', $this->session->getMetadataBag());