MockFileSessionStorageTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\Storage\MockFileSessionStorage;
  13. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  14. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  15. /**
  16. * Test class for MockFileSessionStorage.
  17. *
  18. * @author Drak <drak@zikula.org>
  19. */
  20. class MockFileSessionStorageTest extends TestCase
  21. {
  22. /**
  23. * @var string
  24. */
  25. private $sessionDir;
  26. /**
  27. * @var MockFileSessionStorage
  28. */
  29. protected $storage;
  30. protected function setUp()
  31. {
  32. $this->sessionDir = sys_get_temp_dir().'/sf2test';
  33. $this->storage = $this->getStorage();
  34. }
  35. protected function tearDown()
  36. {
  37. $this->sessionDir = null;
  38. $this->storage = null;
  39. array_map('unlink', glob($this->sessionDir.'/*.session'));
  40. if (is_dir($this->sessionDir)) {
  41. rmdir($this->sessionDir);
  42. }
  43. }
  44. public function testStart()
  45. {
  46. $this->assertEquals('', $this->storage->getId());
  47. $this->assertTrue($this->storage->start());
  48. $id = $this->storage->getId();
  49. $this->assertNotEquals('', $this->storage->getId());
  50. $this->assertTrue($this->storage->start());
  51. $this->assertEquals($id, $this->storage->getId());
  52. }
  53. public function testRegenerate()
  54. {
  55. $this->storage->start();
  56. $this->storage->getBag('attributes')->set('regenerate', 1234);
  57. $this->storage->regenerate();
  58. $this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
  59. $this->storage->regenerate(true);
  60. $this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
  61. }
  62. public function testGetId()
  63. {
  64. $this->assertEquals('', $this->storage->getId());
  65. $this->storage->start();
  66. $this->assertNotEquals('', $this->storage->getId());
  67. }
  68. public function testSave()
  69. {
  70. $this->storage->start();
  71. $id = $this->storage->getId();
  72. $this->assertNotEquals('108', $this->storage->getBag('attributes')->get('new'));
  73. $this->assertFalse($this->storage->getBag('flashes')->has('newkey'));
  74. $this->storage->getBag('attributes')->set('new', '108');
  75. $this->storage->getBag('flashes')->set('newkey', 'test');
  76. $this->storage->save();
  77. $storage = $this->getStorage();
  78. $storage->setId($id);
  79. $storage->start();
  80. $this->assertEquals('108', $storage->getBag('attributes')->get('new'));
  81. $this->assertTrue($storage->getBag('flashes')->has('newkey'));
  82. $this->assertEquals(array('test'), $storage->getBag('flashes')->peek('newkey'));
  83. }
  84. public function testMultipleInstances()
  85. {
  86. $storage1 = $this->getStorage();
  87. $storage1->start();
  88. $storage1->getBag('attributes')->set('foo', 'bar');
  89. $storage1->save();
  90. $storage2 = $this->getStorage();
  91. $storage2->setId($storage1->getId());
  92. $storage2->start();
  93. $this->assertEquals('bar', $storage2->getBag('attributes')->get('foo'), 'values persist between instances');
  94. }
  95. /**
  96. * @expectedException \RuntimeException
  97. */
  98. public function testSaveWithoutStart()
  99. {
  100. $storage1 = $this->getStorage();
  101. $storage1->save();
  102. }
  103. private function getStorage()
  104. {
  105. $storage = new MockFileSessionStorage($this->sessionDir);
  106. $storage->registerBag(new FlashBag());
  107. $storage->registerBag(new AttributeBag());
  108. return $storage;
  109. }
  110. }