No Description

PhpBridgeSessionStorageTest.php 2.8KB

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