123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
- use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
- /**
- * Test class for PhpSessionStorage.
- *
- * @author Drak <drak@zikula.org>
- *
- * These tests require separate processes.
- *
- * @runTestsInSeparateProcesses
- * @preserveGlobalState disabled
- */
- class PhpBridgeSessionStorageTest extends TestCase
- {
- private $savePath;
- protected function setUp()
- {
- $this->iniSet('session.save_handler', 'files');
- $this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test');
- if (!is_dir($this->savePath)) {
- mkdir($this->savePath);
- }
- }
- protected function tearDown()
- {
- session_write_close();
- array_map('unlink', glob($this->savePath.'/*'));
- if (is_dir($this->savePath)) {
- rmdir($this->savePath);
- }
- $this->savePath = null;
- }
- /**
- * @return PhpBridgeSessionStorage
- */
- protected function getStorage()
- {
- $storage = new PhpBridgeSessionStorage();
- $storage->registerBag(new AttributeBag());
- return $storage;
- }
- public function testPhpSession53()
- {
- if (\PHP_VERSION_ID >= 50400) {
- $this->markTestSkipped('Test skipped, for PHP 5.3 only.');
- }
- $storage = $this->getStorage();
- $this->assertFalse(isset($_SESSION));
- $this->assertFalse($storage->getSaveHandler()->isActive());
- session_start();
- $this->assertTrue(isset($_SESSION));
- // in PHP 5.3 we cannot reliably tell if a session has started
- $this->assertFalse($storage->getSaveHandler()->isActive());
- // PHP session might have started, but the storage driver has not, so false is correct here
- $this->assertFalse($storage->isStarted());
- $key = $storage->getMetadataBag()->getStorageKey();
- $this->assertArrayNotHasKey($key, $_SESSION);
- $storage->start();
- $this->assertArrayHasKey($key, $_SESSION);
- }
- /**
- * @requires PHP 5.4
- */
- public function testPhpSession54()
- {
- $storage = $this->getStorage();
- $this->assertFalse($storage->getSaveHandler()->isActive());
- $this->assertFalse($storage->isStarted());
- session_start();
- $this->assertTrue(isset($_SESSION));
- // in PHP 5.4 we can reliably detect a session started
- $this->assertTrue($storage->getSaveHandler()->isActive());
- // PHP session might have started, but the storage driver has not, so false is correct here
- $this->assertFalse($storage->isStarted());
- $key = $storage->getMetadataBag()->getStorageKey();
- $this->assertArrayNotHasKey($key, $_SESSION);
- $storage->start();
- $this->assertArrayHasKey($key, $_SESSION);
- }
- public function testClear()
- {
- $storage = $this->getStorage();
- session_start();
- $_SESSION['drak'] = 'loves symfony';
- $storage->getBag('attributes')->set('symfony', 'greatness');
- $key = $storage->getBag('attributes')->getStorageKey();
- $this->assertEquals($_SESSION[$key], array('symfony' => 'greatness'));
- $this->assertEquals($_SESSION['drak'], 'loves symfony');
- $storage->clear();
- $this->assertEquals($_SESSION[$key], array());
- $this->assertEquals($_SESSION['drak'], 'loves symfony');
- }
- }
|