菜谱项目

FlashBagTest.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 = array();
  25. protected function setUp()
  26. {
  27. parent::setUp();
  28. $this->bag = new FlashBag();
  29. $this->array = array('notice' => array('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 = array('should' => array('change'));
  43. $bag->initialize($array);
  44. $this->assertEquals($array, $bag->peekAll());
  45. }
  46. public function testGetStorageKey()
  47. {
  48. $this->assertEquals('_sf2_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(array(), $this->bag->peek('non_existing'));
  61. $this->assertEquals(array('default'), $this->bag->peek('not_existing', array('default')));
  62. $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
  63. $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
  64. }
  65. public function testGet()
  66. {
  67. $this->assertEquals(array(), $this->bag->get('non_existing'));
  68. $this->assertEquals(array('default'), $this->bag->get('not_existing', array('default')));
  69. $this->assertEquals(array('A previous flash message'), $this->bag->get('notice'));
  70. $this->assertEquals(array(), $this->bag->get('notice'));
  71. }
  72. public function testAll()
  73. {
  74. $this->bag->set('notice', 'Foo');
  75. $this->bag->set('error', 'Bar');
  76. $this->assertEquals(array(
  77. 'notice' => array('Foo'),
  78. 'error' => array('Bar'), ), $this->bag->all()
  79. );
  80. $this->assertEquals(array(), $this->bag->all());
  81. }
  82. public function testSet()
  83. {
  84. $this->bag->set('notice', 'Foo');
  85. $this->bag->set('notice', 'Bar');
  86. $this->assertEquals(array('Bar'), $this->bag->peek('notice'));
  87. }
  88. public function testHas()
  89. {
  90. $this->assertFalse($this->bag->has('nothing'));
  91. $this->assertTrue($this->bag->has('notice'));
  92. }
  93. public function testKeys()
  94. {
  95. $this->assertEquals(array('notice'), $this->bag->keys());
  96. }
  97. public function testPeekAll()
  98. {
  99. $this->bag->set('notice', 'Foo');
  100. $this->bag->set('error', 'Bar');
  101. $this->assertEquals(array(
  102. 'notice' => array('Foo'),
  103. 'error' => array('Bar'),
  104. ), $this->bag->peekAll()
  105. );
  106. $this->assertTrue($this->bag->has('notice'));
  107. $this->assertTrue($this->bag->has('error'));
  108. $this->assertEquals(array(
  109. 'notice' => array('Foo'),
  110. 'error' => array('Bar'),
  111. ), $this->bag->peekAll()
  112. );
  113. }
  114. }