FlashBagTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\FlashBag;
  13. /**
  14. * FlashBagTest.
  15. *
  16. * @author Drak <drak@zikula.org>
  17. */
  18. class FlashBagTest extends TestCase
  19. {
  20. /**
  21. * @var \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface
  22. */
  23. private $bag;
  24. protected $array = [];
  25. protected function setUp()
  26. {
  27. parent::setUp();
  28. $this->bag = new FlashBag();
  29. $this->array = ['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. $bag->initialize($this->array);
  41. $this->assertEquals($this->array, $bag->peekAll());
  42. $array = ['should' => ['change']];
  43. $bag->initialize($array);
  44. $this->assertEquals($array, $bag->peekAll());
  45. }
  46. public function testGetStorageKey()
  47. {
  48. $this->assertEquals('_symfony_flashes', $this->bag->getStorageKey());
  49. $attributeBag = new FlashBag('test');
  50. $this->assertEquals('test', $attributeBag->getStorageKey());
  51. }
  52. public function testGetSetName()
  53. {
  54. $this->assertEquals('flashes', $this->bag->getName());
  55. $this->bag->setName('foo');
  56. $this->assertEquals('foo', $this->bag->getName());
  57. }
  58. public function testPeek()
  59. {
  60. $this->assertEquals([], $this->bag->peek('non_existing'));
  61. $this->assertEquals(['default'], $this->bag->peek('not_existing', ['default']));
  62. $this->assertEquals(['A previous flash message'], $this->bag->peek('notice'));
  63. $this->assertEquals(['A previous flash message'], $this->bag->peek('notice'));
  64. }
  65. public function testAdd()
  66. {
  67. $tab = ['bar' => 'baz'];
  68. $this->bag->add('string_message', 'lorem');
  69. $this->bag->add('object_message', new \stdClass());
  70. $this->bag->add('array_message', $tab);
  71. $this->assertEquals(['lorem'], $this->bag->get('string_message'));
  72. $this->assertEquals([new \stdClass()], $this->bag->get('object_message'));
  73. $this->assertEquals([$tab], $this->bag->get('array_message'));
  74. }
  75. public function testGet()
  76. {
  77. $this->assertEquals([], $this->bag->get('non_existing'));
  78. $this->assertEquals(['default'], $this->bag->get('not_existing', ['default']));
  79. $this->assertEquals(['A previous flash message'], $this->bag->get('notice'));
  80. $this->assertEquals([], $this->bag->get('notice'));
  81. }
  82. public function testAll()
  83. {
  84. $this->bag->set('notice', 'Foo');
  85. $this->bag->set('error', 'Bar');
  86. $this->assertEquals([
  87. 'notice' => ['Foo'],
  88. 'error' => ['Bar'], ], $this->bag->all()
  89. );
  90. $this->assertEquals([], $this->bag->all());
  91. }
  92. public function testSet()
  93. {
  94. $this->bag->set('notice', 'Foo');
  95. $this->bag->set('notice', 'Bar');
  96. $this->assertEquals(['Bar'], $this->bag->peek('notice'));
  97. }
  98. public function testHas()
  99. {
  100. $this->assertFalse($this->bag->has('nothing'));
  101. $this->assertTrue($this->bag->has('notice'));
  102. }
  103. public function testKeys()
  104. {
  105. $this->assertEquals(['notice'], $this->bag->keys());
  106. }
  107. public function testSetAll()
  108. {
  109. $this->bag->add('one_flash', 'Foo');
  110. $this->bag->add('another_flash', 'Bar');
  111. $this->assertTrue($this->bag->has('one_flash'));
  112. $this->assertTrue($this->bag->has('another_flash'));
  113. $this->bag->setAll(['unique_flash' => 'FooBar']);
  114. $this->assertFalse($this->bag->has('one_flash'));
  115. $this->assertFalse($this->bag->has('another_flash'));
  116. $this->assertSame(['unique_flash' => 'FooBar'], $this->bag->all());
  117. $this->assertSame([], $this->bag->all());
  118. }
  119. public function testPeekAll()
  120. {
  121. $this->bag->set('notice', 'Foo');
  122. $this->bag->set('error', 'Bar');
  123. $this->assertEquals([
  124. 'notice' => ['Foo'],
  125. 'error' => ['Bar'],
  126. ], $this->bag->peekAll()
  127. );
  128. $this->assertTrue($this->bag->has('notice'));
  129. $this->assertTrue($this->bag->has('error'));
  130. $this->assertEquals([
  131. 'notice' => ['Foo'],
  132. 'error' => ['Bar'],
  133. ], $this->bag->peekAll()
  134. );
  135. }
  136. }