AutoExpireFlashBagTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Flash;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag as FlashBag;
  13. /**
  14. * AutoExpireFlashBagTest.
  15. *
  16. * @author Drak <drak@zikula.org>
  17. */
  18. class AutoExpireFlashBagTest extends TestCase
  19. {
  20. /**
  21. * @var \Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag
  22. */
  23. private $bag;
  24. protected $array = [];
  25. protected function setUp()
  26. {
  27. parent::setUp();
  28. $this->bag = new FlashBag();
  29. $this->array = ['new' => ['notice' => ['A previous flash message']]];
  30. $this->bag->initialize($this->array);
  31. }
  32. protected function tearDown()
  33. {
  34. $this->bag = null;
  35. parent::tearDown();
  36. }
  37. public function testInitialize()
  38. {
  39. $bag = new FlashBag();
  40. $array = ['new' => ['notice' => ['A previous flash message']]];
  41. $bag->initialize($array);
  42. $this->assertEquals(['A previous flash message'], $bag->peek('notice'));
  43. $array = ['new' => [
  44. 'notice' => ['Something else'],
  45. 'error' => ['a'],
  46. ]];
  47. $bag->initialize($array);
  48. $this->assertEquals(['Something else'], $bag->peek('notice'));
  49. $this->assertEquals(['a'], $bag->peek('error'));
  50. }
  51. public function testGetStorageKey()
  52. {
  53. $this->assertEquals('_symfony_flashes', $this->bag->getStorageKey());
  54. $attributeBag = new FlashBag('test');
  55. $this->assertEquals('test', $attributeBag->getStorageKey());
  56. }
  57. public function testGetSetName()
  58. {
  59. $this->assertEquals('flashes', $this->bag->getName());
  60. $this->bag->setName('foo');
  61. $this->assertEquals('foo', $this->bag->getName());
  62. }
  63. public function testPeek()
  64. {
  65. $this->assertEquals([], $this->bag->peek('non_existing'));
  66. $this->assertEquals(['default'], $this->bag->peek('non_existing', ['default']));
  67. $this->assertEquals(['A previous flash message'], $this->bag->peek('notice'));
  68. $this->assertEquals(['A previous flash message'], $this->bag->peek('notice'));
  69. }
  70. public function testSet()
  71. {
  72. $this->bag->set('notice', 'Foo');
  73. $this->assertEquals(['A previous flash message'], $this->bag->peek('notice'));
  74. }
  75. public function testHas()
  76. {
  77. $this->assertFalse($this->bag->has('nothing'));
  78. $this->assertTrue($this->bag->has('notice'));
  79. }
  80. public function testKeys()
  81. {
  82. $this->assertEquals(['notice'], $this->bag->keys());
  83. }
  84. public function testPeekAll()
  85. {
  86. $array = [
  87. 'new' => [
  88. 'notice' => 'Foo',
  89. 'error' => 'Bar',
  90. ],
  91. ];
  92. $this->bag->initialize($array);
  93. $this->assertEquals([
  94. 'notice' => 'Foo',
  95. 'error' => 'Bar',
  96. ], $this->bag->peekAll()
  97. );
  98. $this->assertEquals([
  99. 'notice' => 'Foo',
  100. 'error' => 'Bar',
  101. ], $this->bag->peekAll()
  102. );
  103. }
  104. public function testGet()
  105. {
  106. $this->assertEquals([], $this->bag->get('non_existing'));
  107. $this->assertEquals(['default'], $this->bag->get('non_existing', ['default']));
  108. $this->assertEquals(['A previous flash message'], $this->bag->get('notice'));
  109. $this->assertEquals([], $this->bag->get('notice'));
  110. }
  111. public function testSetAll()
  112. {
  113. $this->bag->setAll(['a' => 'first', 'b' => 'second']);
  114. $this->assertFalse($this->bag->has('a'));
  115. $this->assertFalse($this->bag->has('b'));
  116. }
  117. public function testAll()
  118. {
  119. $this->bag->set('notice', 'Foo');
  120. $this->bag->set('error', 'Bar');
  121. $this->assertEquals([
  122. 'notice' => ['A previous flash message'],
  123. ], $this->bag->all()
  124. );
  125. $this->assertEquals([], $this->bag->all());
  126. }
  127. public function testClear()
  128. {
  129. $this->assertEquals(['notice' => ['A previous flash message']], $this->bag->clear());
  130. }
  131. public function testDoNotRemoveTheNewFlashesWhenDisplayingTheExistingOnes()
  132. {
  133. $this->bag->add('success', 'Something');
  134. $this->bag->all();
  135. $this->assertEquals(['new' => ['success' => ['Something']], 'display' => []], $this->array);
  136. }
  137. }