MockArraySessionStorageTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\Flash\FlashBag;
  14. use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
  15. /**
  16. * Test class for MockArraySessionStorage.
  17. *
  18. * @author Drak <drak@zikula.org>
  19. */
  20. class MockArraySessionStorageTest extends TestCase
  21. {
  22. /**
  23. * @var MockArraySessionStorage
  24. */
  25. private $storage;
  26. /**
  27. * @var AttributeBag
  28. */
  29. private $attributes;
  30. /**
  31. * @var FlashBag
  32. */
  33. private $flashes;
  34. private $data;
  35. protected function setUp()
  36. {
  37. $this->attributes = new AttributeBag();
  38. $this->flashes = new FlashBag();
  39. $this->data = [
  40. $this->attributes->getStorageKey() => ['foo' => 'bar'],
  41. $this->flashes->getStorageKey() => ['notice' => 'hello'],
  42. ];
  43. $this->storage = new MockArraySessionStorage();
  44. $this->storage->registerBag($this->flashes);
  45. $this->storage->registerBag($this->attributes);
  46. $this->storage->setSessionData($this->data);
  47. }
  48. protected function tearDown()
  49. {
  50. $this->data = null;
  51. $this->flashes = null;
  52. $this->attributes = null;
  53. $this->storage = null;
  54. }
  55. public function testStart()
  56. {
  57. $this->assertEquals('', $this->storage->getId());
  58. $this->storage->start();
  59. $id = $this->storage->getId();
  60. $this->assertNotEquals('', $id);
  61. $this->storage->start();
  62. $this->assertEquals($id, $this->storage->getId());
  63. }
  64. public function testRegenerate()
  65. {
  66. $this->storage->start();
  67. $id = $this->storage->getId();
  68. $this->storage->regenerate();
  69. $this->assertNotEquals($id, $this->storage->getId());
  70. $this->assertEquals(['foo' => 'bar'], $this->storage->getBag('attributes')->all());
  71. $this->assertEquals(['notice' => 'hello'], $this->storage->getBag('flashes')->peekAll());
  72. $id = $this->storage->getId();
  73. $this->storage->regenerate(true);
  74. $this->assertNotEquals($id, $this->storage->getId());
  75. $this->assertEquals(['foo' => 'bar'], $this->storage->getBag('attributes')->all());
  76. $this->assertEquals(['notice' => 'hello'], $this->storage->getBag('flashes')->peekAll());
  77. }
  78. public function testGetId()
  79. {
  80. $this->assertEquals('', $this->storage->getId());
  81. $this->storage->start();
  82. $this->assertNotEquals('', $this->storage->getId());
  83. }
  84. public function testClearClearsBags()
  85. {
  86. $this->storage->clear();
  87. $this->assertSame([], $this->storage->getBag('attributes')->all());
  88. $this->assertSame([], $this->storage->getBag('flashes')->peekAll());
  89. }
  90. public function testClearStartsSession()
  91. {
  92. $this->storage->clear();
  93. $this->assertTrue($this->storage->isStarted());
  94. }
  95. public function testClearWithNoBagsStartsSession()
  96. {
  97. $storage = new MockArraySessionStorage();
  98. $storage->clear();
  99. $this->assertTrue($storage->isStarted());
  100. }
  101. /**
  102. * @expectedException \RuntimeException
  103. */
  104. public function testUnstartedSave()
  105. {
  106. $this->storage->save();
  107. }
  108. }