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\FlashBag; 16 17/** 18 * FlashBagTest. 19 * 20 * @author Drak <drak@zikula.org> 21 */ 22class FlashBagTest extends TestCase 23{ 24 /** 25 * @var \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface 26 */ 27 private $bag; 28 29 protected $array = []; 30 31 protected function setUp() 32 { 33 parent::setUp(); 34 $this->bag = new FlashBag(); 35 $this->array = ['notice' => ['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 $bag->initialize($this->array); 49 $this->assertEquals($this->array, $bag->peekAll()); 50 $array = ['should' => ['change']]; 51 $bag->initialize($array); 52 $this->assertEquals($array, $bag->peekAll()); 53 } 54 55 public function testGetStorageKey() 56 { 57 $this->assertEquals('_symfony_flashes', $this->bag->getStorageKey()); 58 $attributeBag = new FlashBag('test'); 59 $this->assertEquals('test', $attributeBag->getStorageKey()); 60 } 61 62 public function testGetSetName() 63 { 64 $this->assertEquals('flashes', $this->bag->getName()); 65 $this->bag->setName('foo'); 66 $this->assertEquals('foo', $this->bag->getName()); 67 } 68 69 public function testPeek() 70 { 71 $this->assertEquals([], $this->bag->peek('non_existing')); 72 $this->assertEquals(['default'], $this->bag->peek('not_existing', ['default'])); 73 $this->assertEquals(['A previous flash message'], $this->bag->peek('notice')); 74 $this->assertEquals(['A previous flash message'], $this->bag->peek('notice')); 75 } 76 77 public function testAdd() 78 { 79 $tab = ['bar' => 'baz']; 80 $this->bag->add('string_message', 'lorem'); 81 $this->bag->add('object_message', new \stdClass()); 82 $this->bag->add('array_message', $tab); 83 84 $this->assertEquals(['lorem'], $this->bag->get('string_message')); 85 $this->assertEquals([new \stdClass()], $this->bag->get('object_message')); 86 $this->assertEquals([$tab], $this->bag->get('array_message')); 87 } 88 89 public function testGet() 90 { 91 $this->assertEquals([], $this->bag->get('non_existing')); 92 $this->assertEquals(['default'], $this->bag->get('not_existing', ['default'])); 93 $this->assertEquals(['A previous flash message'], $this->bag->get('notice')); 94 $this->assertEquals([], $this->bag->get('notice')); 95 } 96 97 public function testAll() 98 { 99 $this->bag->set('notice', 'Foo'); 100 $this->bag->set('error', 'Bar'); 101 $this->assertEquals([ 102 'notice' => ['Foo'], 103 'error' => ['Bar'], ], $this->bag->all() 104 ); 105 106 $this->assertEquals([], $this->bag->all()); 107 } 108 109 public function testSet() 110 { 111 $this->bag->set('notice', 'Foo'); 112 $this->bag->set('notice', 'Bar'); 113 $this->assertEquals(['Bar'], $this->bag->peek('notice')); 114 } 115 116 public function testHas() 117 { 118 $this->assertFalse($this->bag->has('nothing')); 119 $this->assertTrue($this->bag->has('notice')); 120 } 121 122 public function testKeys() 123 { 124 $this->assertEquals(['notice'], $this->bag->keys()); 125 } 126 127 public function testSetAll() 128 { 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(['unique_flash' => 'FooBar']); 134 $this->assertFalse($this->bag->has('one_flash')); 135 $this->assertFalse($this->bag->has('another_flash')); 136 $this->assertSame(['unique_flash' => 'FooBar'], $this->bag->all()); 137 $this->assertSame([], $this->bag->all()); 138 } 139 140 public function testPeekAll() 141 { 142 $this->bag->set('notice', 'Foo'); 143 $this->bag->set('error', 'Bar'); 144 $this->assertEquals([ 145 'notice' => ['Foo'], 146 'error' => ['Bar'], 147 ], $this->bag->peekAll() 148 ); 149 $this->assertTrue($this->bag->has('notice')); 150 $this->assertTrue($this->bag->has('error')); 151 $this->assertEquals([ 152 'notice' => ['Foo'], 153 'error' => ['Bar'], 154 ], $this->bag->peekAll() 155 ); 156 } 157} 158