1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
declare(strict_types=1); |
20
|
|
|
|
21
|
|
|
namespace PSR7SessionsTest\Storageless\Session; |
22
|
|
|
|
23
|
|
|
use PHPUnit_Framework_TestCase; |
24
|
|
|
use PSR7Sessions\Storageless\Session\LazySession; |
25
|
|
|
use PSR7Sessions\Storageless\Session\SessionInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @covers \PSR7Sessions\Storageless\Session\LazySession |
29
|
|
|
*/ |
30
|
|
|
final class LazySessionTest extends PHPUnit_Framework_TestCase |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var SessionInterface|\PHPUnit_Framework_MockObject_MockObject |
34
|
|
|
*/ |
35
|
|
|
private $wrappedSession; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var callable|\PHPUnit_Framework_MockObject_MockObject |
39
|
|
|
*/ |
40
|
|
|
private $sessionLoader; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var LazySession |
44
|
|
|
*/ |
45
|
|
|
private $lazySession; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritDoc} |
49
|
|
|
*/ |
50
|
|
|
protected function setUp() |
51
|
|
|
{ |
52
|
|
|
$this->wrappedSession = $this->createMock(SessionInterface::class); |
53
|
|
|
$this->sessionLoader = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock(); |
54
|
|
|
$this->lazySession = LazySession::fromContainerBuildingCallback($this->sessionLoader); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testIsALazySession() |
58
|
|
|
{ |
59
|
|
|
self::assertInstanceOf(LazySession::class, $this->lazySession); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testLazyNonInitializedSessionIsAlwaysNotChanged() |
63
|
|
|
{ |
64
|
|
|
$this->sessionLoader->expects(self::never())->method('__invoke'); |
|
|
|
|
65
|
|
|
|
66
|
|
|
self::assertFalse($this->lazySession->hasChanged()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testHasChanged() |
70
|
|
|
{ |
71
|
|
|
$this->wrappedSessionWillBeLoaded(); |
72
|
|
|
|
73
|
|
|
$this->wrappedSession->expects(self::at(1))->method('hasChanged')->willReturn(true); |
|
|
|
|
74
|
|
|
$this->wrappedSession->expects(self::at(2))->method('hasChanged')->willReturn(false); |
75
|
|
|
|
76
|
|
|
$this->forceWrappedSessionInitialization(); |
77
|
|
|
|
78
|
|
|
self::assertTrue($this->lazySession->hasChanged()); |
79
|
|
|
self::assertFalse($this->lazySession->hasChanged()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testHas() |
83
|
|
|
{ |
84
|
|
|
$this->wrappedSessionWillBeLoaded(); |
85
|
|
|
|
86
|
|
|
$this->wrappedSession->expects(self::exactly(2))->method('has')->willReturnMap([ |
|
|
|
|
87
|
|
|
['foo', false], |
88
|
|
|
['bar', true], |
89
|
|
|
]); |
90
|
|
|
|
91
|
|
|
self::assertFalse($this->lazySession->has('foo')); |
92
|
|
|
self::assertTrue($this->lazySession->has('bar')); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testGet() |
96
|
|
|
{ |
97
|
|
|
$this->wrappedSessionWillBeLoaded(); |
98
|
|
|
|
99
|
|
|
$this->wrappedSession->expects(self::exactly(3))->method('get')->willReturnMap([ |
|
|
|
|
100
|
|
|
['foo', null, 'bar'], |
101
|
|
|
['baz', null, 'tab'], |
102
|
|
|
['baz', 'default', 'taz'], |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
self::assertSame('bar', $this->lazySession->get('foo')); |
106
|
|
|
self::assertSame('tab', $this->lazySession->get('baz')); |
107
|
|
|
self::assertSame('taz', $this->lazySession->get('baz', 'default')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testRemove() |
111
|
|
|
{ |
112
|
|
|
$this->wrappedSessionWillBeLoaded(); |
113
|
|
|
|
114
|
|
|
$this->wrappedSession->expects(self::exactly(2))->method('remove')->with(self::logicalOr('foo', 'bar')); |
|
|
|
|
115
|
|
|
|
116
|
|
|
$this->lazySession->remove('foo'); |
117
|
|
|
$this->lazySession->remove('bar'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testClear() |
121
|
|
|
{ |
122
|
|
|
$this->wrappedSessionWillBeLoaded(); |
123
|
|
|
|
124
|
|
|
$this->wrappedSession->expects(self::exactly(2))->method('clear'); |
|
|
|
|
125
|
|
|
|
126
|
|
|
$this->lazySession->clear(); |
127
|
|
|
$this->lazySession->clear(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testSet() |
131
|
|
|
{ |
132
|
|
|
$this->wrappedSessionWillBeLoaded(); |
133
|
|
|
|
134
|
|
|
$this |
|
|
|
|
135
|
|
|
->wrappedSession |
136
|
|
|
->expects(self::exactly(2)) |
137
|
|
|
->method('set') |
138
|
|
|
->with(self::logicalOr('foo', 'baz'), self::logicalOr('bar', 'tab')); |
139
|
|
|
|
140
|
|
|
$this->lazySession->set('foo', 'bar'); |
141
|
|
|
$this->lazySession->set('baz', 'tab'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testIsEmpty() |
145
|
|
|
{ |
146
|
|
|
$this->wrappedSessionWillBeLoaded(); |
147
|
|
|
|
148
|
|
|
$this->wrappedSession->expects(self::at(0))->method('isEmpty')->willReturn(true); |
|
|
|
|
149
|
|
|
$this->wrappedSession->expects(self::at(1))->method('isEmpty')->willReturn(false); |
150
|
|
|
|
151
|
|
|
self::assertTrue($this->lazySession->isEmpty()); |
152
|
|
|
self::assertFalse($this->lazySession->isEmpty()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testJsonSerialize() |
156
|
|
|
{ |
157
|
|
|
$this->wrappedSessionWillBeLoaded(); |
158
|
|
|
|
159
|
|
|
$this->wrappedSession->expects(self::at(0))->method('jsonSerialize')->willReturn('foo'); |
|
|
|
|
160
|
|
|
$this->wrappedSession->expects(self::at(1))->method('jsonSerialize')->willReturn('bar'); |
161
|
|
|
|
162
|
|
|
self::assertSame('foo', $this->lazySession->jsonSerialize()); |
163
|
|
|
self::assertSame('bar', $this->lazySession->jsonSerialize()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
private function wrappedSessionWillBeLoaded() |
167
|
|
|
{ |
168
|
|
|
$this->sessionLoader->expects(self::once())->method('__invoke')->willReturn($this->wrappedSession); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
private function forceWrappedSessionInitialization() |
172
|
|
|
{ |
173
|
|
|
// no-op operation that is known to trigger session lazy-loading |
174
|
|
|
$this->lazySession->remove(uniqid('nonExisting', true)); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.