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\Attribute;
13
14use PHPUnit\Framework\TestCase;
15use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
16
17/**
18 * Tests NamespacedAttributeBag.
19 *
20 * @author Drak <drak@zikula.org>
21 */
22class NamespacedAttributeBagTest extends TestCase
23{
24    private $array = [];
25
26    /**
27     * @var NamespacedAttributeBag
28     */
29    private $bag;
30
31    protected function setUp()
32    {
33        $this->array = [
34            'hello' => 'world',
35            'always' => 'be happy',
36            'user.login' => 'drak',
37            'csrf.token' => [
38                'a' => '1234',
39                'b' => '4321',
40            ],
41            'category' => [
42                'fishing' => [
43                    'first' => 'cod',
44                    'second' => 'sole',
45                ],
46            ],
47        ];
48        $this->bag = new NamespacedAttributeBag('_sf2', '/');
49        $this->bag->initialize($this->array);
50    }
51
52    protected function tearDown()
53    {
54        $this->bag = null;
55        $this->array = [];
56    }
57
58    public function testInitialize()
59    {
60        $bag = new NamespacedAttributeBag();
61        $bag->initialize($this->array);
62        $this->assertEquals($this->array, $this->bag->all());
63        $array = ['should' => 'not stick'];
64        $bag->initialize($array);
65
66        // should have remained the same
67        $this->assertEquals($this->array, $this->bag->all());
68    }
69
70    public function testGetStorageKey()
71    {
72        $this->assertEquals('_sf2', $this->bag->getStorageKey());
73        $attributeBag = new NamespacedAttributeBag('test');
74        $this->assertEquals('test', $attributeBag->getStorageKey());
75    }
76
77    /**
78     * @dataProvider attributesProvider
79     */
80    public function testHas($key, $value, $exists)
81    {
82        $this->assertEquals($exists, $this->bag->has($key));
83    }
84
85    /**
86     * @dataProvider attributesProvider
87     */
88    public function testHasNoSideEffect($key, $value, $expected)
89    {
90        $expected = json_encode($this->bag->all());
91        $this->bag->has($key);
92
93        $this->assertEquals($expected, json_encode($this->bag->all()));
94    }
95
96    /**
97     * @dataProvider attributesProvider
98     */
99    public function testGet($key, $value, $expected)
100    {
101        $this->assertEquals($value, $this->bag->get($key));
102    }
103
104    public function testGetDefaults()
105    {
106        $this->assertNull($this->bag->get('user2.login'));
107        $this->assertEquals('default', $this->bag->get('user2.login', 'default'));
108    }
109
110    /**
111     * @dataProvider attributesProvider
112     */
113    public function testGetNoSideEffect($key, $value, $expected)
114    {
115        $expected = json_encode($this->bag->all());
116        $this->bag->get($key);
117
118        $this->assertEquals($expected, json_encode($this->bag->all()));
119    }
120
121    /**
122     * @dataProvider attributesProvider
123     */
124    public function testSet($key, $value, $expected)
125    {
126        $this->bag->set($key, $value);
127        $this->assertEquals($value, $this->bag->get($key));
128    }
129
130    public function testAll()
131    {
132        $this->assertEquals($this->array, $this->bag->all());
133
134        $this->bag->set('hello', 'fabien');
135        $array = $this->array;
136        $array['hello'] = 'fabien';
137        $this->assertEquals($array, $this->bag->all());
138    }
139
140    public function testReplace()
141    {
142        $array = [];
143        $array['name'] = 'jack';
144        $array['foo.bar'] = 'beep';
145        $this->bag->replace($array);
146        $this->assertEquals($array, $this->bag->all());
147        $this->assertNull($this->bag->get('hello'));
148        $this->assertNull($this->bag->get('always'));
149        $this->assertNull($this->bag->get('user.login'));
150    }
151
152    public function testRemove()
153    {
154        $this->assertEquals('world', $this->bag->get('hello'));
155        $this->bag->remove('hello');
156        $this->assertNull($this->bag->get('hello'));
157
158        $this->assertEquals('be happy', $this->bag->get('always'));
159        $this->bag->remove('always');
160        $this->assertNull($this->bag->get('always'));
161
162        $this->assertEquals('drak', $this->bag->get('user.login'));
163        $this->bag->remove('user.login');
164        $this->assertNull($this->bag->get('user.login'));
165    }
166
167    public function testRemoveExistingNamespacedAttribute()
168    {
169        $this->assertSame('cod', $this->bag->remove('category/fishing/first'));
170    }
171
172    public function testRemoveNonexistingNamespacedAttribute()
173    {
174        $this->assertNull($this->bag->remove('foo/bar/baz'));
175    }
176
177    public function testClear()
178    {
179        $this->bag->clear();
180        $this->assertEquals([], $this->bag->all());
181    }
182
183    public function attributesProvider()
184    {
185        return [
186            ['hello', 'world', true],
187            ['always', 'be happy', true],
188            ['user.login', 'drak', true],
189            ['csrf.token', ['a' => '1234', 'b' => '4321'], true],
190            ['csrf.token/a', '1234', true],
191            ['csrf.token/b', '4321', true],
192            ['category', ['fishing' => ['first' => 'cod', 'second' => 'sole']], true],
193            ['category/fishing', ['first' => 'cod', 'second' => 'sole'], true],
194            ['category/fishing/missing/first', null, false],
195            ['category/fishing/first', 'cod', true],
196            ['category/fishing/second', 'sole', true],
197            ['category/fishing/missing/second', null, false],
198            ['user2.login', null, false],
199            ['never', null, false],
200            ['bye', null, false],
201            ['bye/for/now', null, false],
202        ];
203    }
204}
205