PhpBridgeSessionStorageTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 testPhpSession()
  54. {
  55. $storage = $this->getStorage();
  56. $this->assertFalse($storage->getSaveHandler()->isActive());
  57. $this->assertFalse($storage->isStarted());
  58. session_start();
  59. $this->assertTrue(isset($_SESSION));
  60. // in PHP 5.4 we can reliably detect a session started
  61. $this->assertTrue($storage->getSaveHandler()->isActive());
  62. // PHP session might have started, but the storage driver has not, so false is correct here
  63. $this->assertFalse($storage->isStarted());
  64. $key = $storage->getMetadataBag()->getStorageKey();
  65. $this->assertArrayNotHasKey($key, $_SESSION);
  66. $storage->start();
  67. $this->assertArrayHasKey($key, $_SESSION);
  68. }
  69. public function testClear()
  70. {
  71. $storage = $this->getStorage();
  72. session_start();
  73. $_SESSION['drak'] = 'loves symfony';
  74. $storage->getBag('attributes')->set('symfony', 'greatness');
  75. $key = $storage->getBag('attributes')->getStorageKey();
  76. $this->assertEquals($_SESSION[$key], ['symfony' => 'greatness']);
  77. $this->assertEquals($_SESSION['drak'], 'loves symfony');
  78. $storage->clear();
  79. $this->assertEquals($_SESSION[$key], []);
  80. $this->assertEquals($_SESSION['drak'], 'loves symfony');
  81. }
  82. }