1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;
13:
14: use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
15: use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
16: use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
17:
18: 19: 20: 21: 22:
23: class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase
24: {
25: 26: 27:
28: private $sessionDir;
29:
30: 31: 32:
33: protected $storage;
34:
35: protected function setUp()
36: {
37: $this->sessionDir = sys_get_temp_dir().'/sf2test';
38: $this->storage = $this->getStorage();
39: }
40:
41: protected function tearDown()
42: {
43: $this->sessionDir = null;
44: $this->storage = null;
45: array_map('unlink', glob($this->sessionDir.'/*.session'));
46: if (is_dir($this->sessionDir)) {
47: rmdir($this->sessionDir);
48: }
49: }
50:
51: public function testStart()
52: {
53: $this->assertEquals('', $this->storage->getId());
54: $this->assertTrue($this->storage->start());
55: $id = $this->storage->getId();
56: $this->assertNotEquals('', $this->storage->getId());
57: $this->assertTrue($this->storage->start());
58: $this->assertEquals($id, $this->storage->getId());
59: }
60:
61: public function testRegenerate()
62: {
63: $this->storage->start();
64: $this->storage->getBag('attributes')->set('regenerate', 1234);
65: $this->storage->regenerate();
66: $this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
67: $this->storage->regenerate(true);
68: $this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
69: }
70:
71: public function testGetId()
72: {
73: $this->assertEquals('', $this->storage->getId());
74: $this->storage->start();
75: $this->assertNotEquals('', $this->storage->getId());
76: }
77:
78: public function testSave()
79: {
80: $this->storage->start();
81: $id = $this->storage->getId();
82: $this->assertNotEquals('108', $this->storage->getBag('attributes')->get('new'));
83: $this->assertFalse($this->storage->getBag('flashes')->has('newkey'));
84: $this->storage->getBag('attributes')->set('new', '108');
85: $this->storage->getBag('flashes')->set('newkey', 'test');
86: $this->storage->save();
87:
88: $storage = $this->getStorage();
89: $storage->setId($id);
90: $storage->start();
91: $this->assertEquals('108', $storage->getBag('attributes')->get('new'));
92: $this->assertTrue($storage->getBag('flashes')->has('newkey'));
93: $this->assertEquals(array('test'), $storage->getBag('flashes')->peek('newkey'));
94: }
95:
96: public function testMultipleInstances()
97: {
98: $storage1 = $this->getStorage();
99: $storage1->start();
100: $storage1->getBag('attributes')->set('foo', 'bar');
101: $storage1->save();
102:
103: $storage2 = $this->getStorage();
104: $storage2->setId($storage1->getId());
105: $storage2->start();
106: $this->assertEquals('bar', $storage2->getBag('attributes')->get('foo'), 'values persist between instances');
107: }
108:
109: 110: 111:
112: public function testSaveWithoutStart()
113: {
114: $storage1 = $this->getStorage();
115: $storage1->save();
116: }
117:
118: private function getStorage()
119: {
120: $storage = new MockFileSessionStorage($this->sessionDir);
121: $storage->registerBag(new FlashBag());
122: $storage->registerBag(new AttributeBag());
123:
124: return $storage;
125: }
126: }
127: