This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
SessionArrayStorageTest.php
169 lines (148 loc) · 5.02 KB
/
SessionArrayStorageTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Session
*/
namespace ZendTest\Session;
use Zend\Session\Storage\SessionArrayStorage;
use Zend\Session\Container;
/**
* @category Zend
* @package Zend_Session
* @subpackage UnitTests
* @group Zend_Session
*/
class SessionArrayStorageTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$_SESSION = array();
$this->storage = new SessionArrayStorage;
}
public function tearDown()
{
$_SESSION = array();
}
public function testStorageWritesToSessionSuperglobal()
{
$this->storage['foo'] = 'bar';
$this->assertSame($_SESSION['foo'], $this->storage->foo);
unset($this->storage['foo']);
$this->assertFalse(array_key_exists('foo', $_SESSION));
}
public function testPassingArrayToConstructorOverwritesSessionSuperglobal()
{
$_SESSION['foo'] = 'bar';
$array = array('foo' => 'FOO');
$storage = new SessionArrayStorage($array);
$expected = array(
'foo' => 'FOO',
'__ZF' => array(
'_REQUEST_ACCESS_TIME' => $storage->getRequestAccessTime(),
),
);
$this->assertSame($expected, $_SESSION);
}
public function testModifyingSessionSuperglobalDirectlyUpdatesStorage()
{
$_SESSION['foo'] = 'bar';
$this->assertTrue(isset($this->storage['foo']));
}
public function testDestructorSetsSessionToArray()
{
$this->storage->foo = 'bar';
$expected = array(
'__ZF' => array(
'_REQUEST_ACCESS_TIME' => $this->storage->getRequestAccessTime(),
),
'foo' => 'bar',
);
$this->storage->__destruct();
$this->assertSame($expected, $_SESSION);
}
public function testModifyingOneSessionObjectModifiesTheOther()
{
$this->storage->foo = 'bar';
$storage = new SessionArrayStorage();
$storage->bar = 'foo';
$this->assertEquals('foo', $this->storage->bar);
}
public function testMarkingOneSessionObjectImmutableShouldMarkOtherInstancesImmutable()
{
$this->storage->foo = 'bar';
$storage = new SessionArrayStorage();
$this->assertEquals('bar', $storage['foo']);
$this->storage->markImmutable();
$this->assertTrue($storage->isImmutable(), var_export($_SESSION, 1));
}
public function testAssignment()
{
$_SESSION['foo'] = 'bar';
$this->assertEquals('bar', $this->storage['foo']);
}
public function testMultiDimensionalAssignment()
{
$_SESSION['foo']['bar'] = 'baz';
$this->assertEquals('baz', $this->storage['foo']['bar']);
}
public function testUnset()
{
$_SESSION['foo'] = 'bar';
unset($_SESSION['foo']);
$this->assertFalse(isset($this->storage['foo']));
}
public function testMultiDimensionalUnset()
{
if (version_compare(PHP_VERSION, '5.3.4') < 0) {
$this->markTestSkipped('Known issue on versions of PHP less than 5.3.4');
}
$this->storage['foo'] = array('bar' => array('baz' => 'boo'));
unset($this->storage['foo']['bar']['baz']);
$this->assertFalse(isset($this->storage['foo']['bar']['baz']));
unset($this->storage['foo']['bar']);
$this->assertFalse(isset($this->storage['foo']['bar']));
}
public function testSessionWorksWithContainer()
{
$container = new Container('test');
$container->foo = 'bar';
$this->assertSame($container->foo, $_SESSION['test']['foo']);
}
public function testToArrayWithMetaData()
{
$this->storage->foo = 'bar';
$this->storage->bar = 'baz';
$this->storage->setMetadata('foo', 'bar');
$expected = array(
'__ZF' => array(
'_REQUEST_ACCESS_TIME' => $this->storage->getRequestAccessTime(),
'foo' => 'bar',
),
'foo' => 'bar',
'bar' => 'baz',
);
$this->assertSame($expected, $this->storage->toArray(true));
}
public function testUndefinedSessionManipulation()
{
if (version_compare(PHP_VERSION, '5.3.4') < 0) {
$this->markTestSkipped('Known issue on versions of PHP less than 5.3.4');
}
$this->storage['foo'] = 'bar';
$this->storage['bar'][] = 'bar';
$this->storage['baz']['foo'] = 'bar';
$expected = array(
'__ZF' => array(
'_REQUEST_ACCESS_TIME' => $this->storage->getRequestAccessTime(),
),
'foo' => 'bar',
'bar' => array('bar'),
'baz' => array('foo' => 'bar'),
);
$this->assertSame($expected, $this->storage->toArray(true));
}
}