1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\HttpFoundation\Tests\Session\Flash;
13
14use PHPUnit\Framework\TestCase;
15use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag as FlashBag;
16
17/**
18 * AutoExpireFlashBagTest.
19 *
20 * @author Drak <drak@zikula.org>
21 */
22class AutoExpireFlashBagTest extends TestCase
23{
24    /**
25     * @var \Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag
26     */
27    private $bag;
28
29    protected $array = array();
30
31    protected function setUp()
32    {
33        parent::setUp();
34        $this->bag = new FlashBag();
35        $this->array = array('new' => array('notice' => array('A previous flash message')));
36        $this->bag->initialize($this->array);
37    }
38
39    protected function tearDown()
40    {
41        $this->bag = null;
42        parent::tearDown();
43    }
44
45    public function testInitialize()
46    {
47        $bag = new FlashBag();
48        $array = array('new' => array('notice' => array('A previous flash message')));
49        $bag->initialize($array);
50        $this->assertEquals(array('A previous flash message'), $bag->peek('notice'));
51        $array = array('new' => array(
52                'notice' => array('Something else'),
53                'error' => array('a'),
54            ));
55        $bag->initialize($array);
56        $this->assertEquals(array('Something else'), $bag->peek('notice'));
57        $this->assertEquals(array('a'), $bag->peek('error'));
58    }
59
60    public function testGetStorageKey()
61    {
62        $this->assertEquals('_symfony_flashes', $this->bag->getStorageKey());
63        $attributeBag = new FlashBag('test');
64        $this->assertEquals('test', $attributeBag->getStorageKey());
65    }
66
67    public function testGetSetName()
68    {
69        $this->assertEquals('flashes', $this->bag->getName());
70        $this->bag->setName('foo');
71        $this->assertEquals('foo', $this->bag->getName());
72    }
73
74    public function testPeek()
75    {
76        $this->assertEquals(array(), $this->bag->peek('non_existing'));
77        $this->assertEquals(array('default'), $this->bag->peek('non_existing', array('default')));
78        $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
79        $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
80    }
81
82    public function testSet()
83    {
84        $this->bag->set('notice', 'Foo');
85        $this->assertEquals(array('A previous flash message'), $this->bag->peek('notice'));
86    }
87
88    public function testHas()
89    {
90        $this->assertFalse($this->bag->has('nothing'));
91        $this->assertTrue($this->bag->has('notice'));
92    }
93
94    public function testKeys()
95    {
96        $this->assertEquals(array('notice'), $this->bag->keys());
97    }
98
99    public function testPeekAll()
100    {
101        $array = array(
102            'new' => array(
103                'notice' => 'Foo',
104                'error' => 'Bar',
105            ),
106        );
107
108        $this->bag->initialize($array);
109        $this->assertEquals(array(
110            'notice' => 'Foo',
111            'error' => 'Bar',
112            ), $this->bag->peekAll()
113        );
114
115        $this->assertEquals(array(
116            'notice' => 'Foo',
117            'error' => 'Bar',
118            ), $this->bag->peekAll()
119        );
120    }
121
122    public function testGet()
123    {
124        $this->assertEquals(array(), $this->bag->get('non_existing'));
125        $this->assertEquals(array('default'), $this->bag->get('non_existing', array('default')));
126        $this->assertEquals(array('A previous flash message'), $this->bag->get('notice'));
127        $this->assertEquals(array(), $this->bag->get('notice'));
128    }
129
130    public function testSetAll()
131    {
132        $this->bag->setAll(array('a' => 'first', 'b' => 'second'));
133        $this->assertFalse($this->bag->has('a'));
134        $this->assertFalse($this->bag->has('b'));
135    }
136
137    public function testAll()
138    {
139        $this->bag->set('notice', 'Foo');
140        $this->bag->set('error', 'Bar');
141        $this->assertEquals(array(
142            'notice' => array('A previous flash message'),
143            ), $this->bag->all()
144        );
145
146        $this->assertEquals(array(), $this->bag->all());
147    }
148
149    public function testClear()
150    {
151        $this->assertEquals(array('notice' => array('A previous flash message')), $this->bag->clear());
152    }
153
154    public function testDoNotRemoveTheNewFlashesWhenDisplayingTheExistingOnes()
155    {
156        $this->bag->add('success', 'Something');
157        $this->bag->all();
158
159        $this->assertEquals(array('new' => array('success' => array('Something')), 'display' => array()), $this->array);
160    }
161}
162