Yaffs site version 1.1
[yaffs-website] / vendor / symfony / http-foundation / Tests / Session / Storage / PhpBridgeSessionStorageTest.php
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
12 namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
16 use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
17
18 /**
19  * Test class for PhpSessionStorage.
20  *
21  * @author Drak <drak@zikula.org>
22  *
23  * These tests require separate processes.
24  *
25  * @runTestsInSeparateProcesses
26  * @preserveGlobalState disabled
27  */
28 class PhpBridgeSessionStorageTest extends TestCase
29 {
30     private $savePath;
31
32     protected function setUp()
33     {
34         $this->iniSet('session.save_handler', 'files');
35         $this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test');
36         if (!is_dir($this->savePath)) {
37             mkdir($this->savePath);
38         }
39     }
40
41     protected function tearDown()
42     {
43         session_write_close();
44         array_map('unlink', glob($this->savePath.'/*'));
45         if (is_dir($this->savePath)) {
46             rmdir($this->savePath);
47         }
48
49         $this->savePath = null;
50     }
51
52     /**
53      * @return PhpBridgeSessionStorage
54      */
55     protected function getStorage()
56     {
57         $storage = new PhpBridgeSessionStorage();
58         $storage->registerBag(new AttributeBag());
59
60         return $storage;
61     }
62
63     public function testPhpSession53()
64     {
65         if (\PHP_VERSION_ID >= 50400) {
66             $this->markTestSkipped('Test skipped, for PHP 5.3 only.');
67         }
68
69         $storage = $this->getStorage();
70
71         $this->assertFalse(isset($_SESSION));
72         $this->assertFalse($storage->getSaveHandler()->isActive());
73
74         session_start();
75         $this->assertTrue(isset($_SESSION));
76         // in PHP 5.3 we cannot reliably tell if a session has started
77         $this->assertFalse($storage->getSaveHandler()->isActive());
78         // PHP session might have started, but the storage driver has not, so false is correct here
79         $this->assertFalse($storage->isStarted());
80
81         $key = $storage->getMetadataBag()->getStorageKey();
82         $this->assertFalse(isset($_SESSION[$key]));
83         $storage->start();
84         $this->assertTrue(isset($_SESSION[$key]));
85     }
86
87     /**
88      * @requires PHP 5.4
89      */
90     public function testPhpSession54()
91     {
92         $storage = $this->getStorage();
93
94         $this->assertFalse($storage->getSaveHandler()->isActive());
95         $this->assertFalse($storage->isStarted());
96
97         session_start();
98         $this->assertTrue(isset($_SESSION));
99         // in PHP 5.4 we can reliably detect a session started
100         $this->assertTrue($storage->getSaveHandler()->isActive());
101         // PHP session might have started, but the storage driver has not, so false is correct here
102         $this->assertFalse($storage->isStarted());
103
104         $key = $storage->getMetadataBag()->getStorageKey();
105         $this->assertFalse(isset($_SESSION[$key]));
106         $storage->start();
107         $this->assertTrue(isset($_SESSION[$key]));
108     }
109
110     public function testClear()
111     {
112         $storage = $this->getStorage();
113         session_start();
114         $_SESSION['drak'] = 'loves symfony';
115         $storage->getBag('attributes')->set('symfony', 'greatness');
116         $key = $storage->getBag('attributes')->getStorageKey();
117         $this->assertEquals($_SESSION[$key], array('symfony' => 'greatness'));
118         $this->assertEquals($_SESSION['drak'], 'loves symfony');
119         $storage->clear();
120         $this->assertEquals($_SESSION[$key], array());
121         $this->assertEquals($_SESSION['drak'], 'loves symfony');
122     }
123 }