1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\HttpFoundation\Tests\Session;
13:
14: use Symfony\Component\HttpFoundation\Session\Session;
15: use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
16: use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
17: use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
18:
19: 20: 21: 22: 23: 24: 25:
26: class SessionTest extends \PHPUnit_Framework_TestCase
27: {
28: 29: 30:
31: protected $storage;
32:
33: 34: 35:
36: protected $session;
37:
38: protected function setUp()
39: {
40: $this->storage = new MockArraySessionStorage();
41: $this->session = new Session($this->storage, new AttributeBag(), new FlashBag());
42: }
43:
44: protected function tearDown()
45: {
46: $this->storage = null;
47: $this->session = null;
48: }
49:
50: public function testStart()
51: {
52: $this->assertEquals('', $this->session->getId());
53: $this->assertTrue($this->session->start());
54: $this->assertNotEquals('', $this->session->getId());
55: }
56:
57: public function testIsStarted()
58: {
59: $this->assertFalse($this->session->isStarted());
60: $this->session->start();
61: $this->assertTrue($this->session->isStarted());
62: }
63:
64: public function testSetId()
65: {
66: $this->assertEquals('', $this->session->getId());
67: $this->session->setId('0123456789abcdef');
68: $this->session->start();
69: $this->assertEquals('0123456789abcdef', $this->session->getId());
70: }
71:
72: public function testSetName()
73: {
74: $this->assertEquals('MOCKSESSID', $this->session->getName());
75: $this->session->setName('session.test.com');
76: $this->session->start();
77: $this->assertEquals('session.test.com', $this->session->getName());
78: }
79:
80: public function testGet()
81: {
82:
83: $this->assertNull($this->session->get('foo'));
84: $this->assertEquals(1, $this->session->get('foo', 1));
85: }
86:
87: 88: 89:
90: public function testSet($key, $value)
91: {
92: $this->session->set($key, $value);
93: $this->assertEquals($value, $this->session->get($key));
94: }
95:
96: 97: 98:
99: public function testHas($key, $value)
100: {
101: $this->session->set($key, $value);
102: $this->assertTrue($this->session->has($key));
103: $this->assertFalse($this->session->has($key.'non_value'));
104: }
105:
106: public function testReplace()
107: {
108: $this->session->replace(array('happiness' => 'be good', 'symfony' => 'awesome'));
109: $this->assertEquals(array('happiness' => 'be good', 'symfony' => 'awesome'), $this->session->all());
110: $this->session->replace(array());
111: $this->assertEquals(array(), $this->session->all());
112: }
113:
114: 115: 116:
117: public function testAll($key, $value, $result)
118: {
119: $this->session->set($key, $value);
120: $this->assertEquals($result, $this->session->all());
121: }
122:
123: 124: 125:
126: public function testClear($key, $value)
127: {
128: $this->session->set('hi', 'fabien');
129: $this->session->set($key, $value);
130: $this->session->clear();
131: $this->assertEquals(array(), $this->session->all());
132: }
133:
134: public function setProvider()
135: {
136: return array(
137: array('foo', 'bar', array('foo' => 'bar')),
138: array('foo.bar', 'too much beer', array('foo.bar' => 'too much beer')),
139: array('great', 'symfony is great', array('great' => 'symfony is great')),
140: );
141: }
142:
143: 144: 145:
146: public function testRemove($key, $value)
147: {
148: $this->session->set('hi.world', 'have a nice day');
149: $this->session->set($key, $value);
150: $this->session->remove($key);
151: $this->assertEquals(array('hi.world' => 'have a nice day'), $this->session->all());
152: }
153:
154: public function testInvalidate()
155: {
156: $this->session->set('invalidate', 123);
157: $this->session->invalidate();
158: $this->assertEquals(array(), $this->session->all());
159: }
160:
161: public function testMigrate()
162: {
163: $this->session->set('migrate', 321);
164: $this->session->migrate();
165: $this->assertEquals(321, $this->session->get('migrate'));
166: }
167:
168: public function testMigrateDestroy()
169: {
170: $this->session->set('migrate', 333);
171: $this->session->migrate(true);
172: $this->assertEquals(333, $this->session->get('migrate'));
173: }
174:
175: public function testSave()
176: {
177: $this->session->start();
178: $this->session->save();
179: }
180:
181: public function testGetId()
182: {
183: $this->assertEquals('', $this->session->getId());
184: $this->session->start();
185: $this->assertNotEquals('', $this->session->getId());
186: }
187:
188: public function testGetFlashBag()
189: {
190: $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface', $this->session->getFlashBag());
191: }
192:
193: 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: 213: 214:
215: public function testGetCount()
216: {
217: $this->session->set('hello', 'world');
218: $this->session->set('symfony', 'rocks');
219:
220: $this->assertCount(2, $this->session);
221: }
222:
223: public function testGetMeta()
224: {
225: $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\MetadataBag', $this->session->getMetadataBag());
226: }
227: }
228: