PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/PhpBridgeSessionStorageTest.php

http://github.com/symfony/symfony
PHP | 92 lines | 59 code | 14 blank | 19 comment | 2 complexity | b7ab6d09e3eabf0c6deff5400926bb08 MD5 | raw file
  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(): void
  28. {
  29. $this->iniSet('session.save_handler', 'files');
  30. $this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sftest');
  31. if (!is_dir($this->savePath)) {
  32. mkdir($this->savePath);
  33. }
  34. }
  35. protected function tearDown(): void
  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. protected function getStorage(): PhpBridgeSessionStorage
  45. {
  46. $storage = new PhpBridgeSessionStorage();
  47. $storage->registerBag(new AttributeBag());
  48. return $storage;
  49. }
  50. public function testPhpSession()
  51. {
  52. $storage = $this->getStorage();
  53. $this->assertNotSame(PHP_SESSION_ACTIVE, session_status());
  54. $this->assertFalse($storage->isStarted());
  55. session_start();
  56. $this->assertTrue(isset($_SESSION));
  57. $this->assertSame(PHP_SESSION_ACTIVE, session_status());
  58. // PHP session might have started, but the storage driver has not, so false is correct here
  59. $this->assertFalse($storage->isStarted());
  60. $key = $storage->getMetadataBag()->getStorageKey();
  61. $this->assertArrayNotHasKey($key, $_SESSION);
  62. $storage->start();
  63. $this->assertArrayHasKey($key, $_SESSION);
  64. }
  65. public function testClear()
  66. {
  67. $storage = $this->getStorage();
  68. session_start();
  69. $_SESSION['drak'] = 'loves symfony';
  70. $storage->getBag('attributes')->set('symfony', 'greatness');
  71. $key = $storage->getBag('attributes')->getStorageKey();
  72. $this->assertEquals($_SESSION[$key], ['symfony' => 'greatness']);
  73. $this->assertEquals($_SESSION['drak'], 'loves symfony');
  74. $storage->clear();
  75. $this->assertEquals($_SESSION[$key], []);
  76. $this->assertEquals($_SESSION['drak'], 'loves symfony');
  77. }
  78. }