PhpBridgeSessionStorageTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. *
  23. * @preserveGlobalState disabled
  24. */
  25. class PhpBridgeSessionStorageTest extends TestCase
  26. {
  27. private $savePath;
  28. protected function setUp(): void
  29. {
  30. $this->iniSet('session.save_handler', 'files');
  31. $this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sftest');
  32. if (!is_dir($this->savePath)) {
  33. mkdir($this->savePath);
  34. }
  35. }
  36. protected function tearDown(): void
  37. {
  38. session_write_close();
  39. array_map('unlink', glob($this->savePath.'/*'));
  40. if (is_dir($this->savePath)) {
  41. @rmdir($this->savePath);
  42. }
  43. $this->savePath = null;
  44. }
  45. protected function getStorage(): PhpBridgeSessionStorage
  46. {
  47. $storage = new PhpBridgeSessionStorage();
  48. $storage->registerBag(new AttributeBag());
  49. return $storage;
  50. }
  51. public function testPhpSession()
  52. {
  53. $storage = $this->getStorage();
  54. $this->assertNotSame(\PHP_SESSION_ACTIVE, session_status());
  55. $this->assertFalse($storage->isStarted());
  56. session_start();
  57. $this->assertTrue(isset($_SESSION));
  58. $this->assertSame(\PHP_SESSION_ACTIVE, session_status());
  59. // PHP session might have started, but the storage driver has not, so false is correct here
  60. $this->assertFalse($storage->isStarted());
  61. $key = $storage->getMetadataBag()->getStorageKey();
  62. $this->assertArrayNotHasKey($key, $_SESSION);
  63. $storage->start();
  64. $this->assertArrayHasKey($key, $_SESSION);
  65. }
  66. public function testClear()
  67. {
  68. $storage = $this->getStorage();
  69. session_start();
  70. $_SESSION['drak'] = 'loves symfony';
  71. $storage->getBag('attributes')->set('symfony', 'greatness');
  72. $key = $storage->getBag('attributes')->getStorageKey();
  73. $this->assertEquals(['symfony' => 'greatness'], $_SESSION[$key]);
  74. $this->assertEquals('loves symfony', $_SESSION['drak']);
  75. $storage->clear();
  76. $this->assertEquals([], $_SESSION[$key]);
  77. $this->assertEquals('loves symfony', $_SESSION['drak']);
  78. }
  79. }