PhpBridgeSessionStorageTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  13. use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
  14. /**
  15. * Test class for PhpSessionStorage.
  16. *
  17. * @author Drak <drak@zikula.org>
  18. *
  19. * These tests require separate processes.
  20. *
  21. * @runTestsInSeparateProcesses
  22. * @preserveGlobalState disabled
  23. */
  24. class PhpBridgeSessionStorageTest extends TestCase
  25. {
  26. private $savePath;
  27. protected function setUp()
  28. {
  29. $this->iniSet('session.save_handler', 'files');
  30. $this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test');
  31. if (!is_dir($this->savePath)) {
  32. mkdir($this->savePath);
  33. }
  34. }
  35. protected function tearDown()
  36. {
  37. session_write_close();
  38. array_map('unlink', glob($this->savePath.'/*'));
  39. if (is_dir($this->savePath)) {
  40. rmdir($this->savePath);
  41. }
  42. $this->savePath = null;
  43. }
  44. /**
  45. * @return PhpBridgeSessionStorage
  46. */
  47. protected function getStorage()
  48. {
  49. $storage = new PhpBridgeSessionStorage();
  50. $storage->registerBag(new AttributeBag());
  51. return $storage;
  52. }
  53. public function testPhpSession53()
  54. {
  55. if (\PHP_VERSION_ID >= 50400) {
  56. $this->markTestSkipped('Test skipped, for PHP 5.3 only.');
  57. }
  58. $storage = $this->getStorage();
  59. $this->assertFalse(isset($_SESSION));
  60. $this->assertFalse($storage->getSaveHandler()->isActive());
  61. session_start();
  62. $this->assertTrue(isset($_SESSION));
  63. // in PHP 5.3 we cannot reliably tell if a session has started
  64. $this->assertFalse($storage->getSaveHandler()->isActive());
  65. // PHP session might have started, but the storage driver has not, so false is correct here
  66. $this->assertFalse($storage->isStarted());
  67. $key = $storage->getMetadataBag()->getStorageKey();
  68. $this->assertArrayNotHasKey($key, $_SESSION);
  69. $storage->start();
  70. $this->assertArrayHasKey($key, $_SESSION);
  71. }
  72. /**
  73. * @requires PHP 5.4
  74. */
  75. public function testPhpSession54()
  76. {
  77. $storage = $this->getStorage();
  78. $this->assertFalse($storage->getSaveHandler()->isActive());
  79. $this->assertFalse($storage->isStarted());
  80. session_start();
  81. $this->assertTrue(isset($_SESSION));
  82. // in PHP 5.4 we can reliably detect a session started
  83. $this->assertTrue($storage->getSaveHandler()->isActive());
  84. // PHP session might have started, but the storage driver has not, so false is correct here
  85. $this->assertFalse($storage->isStarted());
  86. $key = $storage->getMetadataBag()->getStorageKey();
  87. $this->assertArrayNotHasKey($key, $_SESSION);
  88. $storage->start();
  89. $this->assertArrayHasKey($key, $_SESSION);
  90. }
  91. public function testClear()
  92. {
  93. $storage = $this->getStorage();
  94. session_start();
  95. $_SESSION['drak'] = 'loves symfony';
  96. $storage->getBag('attributes')->set('symfony', 'greatness');
  97. $key = $storage->getBag('attributes')->getStorageKey();
  98. $this->assertEquals($_SESSION[$key], array('symfony' => 'greatness'));
  99. $this->assertEquals($_SESSION['drak'], 'loves symfony');
  100. $storage->clear();
  101. $this->assertEquals($_SESSION[$key], array());
  102. $this->assertEquals($_SESSION['drak'], 'loves symfony');
  103. }
  104. }