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