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\Storage;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
16 use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
19 * Test class for PhpSessionStorage.
21 * @author Drak <drak@zikula.org>
23 * These tests require separate processes.
25 * @runTestsInSeparateProcesses
26 * @preserveGlobalState disabled
28 class PhpBridgeSessionStorageTest extends TestCase
32 protected function setUp()
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);
41 protected function tearDown()
43 session_write_close();
44 array_map('unlink', glob($this->savePath.'/*'));
45 if (is_dir($this->savePath)) {
46 rmdir($this->savePath);
49 $this->savePath = null;
53 * @return PhpBridgeSessionStorage
55 protected function getStorage()
57 $storage = new PhpBridgeSessionStorage();
58 $storage->registerBag(new AttributeBag());
63 public function testPhpSession53()
65 if (\PHP_VERSION_ID >= 50400) {
66 $this->markTestSkipped('Test skipped, for PHP 5.3 only.');
69 $storage = $this->getStorage();
71 $this->assertFalse(isset($_SESSION));
72 $this->assertFalse($storage->getSaveHandler()->isActive());
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());
81 $key = $storage->getMetadataBag()->getStorageKey();
82 $this->assertFalse(isset($_SESSION[$key]));
84 $this->assertTrue(isset($_SESSION[$key]));
90 public function testPhpSession54()
92 $storage = $this->getStorage();
94 $this->assertFalse($storage->getSaveHandler()->isActive());
95 $this->assertFalse($storage->isStarted());
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());
104 $key = $storage->getMetadataBag()->getStorageKey();
105 $this->assertFalse(isset($_SESSION[$key]));
107 $this->assertTrue(isset($_SESSION[$key]));
110 public function testClear()
112 $storage = $this->getStorage();
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');
120 $this->assertEquals($_SESSION[$key], array());
121 $this->assertEquals($_SESSION['drak'], 'loves symfony');