4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests\Session\Flash;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
20 * @author Drak <drak@zikula.org>
22 class FlashBagTest extends TestCase
25 * @var \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface
29 protected $array = array();
31 protected function setUp()
34 $this->bag = new FlashBag();
35 $this->array = array('notice' => array('A previous flash message'));
36 $this->bag->initialize($this->array);
39 protected function tearDown()
45 public function testInitialize()
47 $bag = new FlashBag();
48 $bag->initialize($this->array);
49 $this->assertEquals($this->array, $bag->peekAll());
50 $array = array('should' => array('change'));
51 $bag->initialize($array);
52 $this->assertEquals($array, $bag->peekAll());
55 public function testGetStorageKey()
57 $this->assertEquals('_symfony_flashes', $this->bag->getStorageKey());
58 $attributeBag = new FlashBag('test');
59 $this->assertEquals('test', $attributeBag->getStorageKey());
62 public function testGetSetName()
64 $this->assertEquals('flashes', $this->bag->getName());
65 $this->bag->setName('foo');
66 $this->assertEquals('foo', $this->bag->getName());
69 public function testPeek()
71 $this->assertEquals(array(), $this->bag->peek('non_existing'));
72 $this->assertEquals(array('default'), $this->bag->peek('not_existing', array('default')));
73 $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
74 $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
77 public function testAdd()
79 $tab = array('bar' => 'baz');
80 $this->bag->add('string_message', 'lorem');
81 $this->bag->add('object_message', new \stdClass());
82 $this->bag->add('array_message', $tab);
84 $this->assertEquals(array('lorem'), $this->bag->get('string_message'));
85 $this->assertEquals(array(new \stdClass()), $this->bag->get('object_message'));
86 $this->assertEquals(array($tab), $this->bag->get('array_message'));
89 public function testGet()
91 $this->assertEquals(array(), $this->bag->get('non_existing'));
92 $this->assertEquals(array('default'), $this->bag->get('not_existing', array('default')));
93 $this->assertEquals(array('A previous flash message'), $this->bag->get('notice'));
94 $this->assertEquals(array(), $this->bag->get('notice'));
97 public function testAll()
99 $this->bag->set('notice', 'Foo');
100 $this->bag->set('error', 'Bar');
101 $this->assertEquals(array(
102 'notice' => array('Foo'),
103 'error' => array('Bar'), ), $this->bag->all()
106 $this->assertEquals(array(), $this->bag->all());
109 public function testSet()
111 $this->bag->set('notice', 'Foo');
112 $this->bag->set('notice', 'Bar');
113 $this->assertEquals(array('Bar'), $this->bag->peek('notice'));
116 public function testHas()
118 $this->assertFalse($this->bag->has('nothing'));
119 $this->assertTrue($this->bag->has('notice'));
122 public function testKeys()
124 $this->assertEquals(array('notice'), $this->bag->keys());
127 public function testSetAll()
129 $this->bag->add('one_flash', 'Foo');
130 $this->bag->add('another_flash', 'Bar');
131 $this->assertTrue($this->bag->has('one_flash'));
132 $this->assertTrue($this->bag->has('another_flash'));
133 $this->bag->setAll(array('unique_flash' => 'FooBar'));
134 $this->assertFalse($this->bag->has('one_flash'));
135 $this->assertFalse($this->bag->has('another_flash'));
136 $this->assertSame(array('unique_flash' => 'FooBar'), $this->bag->all());
137 $this->assertSame(array(), $this->bag->all());
140 public function testPeekAll()
142 $this->bag->set('notice', 'Foo');
143 $this->bag->set('error', 'Bar');
144 $this->assertEquals(array(
145 'notice' => array('Foo'),
146 'error' => array('Bar'),
147 ), $this->bag->peekAll()
149 $this->assertTrue($this->bag->has('notice'));
150 $this->assertTrue($this->bag->has('error'));
151 $this->assertEquals(array(
152 'notice' => array('Foo'),
153 'error' => array('Bar'),
154 ), $this->bag->peekAll()