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