FlashBagTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\FlashBag;
  12. /**
  13. * FlashBagTest.
  14. *
  15. * @author Drak <drak@zikula.org>
  16. */
  17. class FlashBagTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface
  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('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. $bag->initialize($this->array);
  43. $this->assertEquals($this->array, $bag->peekAll());
  44. $array = array('should' => array('change'));
  45. $bag->initialize($array);
  46. $this->assertEquals($array, $bag->peekAll());
  47. }
  48. public function testGetStorageKey()
  49. {
  50. $this->assertEquals('_sf2_flashes', $this->bag->getStorageKey());
  51. $attributeBag = new FlashBag('test');
  52. $this->assertEquals('test', $attributeBag->getStorageKey());
  53. }
  54. public function testGetSetName()
  55. {
  56. $this->assertEquals('flashes', $this->bag->getName());
  57. $this->bag->setName('foo');
  58. $this->assertEquals('foo', $this->bag->getName());
  59. }
  60. public function testPeek()
  61. {
  62. $this->assertEquals(array(), $this->bag->peek('non_existing'));
  63. $this->assertEquals(array('default'), $this->bag->peek('not_existing', array('default')));
  64. $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
  65. $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
  66. }
  67. public function testGet()
  68. {
  69. $this->assertEquals(array(), $this->bag->get('non_existing'));
  70. $this->assertEquals(array('default'), $this->bag->get('not_existing', array('default')));
  71. $this->assertEquals(array('A previous flash message'), $this->bag->get('notice'));
  72. $this->assertEquals(array(), $this->bag->get('notice'));
  73. }
  74. public function testAll()
  75. {
  76. $this->bag->set('notice', 'Foo');
  77. $this->bag->set('error', 'Bar');
  78. $this->assertEquals(array(
  79. 'notice' => array('Foo'),
  80. 'error' => array('Bar'), ), $this->bag->all()
  81. );
  82. $this->assertEquals(array(), $this->bag->all());
  83. }
  84. public function testSet()
  85. {
  86. $this->bag->set('notice', 'Foo');
  87. $this->bag->set('notice', 'Bar');
  88. $this->assertEquals(array('Bar'), $this->bag->peek('notice'));
  89. }
  90. public function testHas()
  91. {
  92. $this->assertFalse($this->bag->has('nothing'));
  93. $this->assertTrue($this->bag->has('notice'));
  94. }
  95. public function testKeys()
  96. {
  97. $this->assertEquals(array('notice'), $this->bag->keys());
  98. }
  99. public function testPeekAll()
  100. {
  101. $this->bag->set('notice', 'Foo');
  102. $this->bag->set('error', 'Bar');
  103. $this->assertEquals(array(
  104. 'notice' => array('Foo'),
  105. 'error' => array('Bar'),
  106. ), $this->bag->peekAll()
  107. );
  108. $this->assertTrue($this->bag->has('notice'));
  109. $this->assertTrue($this->bag->has('error'));
  110. $this->assertEquals(array(
  111. 'notice' => array('Foo'),
  112. 'error' => array('Bar'),
  113. ), $this->bag->peekAll()
  114. );
  115. }
  116. /**
  117. * @group legacy
  118. */
  119. public function testLegacyGetIterator()
  120. {
  121. $flashes = array('hello' => 'world', 'beep' => 'boop', 'notice' => 'nope');
  122. foreach ($flashes as $key => $val) {
  123. $this->bag->set($key, $val);
  124. }
  125. $i = 0;
  126. foreach ($this->bag as $key => $val) {
  127. $this->assertEquals(array($flashes[$key]), $val);
  128. ++$i;
  129. }
  130. $this->assertEquals(count($flashes), $i);
  131. $this->assertCount(0, $this->bag->all());
  132. }
  133. }