PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/gruenwaldt/loquitur-web
PHP | 123 lines | 79 code | 20 blank | 24 comment | 4 complexity | 239f3c5bbd80ecc6685e2320c6a36d11 MD5 | raw file
Possible License(s): BSD-3-Clause
  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 Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
  12. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  13. /**
  14. * Test class for PhpSessionStorage.
  15. *
  16. * @author Drak <drak@zikula.org>
  17. *
  18. * These tests require separate processes.
  19. *
  20. * @runTestsInSeparateProcesses
  21. */
  22. class PhpSessionStorageTest extends \PHPUnit_Framework_TestCase
  23. {
  24. private $savePath;
  25. protected function setUp()
  26. {
  27. ini_set('session.save_handler', 'files');
  28. ini_set('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test');
  29. if (!is_dir($this->savePath)) {
  30. mkdir($this->savePath);
  31. }
  32. }
  33. protected function tearDown()
  34. {
  35. session_write_close();
  36. array_map('unlink', glob($this->savePath.'/*'));
  37. if (is_dir($this->savePath)) {
  38. rmdir($this->savePath);
  39. }
  40. $this->savePath = null;
  41. }
  42. /**
  43. * @return PhpBridgeSessionStorage
  44. */
  45. protected function getStorage()
  46. {
  47. $storage = new PhpBridgeSessionStorage();
  48. $storage->registerBag(new AttributeBag);
  49. return $storage;
  50. }
  51. public function testPhpSession53()
  52. {
  53. if (version_compare(phpversion(), '5.4.0', '>=')) {
  54. $this->markTestSkipped('Test skipped, for PHP 5.3 only.');
  55. }
  56. $storage = $this->getStorage();
  57. $this->assertFalse(isset($_SESSION));
  58. $this->assertFalse($storage->getSaveHandler()->isActive());
  59. session_start();
  60. $this->assertTrue(isset($_SESSION));
  61. // in PHP 5.3 we cannot reliably tell if a session has started
  62. $this->assertFalse($storage->getSaveHandler()->isActive());
  63. // PHP session might have started, but the storage driver has not, so false is correct here
  64. $this->assertFalse($storage->isStarted());
  65. $key = $storage->getMetadataBag()->getStorageKey();
  66. $this->assertFalse(isset($_SESSION[$key]));
  67. $storage->start();
  68. $this->assertTrue(isset($_SESSION[$key]));
  69. }
  70. public function testPhpSession54()
  71. {
  72. if (version_compare(phpversion(), '5.4.0', '<')) {
  73. $this->markTestSkipped('Test skipped, for PHP 5.4 only.');
  74. }
  75. $storage = $this->getStorage();
  76. $this->assertFalse(isset($_SESSION));
  77. $this->assertFalse($storage->getSaveHandler()->isActive());
  78. $this->assertFalse($storage->isStarted());
  79. session_start();
  80. $this->assertTrue(isset($_SESSION));
  81. // in PHP 5.4 we can reliably detect a session started
  82. $this->assertTrue($storage->getSaveHandler()->isActive());
  83. // PHP session might have started, but the storage driver has not, so false is correct here
  84. $this->assertFalse($storage->isStarted());
  85. $key = $storage->getMetadataBag()->getStorageKey();
  86. $this->assertFalse(isset($_SESSION[$key]));
  87. $storage->start();
  88. $this->assertTrue(isset($_SESSION[$key]));
  89. }
  90. public function testClear()
  91. {
  92. $storage = $this->getStorage();
  93. session_start();
  94. $_SESSION['drak'] = 'loves symfony';
  95. $storage->getBag('attributes')->set('symfony', 'greatness');
  96. $key = $storage->getBag('attributes')->getStorageKey();
  97. $this->assertEquals($_SESSION[$key], array('symfony' => 'greatness'));
  98. $this->assertEquals($_SESSION['drak'], 'loves symfony');
  99. $storage->clear();
  100. $this->assertEquals($_SESSION[$key], array());
  101. $this->assertEquals($_SESSION['drak'], 'loves symfony');
  102. }
  103. }