Open Monograph Press  3.3.0
MockFileSessionStorageTest.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;
18 
24 class MockFileSessionStorageTest extends TestCase
25 {
29  private $sessionDir;
30 
34  protected $storage;
35 
36  protected function setUp()
37  {
38  $this->sessionDir = sys_get_temp_dir().'/sf2test';
39  $this->storage = $this->getStorage();
40  }
41 
42  protected function tearDown()
43  {
44  $this->sessionDir = null;
45  $this->storage = null;
46  array_map('unlink', glob($this->sessionDir.'/*.session'));
47  if (is_dir($this->sessionDir)) {
48  rmdir($this->sessionDir);
49  }
50  }
51 
52  public function testStart()
53  {
54  $this->assertEquals('', $this->storage->getId());
55  $this->assertTrue($this->storage->start());
56  $id = $this->storage->getId();
57  $this->assertNotEquals('', $this->storage->getId());
58  $this->assertTrue($this->storage->start());
59  $this->assertEquals($id, $this->storage->getId());
60  }
61 
62  public function testRegenerate()
63  {
64  $this->storage->start();
65  $this->storage->getBag('attributes')->set('regenerate', 1234);
66  $this->storage->regenerate();
67  $this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
68  $this->storage->regenerate(true);
69  $this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
70  }
71 
72  public function testGetId()
73  {
74  $this->assertEquals('', $this->storage->getId());
75  $this->storage->start();
76  $this->assertNotEquals('', $this->storage->getId());
77  }
78 
79  public function testSave()
80  {
81  $this->storage->start();
82  $id = $this->storage->getId();
83  $this->assertNotEquals('108', $this->storage->getBag('attributes')->get('new'));
84  $this->assertFalse($this->storage->getBag('flashes')->has('newkey'));
85  $this->storage->getBag('attributes')->set('new', '108');
86  $this->storage->getBag('flashes')->set('newkey', 'test');
87  $this->storage->save();
88 
89  $storage = $this->getStorage();
90  $storage->setId($id);
91  $storage->start();
92  $this->assertEquals('108', $storage->getBag('attributes')->get('new'));
93  $this->assertTrue($storage->getBag('flashes')->has('newkey'));
94  $this->assertEquals(array('test'), $storage->getBag('flashes')->peek('newkey'));
95  }
96 
97  public function testMultipleInstances()
98  {
99  $storage1 = $this->getStorage();
100  $storage1->start();
101  $storage1->getBag('attributes')->set('foo', 'bar');
102  $storage1->save();
103 
104  $storage2 = $this->getStorage();
105  $storage2->setId($storage1->getId());
106  $storage2->start();
107  $this->assertEquals('bar', $storage2->getBag('attributes')->get('foo'), 'values persist between instances');
108  }
109 
113  public function testSaveWithoutStart()
114  {
115  $storage1 = $this->getStorage();
116  $storage1->save();
117  }
118 
119  private function getStorage()
120  {
121  $storage = new MockFileSessionStorage($this->sessionDir);
122  $storage->registerBag(new FlashBag());
123  $storage->registerBag(new AttributeBag());
124 
125  return $storage;
126  }
127 }
Symfony\Component\HttpFoundation\Tests\Session\Storage\MockFileSessionStorageTest\testSave
testSave()
Definition: MockFileSessionStorageTest.php:85
Symfony\Component\HttpFoundation\Tests\Session\Storage\MockFileSessionStorageTest\testStart
testStart()
Definition: MockFileSessionStorageTest.php:58
Symfony\Component\HttpFoundation\Session\Flash\FlashBag
Definition: lib/vendor/symfony/http-foundation/Session/Flash/FlashBag.php:19
Symfony\Component\HttpFoundation\Tests\Session\Storage\MockFileSessionStorageTest\testSaveWithoutStart
testSaveWithoutStart()
Definition: MockFileSessionStorageTest.php:119
Symfony\Component\HttpFoundation\Tests\Session\Storage\MockFileSessionStorageTest\testRegenerate
testRegenerate()
Definition: MockFileSessionStorageTest.php:68
Symfony\Component\HttpFoundation\Tests\Session\Storage\MockFileSessionStorageTest
Definition: MockFileSessionStorageTest.php:24
Symfony\Component\HttpFoundation\Tests\Session\Storage\MockFileSessionStorageTest\setUp
setUp()
Definition: MockFileSessionStorageTest.php:42
Symfony\Component\HttpFoundation\Tests\Session\Storage\MockFileSessionStorageTest\testMultipleInstances
testMultipleInstances()
Definition: MockFileSessionStorageTest.php:103
Symfony\Component\HttpFoundation\Tests\Session\Storage
Symfony\Component\HttpFoundation\Tests\Session\Storage\MockFileSessionStorageTest\testGetId
testGetId()
Definition: MockFileSessionStorageTest.php:78
Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage
Definition: lib/vendor/symfony/http-foundation/Session/Storage/MockFileSessionStorage.php:25
Symfony\Component\HttpFoundation\Tests\Session\Storage\MockFileSessionStorageTest\tearDown
tearDown()
Definition: MockFileSessionStorageTest.php:48
Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag
Definition: lib/vendor/symfony/http-foundation/Session/Attribute/AttributeBag.php:17
Symfony\Component\HttpFoundation\Tests\Session\Storage\MockFileSessionStorageTest\$storage
$storage
Definition: MockFileSessionStorageTest.php:40