This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
StorageTest.php
246 lines (214 loc) · 8.16 KB
/
StorageTest.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?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\ArrayStorage;
/**
* @category Zend
* @package Zend_Session
* @subpackage UnitTests
* @group Zend_Session
*/
class StorageTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ArrayStorage
*/
protected $storage;
public function setUp()
{
$this->storage = new ArrayStorage;
}
public function testStorageAllowsArrayAccess()
{
$this->storage['foo'] = 'bar';
$this->assertTrue(isset($this->storage['foo']));
$this->assertEquals('bar', $this->storage['foo']);
unset($this->storage['foo']);
$this->assertFalse(isset($this->storage['foo']));
}
public function testStorageAllowsPropertyAccess()
{
$this->storage->foo = 'bar';
$this->assertTrue(isset($this->storage->foo));
$this->assertEquals('bar', $this->storage->foo);
unset($this->storage->foo);
$this->assertFalse(isset($this->storage->foo));
}
public function testStorageAllowsSettingMetadata()
{
$this->storage->setMetadata('TEST', 'foo');
$this->assertEquals('foo', $this->storage->getMetadata('TEST'));
}
public function testSettingArrayMetadataMergesOnSubsequentRequests()
{
$this->storage->setMetadata('TEST', array('foo' => 'bar', 'bar' => 'baz'));
$this->storage->setMetadata('TEST', array('foo' => 'baz', 'baz' => 'bat', 'lonesome'));
$expected = array('foo' => 'baz', 'bar' => 'baz', 'baz' => 'bat', 'lonesome');
$this->assertEquals($expected, $this->storage->getMetadata('TEST'));
}
public function testSettingArrayMetadataWithOverwriteFlagOverwritesExistingData()
{
$this->storage->setMetadata('TEST', array('foo' => 'bar', 'bar' => 'baz'));
$this->storage->setMetadata('TEST', array('foo' => 'baz', 'baz' => 'bat', 'lonesome'), true);
$expected = array('foo' => 'baz', 'baz' => 'bat', 'lonesome');
$this->assertEquals($expected, $this->storage->getMetadata('TEST'));
}
public function testLockWithNoKeyMakesStorageReadOnly()
{
$this->storage->foo = 'bar';
$this->storage->lock();
$this->setExpectedException('Zend\Session\Exception\RuntimeException', 'Cannot set key "foo" due to locking');
$this->storage->foo = 'baz';
}
public function testLockWithNoKeyMarksEntireStorageLocked()
{
$this->storage->foo = 'bar';
$this->storage->bar = 'baz';
$this->storage->lock();
$this->assertTrue($this->storage->isLocked());
$this->assertTrue($this->storage->isLocked('foo'));
$this->assertTrue($this->storage->isLocked('bar'));
}
public function testLockWithKeyMakesOnlyThatKeyReadOnly()
{
$this->storage->foo = 'bar';
$this->storage->lock('foo');
$this->storage->bar = 'baz';
$this->assertEquals('baz', $this->storage->bar);
$this->setExpectedException('Zend\Session\Exception\RuntimeException', 'Cannot set key "foo" due to locking');
$this->storage->foo = 'baz';
}
public function testLockWithKeyMarksOnlyThatKeyLocked()
{
$this->storage->foo = 'bar';
$this->storage->bar = 'baz';
$this->storage->lock('foo');
$this->assertTrue($this->storage->isLocked('foo'));
$this->assertFalse($this->storage->isLocked('bar'));
}
public function testLockWithNoKeyShouldWriteToMetadata()
{
$this->storage->foo = 'bar';
$this->storage->lock();
$locked = $this->storage->getMetadata('_READONLY');
$this->assertTrue($locked);
}
public function testLockWithKeyShouldWriteToMetadata()
{
$this->storage->foo = 'bar';
$this->storage->lock('foo');
$locks = $this->storage->getMetadata('_LOCKS');
$this->assertTrue(is_array($locks));
$this->assertTrue(array_key_exists('foo', $locks));
}
public function testUnlockShouldUnlockEntireObject()
{
$this->storage->foo = 'bar';
$this->storage->bar = 'baz';
$this->storage->lock();
$this->storage->unlock();
$this->assertFalse($this->storage->isLocked('foo'));
$this->assertFalse($this->storage->isLocked('bar'));
}
public function testUnlockShouldUnlockSelectivelyLockedKeys()
{
$this->storage->foo = 'bar';
$this->storage->bar = 'baz';
$this->storage->lock('foo');
$this->storage->unlock();
$this->assertFalse($this->storage->isLocked('foo'));
$this->assertFalse($this->storage->isLocked('bar'));
}
public function testUnlockWithKeyShouldUnlockOnlyThatKey()
{
$this->storage->foo = 'bar';
$this->storage->bar = 'baz';
$this->storage->lock();
$this->storage->unlock('foo');
$this->assertFalse($this->storage->isLocked('foo'));
$this->assertTrue($this->storage->isLocked('bar'));
}
public function testUnlockWithKeyOfSelectiveLockShouldUnlockThatKey()
{
$this->storage->foo = 'bar';
$this->storage->lock('foo');
$this->storage->unlock('foo');
$this->assertFalse($this->storage->isLocked('foo'));
}
public function testClearWithNoArgumentsRemovesExistingData()
{
$this->storage->foo = 'bar';
$this->storage->bar = 'baz';
$this->storage->clear();
$data = $this->storage->toArray();
$this->assertSame(array(), $data);
}
public function testClearWithNoArgumentsRemovesExistingMetadata()
{
$this->storage->foo = 'bar';
$this->storage->lock('foo');
$this->storage->setMetadata('FOO', 'bar');
$this->storage->clear();
$this->assertFalse($this->storage->isLocked('foo'));
$this->assertFalse($this->storage->getMetadata('FOO'));
}
public function testClearWithArgumentRemovesExistingDataForThatKeyOnly()
{
$this->storage->foo = 'bar';
$this->storage->bar = 'baz';
$this->storage->clear('foo');
$data = $this->storage->toArray();
$this->assertSame(array('bar' => 'baz'), $data);
}
public function testClearWithArgumentRemovesExistingMetadataForThatKeyOnly()
{
$this->storage->foo = 'bar';
$this->storage->bar = 'baz';
$this->storage->lock('foo');
$this->storage->lock('bar');
$this->storage->setMetadata('foo', 'bar');
$this->storage->setMetadata('bar', 'baz');
$this->storage->clear('foo');
$this->assertFalse($this->storage->isLocked('foo'));
$this->assertTrue($this->storage->isLocked('bar'));
$this->assertFalse($this->storage->getMetadata('foo'));
$this->assertEquals('baz', $this->storage->getMetadata('bar'));
}
public function testClearWhenStorageMarkedImmutableRaisesException()
{
$this->storage->foo = 'bar';
$this->storage->bar = 'baz';
$this->storage->markImmutable();
$this->setExpectedException('Zend\Session\Exception\RuntimeException',
'Cannot clear storage as it is marked immutable');
$this->storage->clear();
}
public function testRequestAccessTimeIsPreservedEvenInFactoryMethod()
{
$this->assertNotEmpty($this->storage->getRequestAccessTime());
$this->storage->fromArray(array());
$this->assertNotEmpty($this->storage->getRequestAccessTime());
}
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));
}
}