FlashBagTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /**
  25. * @var array
  26. */
  27. protected $array = array();
  28. protected function setUp()
  29. {
  30. parent::setUp();
  31. $this->bag = new FlashBag();
  32. $this->array = array('notice' => array('A previous flash message'));
  33. $this->bag->initialize($this->array);
  34. }
  35. protected function tearDown()
  36. {
  37. $this->bag = null;
  38. parent::tearDown();
  39. }
  40. public function testInitialize()
  41. {
  42. $bag = new FlashBag();
  43. $bag->initialize($this->array);
  44. $this->assertEquals($this->array, $bag->peekAll());
  45. $array = array('should' => array('change'));
  46. $bag->initialize($array);
  47. $this->assertEquals($array, $bag->peekAll());
  48. }
  49. public function testGetStorageKey()
  50. {
  51. $this->assertEquals('_sf2_flashes', $this->bag->getStorageKey());
  52. $attributeBag = new FlashBag('test');
  53. $this->assertEquals('test', $attributeBag->getStorageKey());
  54. }
  55. public function testGetSetName()
  56. {
  57. $this->assertEquals('flashes', $this->bag->getName());
  58. $this->bag->setName('foo');
  59. $this->assertEquals('foo', $this->bag->getName());
  60. }
  61. public function testPeek()
  62. {
  63. $this->assertEquals(array(), $this->bag->peek('non_existing'));
  64. $this->assertEquals(array('default'), $this->bag->peek('not_existing', array('default')));
  65. $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
  66. $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
  67. }
  68. public function testGet()
  69. {
  70. $this->assertEquals(array(), $this->bag->get('non_existing'));
  71. $this->assertEquals(array('default'), $this->bag->get('not_existing', array('default')));
  72. $this->assertEquals(array('A previous flash message'), $this->bag->get('notice'));
  73. $this->assertEquals(array(), $this->bag->get('notice'));
  74. }
  75. public function testAll()
  76. {
  77. $this->bag->set('notice', 'Foo');
  78. $this->bag->set('error', 'Bar');
  79. $this->assertEquals(array(
  80. 'notice' => array('Foo'),
  81. 'error' => array('Bar'), ), $this->bag->all()
  82. );
  83. $this->assertEquals(array(), $this->bag->all());
  84. }
  85. public function testSet()
  86. {
  87. $this->bag->set('notice', 'Foo');
  88. $this->bag->set('notice', 'Bar');
  89. $this->assertEquals(array('Bar'), $this->bag->peek('notice'));
  90. }
  91. public function testHas()
  92. {
  93. $this->assertFalse($this->bag->has('nothing'));
  94. $this->assertTrue($this->bag->has('notice'));
  95. }
  96. public function testKeys()
  97. {
  98. $this->assertEquals(array('notice'), $this->bag->keys());
  99. }
  100. public function testPeekAll()
  101. {
  102. $this->bag->set('notice', 'Foo');
  103. $this->bag->set('error', 'Bar');
  104. $this->assertEquals(array(
  105. 'notice' => array('Foo'),
  106. 'error' => array('Bar'),
  107. ), $this->bag->peekAll()
  108. );
  109. $this->assertTrue($this->bag->has('notice'));
  110. $this->assertTrue($this->bag->has('error'));
  111. $this->assertEquals(array(
  112. 'notice' => array('Foo'),
  113. 'error' => array('Bar'),
  114. ), $this->bag->peekAll()
  115. );
  116. }
  117. }