AutoExpireFlashBagTest.php 4.4 KB

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