AutoExpireFlashBagTest.php 4.4 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\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. /**
  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('new' => 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. $array = array('new' => array('notice' => array('A previous flash message')));
  44. $bag->initialize($array);
  45. $this->assertEquals(array('A previous flash message'), $bag->peek('notice'));
  46. $array = array('new' => array(
  47. 'notice' => array('Something else'),
  48. 'error' => array('a'),
  49. ));
  50. $bag->initialize($array);
  51. $this->assertEquals(array('Something else'), $bag->peek('notice'));
  52. $this->assertEquals(array('a'), $bag->peek('error'));
  53. }
  54. public function testGetStorageKey()
  55. {
  56. $this->assertEquals('_sf2_flashes', $this->bag->getStorageKey());
  57. $attributeBag = new FlashBag('test');
  58. $this->assertEquals('test', $attributeBag->getStorageKey());
  59. }
  60. public function testGetSetName()
  61. {
  62. $this->assertEquals('flashes', $this->bag->getName());
  63. $this->bag->setName('foo');
  64. $this->assertEquals('foo', $this->bag->getName());
  65. }
  66. public function testPeek()
  67. {
  68. $this->assertEquals(array(), $this->bag->peek('non_existing'));
  69. $this->assertEquals(array('default'), $this->bag->peek('non_existing', array('default')));
  70. $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
  71. $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
  72. }
  73. public function testSet()
  74. {
  75. $this->bag->set('notice', 'Foo');
  76. $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
  77. }
  78. public function testHas()
  79. {
  80. $this->assertFalse($this->bag->has('nothing'));
  81. $this->assertTrue($this->bag->has('notice'));
  82. }
  83. public function testKeys()
  84. {
  85. $this->assertEquals(array('notice'), $this->bag->keys());
  86. }
  87. public function testPeekAll()
  88. {
  89. $array = array(
  90. 'new' => array(
  91. 'notice' => 'Foo',
  92. 'error' => 'Bar',
  93. ),
  94. );
  95. $this->bag->initialize($array);
  96. $this->assertEquals(array(
  97. 'notice' => 'Foo',
  98. 'error' => 'Bar',
  99. ), $this->bag->peekAll()
  100. );
  101. $this->assertEquals(array(
  102. 'notice' => 'Foo',
  103. 'error' => 'Bar',
  104. ), $this->bag->peekAll()
  105. );
  106. }
  107. public function testGet()
  108. {
  109. $this->assertEquals(array(), $this->bag->get('non_existing'));
  110. $this->assertEquals(array('default'), $this->bag->get('non_existing', array('default')));
  111. $this->assertEquals(array('A previous flash message'), $this->bag->get('notice'));
  112. $this->assertEquals(array(), $this->bag->get('notice'));
  113. }
  114. public function testSetAll()
  115. {
  116. $this->bag->setAll(array('a' => 'first', 'b' => 'second'));
  117. $this->assertFalse($this->bag->has('a'));
  118. $this->assertFalse($this->bag->has('b'));
  119. }
  120. public function testAll()
  121. {
  122. $this->bag->set('notice', 'Foo');
  123. $this->bag->set('error', 'Bar');
  124. $this->assertEquals(array(
  125. 'notice' => array('A previous flash message'),
  126. ), $this->bag->all()
  127. );
  128. $this->assertEquals(array(), $this->bag->all());
  129. }
  130. public function testClear()
  131. {
  132. $this->assertEquals(array('notice' => array('A previous flash message')), $this->bag->clear());
  133. }
  134. }