Open Journal Systems  3.3.0
AttributeBagTest.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 AttributeBagTest 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 AttributeBag('_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 AttributeBag();
64  $bag->initialize($this->array);
65  $this->assertEquals($this->array, $bag->all());
66  $array = array('should' => 'change');
67  $bag->initialize($array);
68  $this->assertEquals($array, $bag->all());
69  }
70 
71  public function testGetStorageKey()
72  {
73  $this->assertEquals('_sf2', $this->bag->getStorageKey());
74  $attributeBag = new AttributeBag('test');
75  $this->assertEquals('test', $attributeBag->getStorageKey());
76  }
77 
78  public function testGetSetName()
79  {
80  $this->assertEquals('attributes', $this->bag->getName());
81  $this->bag->setName('foo');
82  $this->assertEquals('foo', $this->bag->getName());
83  }
84 
88  public function testHas($key, $value, $exists)
89  {
90  $this->assertEquals($exists, $this->bag->has($key));
91  }
92 
96  public function testGet($key, $value, $expected)
97  {
98  $this->assertEquals($value, $this->bag->get($key));
99  }
100 
101  public function testGetDefaults()
102  {
103  $this->assertNull($this->bag->get('user2.login'));
104  $this->assertEquals('default', $this->bag->get('user2.login', 'default'));
105  }
106 
110  public function testSet($key, $value, $expected)
111  {
112  $this->bag->set($key, $value);
113  $this->assertEquals($value, $this->bag->get($key));
114  }
115 
116  public function testAll()
117  {
118  $this->assertEquals($this->array, $this->bag->all());
119 
120  $this->bag->set('hello', 'fabien');
121  $array = $this->array;
122  $array['hello'] = 'fabien';
123  $this->assertEquals($array, $this->bag->all());
124  }
125 
126  public function testReplace()
127  {
128  $array = array();
129  $array['name'] = 'jack';
130  $array['foo.bar'] = 'beep';
131  $this->bag->replace($array);
132  $this->assertEquals($array, $this->bag->all());
133  $this->assertNull($this->bag->get('hello'));
134  $this->assertNull($this->bag->get('always'));
135  $this->assertNull($this->bag->get('user.login'));
136  }
137 
138  public function testRemove()
139  {
140  $this->assertEquals('world', $this->bag->get('hello'));
141  $this->bag->remove('hello');
142  $this->assertNull($this->bag->get('hello'));
143 
144  $this->assertEquals('be happy', $this->bag->get('always'));
145  $this->bag->remove('always');
146  $this->assertNull($this->bag->get('always'));
147 
148  $this->assertEquals('drak', $this->bag->get('user.login'));
149  $this->bag->remove('user.login');
150  $this->assertNull($this->bag->get('user.login'));
151  }
152 
153  public function testClear()
154  {
155  $this->bag->clear();
156  $this->assertEquals(array(), $this->bag->all());
157  }
158 
159  public function attributesProvider()
160  {
161  return array(
162  array('hello', 'world', true),
163  array('always', 'be happy', true),
164  array('user.login', 'drak', true),
165  array('csrf.token', array('a' => '1234', 'b' => '4321'), true),
166  array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true),
167  array('user2.login', null, false),
168  array('never', null, false),
169  array('bye', null, false),
170  array('bye/for/now', null, false),
171  );
172  }
173 
174  public function testGetIterator()
175  {
176  $i = 0;
177  foreach ($this->bag as $key => $val) {
178  $this->assertEquals($this->array[$key], $val);
179  ++$i;
180  }
181 
182  $this->assertEquals(count($this->array), $i);
183  }
184 
185  public function testCount()
186  {
187  $this->assertEquals(count($this->array), count($this->bag));
188  }
189 }
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\testReplace
testReplace()
Definition: AttributeBagTest.php:132
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest
Definition: AttributeBagTest.php:22
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\testRemove
testRemove()
Definition: AttributeBagTest.php:144
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\testGetIterator
testGetIterator()
Definition: AttributeBagTest.php:180
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\testGetDefaults
testGetDefaults()
Definition: AttributeBagTest.php:107
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\testInitialize
testInitialize()
Definition: AttributeBagTest.php:67
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\testCount
testCount()
Definition: AttributeBagTest.php:191
Symfony\Component\HttpFoundation\Tests\Session\Attribute
Definition: AttributeBagTest.php:12
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\attributesProvider
attributesProvider()
Definition: AttributeBagTest.php:165
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\testGetSetName
testGetSetName()
Definition: AttributeBagTest.php:84
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\testGetStorageKey
testGetStorageKey()
Definition: AttributeBagTest.php:77
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\testClear
testClear()
Definition: AttributeBagTest.php:159
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\testHas
testHas($key, $value, $exists)
Definition: AttributeBagTest.php:94
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\tearDown
tearDown()
Definition: AttributeBagTest.php:61
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\testAll
testAll()
Definition: AttributeBagTest.php:122
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\setUp
setUp()
Definition: AttributeBagTest.php:40
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\testSet
testSet($key, $value, $expected)
Definition: AttributeBagTest.php:116
Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag
Definition: lib/vendor/symfony/http-foundation/Session/Attribute/AttributeBag.php:17
Symfony\Component\HttpFoundation\Session\Session\count
count()
Definition: lib/vendor/symfony/http-foundation/Session/Session.php:162
Symfony\Component\HttpFoundation\Tests\Session\Attribute\AttributeBagTest\testGet
testGet($key, $value, $expected)
Definition: AttributeBagTest.php:102