Open Monograph Press  3.3.0
SessionTest.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;
19 
27 class SessionTest extends TestCase
28 {
32  protected $storage;
33 
37  protected $session;
38 
39  protected function setUp()
40  {
41  $this->storage = new MockArraySessionStorage();
42  $this->session = new Session($this->storage, new AttributeBag(), new FlashBag());
43  }
44 
45  protected function tearDown()
46  {
47  $this->storage = null;
48  $this->session = null;
49  }
50 
51  public function testStart()
52  {
53  $this->assertEquals('', $this->session->getId());
54  $this->assertTrue($this->session->start());
55  $this->assertNotEquals('', $this->session->getId());
56  }
57 
58  public function testIsStarted()
59  {
60  $this->assertFalse($this->session->isStarted());
61  $this->session->start();
62  $this->assertTrue($this->session->isStarted());
63  }
64 
65  public function testSetId()
66  {
67  $this->assertEquals('', $this->session->getId());
68  $this->session->setId('0123456789abcdef');
69  $this->session->start();
70  $this->assertEquals('0123456789abcdef', $this->session->getId());
71  }
72 
73  public function testSetName()
74  {
75  $this->assertEquals('MOCKSESSID', $this->session->getName());
76  $this->session->setName('session.test.com');
77  $this->session->start();
78  $this->assertEquals('session.test.com', $this->session->getName());
79  }
80 
81  public function testGet()
82  {
83  // tests defaults
84  $this->assertNull($this->session->get('foo'));
85  $this->assertEquals(1, $this->session->get('foo', 1));
86  }
87 
91  public function testSet($key, $value)
92  {
93  $this->session->set($key, $value);
94  $this->assertEquals($value, $this->session->get($key));
95  }
96 
100  public function testHas($key, $value)
101  {
102  $this->session->set($key, $value);
103  $this->assertTrue($this->session->has($key));
104  $this->assertFalse($this->session->has($key.'non_value'));
105  }
106 
107  public function testReplace()
108  {
109  $this->session->replace(array('happiness' => 'be good', 'symfony' => 'awesome'));
110  $this->assertEquals(array('happiness' => 'be good', 'symfony' => 'awesome'), $this->session->all());
111  $this->session->replace(array());
112  $this->assertEquals(array(), $this->session->all());
113  }
114 
118  public function testAll($key, $value, $result)
119  {
120  $this->session->set($key, $value);
121  $this->assertEquals($result, $this->session->all());
122  }
123 
127  public function testClear($key, $value)
128  {
129  $this->session->set('hi', 'fabien');
130  $this->session->set($key, $value);
131  $this->session->clear();
132  $this->assertEquals(array(), $this->session->all());
133  }
134 
135  public function setProvider()
136  {
137  return array(
138  array('foo', 'bar', array('foo' => 'bar')),
139  array('foo.bar', 'too much beer', array('foo.bar' => 'too much beer')),
140  array('great', 'symfony is great', array('great' => 'symfony is great')),
141  );
142  }
143 
147  public function testRemove($key, $value)
148  {
149  $this->session->set('hi.world', 'have a nice day');
150  $this->session->set($key, $value);
151  $this->session->remove($key);
152  $this->assertEquals(array('hi.world' => 'have a nice day'), $this->session->all());
153  }
154 
155  public function testInvalidate()
156  {
157  $this->session->set('invalidate', 123);
158  $this->session->invalidate();
159  $this->assertEquals(array(), $this->session->all());
160  }
161 
162  public function testMigrate()
163  {
164  $this->session->set('migrate', 321);
165  $this->session->migrate();
166  $this->assertEquals(321, $this->session->get('migrate'));
167  }
168 
169  public function testMigrateDestroy()
170  {
171  $this->session->set('migrate', 333);
172  $this->session->migrate(true);
173  $this->assertEquals(333, $this->session->get('migrate'));
174  }
175 
176  public function testSave()
177  {
178  $this->session->start();
179  $this->session->save();
180 
181  $this->assertFalse($this->session->isStarted());
182  }
183 
184  public function testGetId()
185  {
186  $this->assertEquals('', $this->session->getId());
187  $this->session->start();
188  $this->assertNotEquals('', $this->session->getId());
189  }
190 
191  public function testGetFlashBag()
192  {
193  $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface', $this->session->getFlashBag());
194  }
195 
196  public function testGetIterator()
197  {
198  $attributes = array('hello' => 'world', 'symfony' => 'rocks');
199  foreach ($attributes as $key => $val) {
200  $this->session->set($key, $val);
201  }
202 
203  $i = 0;
204  foreach ($this->session as $key => $val) {
205  $this->assertEquals($attributes[$key], $val);
206  ++$i;
207  }
208 
209  $this->assertEquals(count($attributes), $i);
210  }
211 
212  public function testGetCount()
213  {
214  $this->session->set('hello', 'world');
215  $this->session->set('symfony', 'rocks');
216 
217  $this->assertCount(2, $this->session);
218  }
219 
220  public function testGetMeta()
221  {
222  $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\MetadataBag', $this->session->getMetadataBag());
223  }
224 }
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\tearDown
tearDown()
Definition: SessionTest.php:51
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testGetFlashBag
testGetFlashBag()
Definition: SessionTest.php:197
Symfony\Component\HttpFoundation\Session\Flash\FlashBag
Definition: lib/vendor/symfony/http-foundation/Session/Flash/FlashBag.php:19
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testHas
testHas($key, $value)
Definition: SessionTest.php:106
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testMigrate
testMigrate()
Definition: SessionTest.php:168
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testGetIterator
testGetIterator()
Definition: SessionTest.php:202
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testMigrateDestroy
testMigrateDestroy()
Definition: SessionTest.php:175
Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage
Definition: lib/vendor/symfony/http-foundation/Session/Storage/MockArraySessionStorage.php:28
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testSetId
testSetId()
Definition: SessionTest.php:71
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testClear
testClear($key, $value)
Definition: SessionTest.php:133
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testSet
testSet($key, $value)
Definition: SessionTest.php:97
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testGetMeta
testGetMeta()
Definition: SessionTest.php:226
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\$session
$session
Definition: SessionTest.php:43
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testStart
testStart()
Definition: SessionTest.php:57
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\setProvider
setProvider()
Definition: SessionTest.php:141
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testRemove
testRemove($key, $value)
Definition: SessionTest.php:153
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testGetCount
testGetCount()
Definition: SessionTest.php:218
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testInvalidate
testInvalidate()
Definition: SessionTest.php:161
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\setUp
setUp()
Definition: SessionTest.php:45
Symfony\Component\HttpFoundation\Tests\Session\SessionTest
Definition: SessionTest.php:27
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testIsStarted
testIsStarted()
Definition: SessionTest.php:64
Symfony\Component\HttpFoundation\Session\Session
Definition: lib/vendor/symfony/http-foundation/Session/Session.php:27
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testSetName
testSetName()
Definition: SessionTest.php:79
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testReplace
testReplace()
Definition: SessionTest.php:113
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\$storage
$storage
Definition: SessionTest.php:35
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testSave
testSave()
Definition: SessionTest.php:182
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testGet
testGet()
Definition: SessionTest.php:87
Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag
Definition: lib/vendor/symfony/http-foundation/Session/Attribute/AttributeBag.php:17
Symfony\Component\HttpFoundation\Tests\Session
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testAll
testAll($key, $value, $result)
Definition: SessionTest.php:124
Symfony\Component\HttpFoundation\Tests\Session\SessionTest\testGetId
testGetId()
Definition: SessionTest.php:190
Symfony\Component\HttpFoundation\Session\Session\count
count()
Definition: lib/vendor/symfony/http-foundation/Session/Session.php:162