|
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\Zend; |
|
22
|
|
|
|
|
23
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
24
|
|
|
use PHPUnit\Framework\TestCase; |
|
25
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
26
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
27
|
|
|
use PSR7Sessions\Storageless\Http\SessionMiddleware; |
|
28
|
|
|
use PSR7Sessions\Storageless\Session\SessionInterface; |
|
29
|
|
|
use PSR7Sessions\Storageless\Session\Zend\SessionPersistence; |
|
30
|
|
|
use UnexpectedValueException; |
|
31
|
|
|
use Zend\Expressive\Session\SessionInterface as ZendSessionInterface; |
|
32
|
|
|
use function sprintf; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @covers \PSR7Sessions\Storageless\Session\Zend\SessionPersistence |
|
36
|
|
|
*/ |
|
37
|
|
|
final class SessionPersistenceTest extends TestCase |
|
38
|
|
|
{ |
|
39
|
|
|
public function testInitializeSessionFromRequestWithMissingPsr7SessionAttribute() : void |
|
40
|
|
|
{ |
|
41
|
|
|
$this->expectException(UnexpectedValueException::class); |
|
42
|
|
|
$this->expectExceptionMessage(sprintf( |
|
43
|
|
|
'Please add this following middleware "%s" before execute this method "%s::initializeSessionFromRequest"', |
|
44
|
|
|
SessionMiddleware::class, |
|
45
|
|
|
SessionPersistence::class |
|
46
|
|
|
)); |
|
47
|
|
|
|
|
48
|
|
|
/** @var ServerRequestInterface|MockObject $request */ |
|
49
|
|
|
$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); |
|
50
|
|
|
$request |
|
51
|
|
|
->expects(self::once()) |
|
52
|
|
|
->method('getAttribute') |
|
53
|
|
|
->with(SessionMiddleware::SESSION_ATTRIBUTE, null) |
|
54
|
|
|
->willReturn(null); |
|
55
|
|
|
|
|
56
|
|
|
$persistence = new SessionPersistence(); |
|
57
|
|
|
$persistence->initializeSessionFromRequest($request); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testInitializeSessionFromRequestWithPsr7SessionAttribute() : void |
|
61
|
|
|
{ |
|
62
|
|
|
/** @var SessionInterface|MockObject $session */ |
|
63
|
|
|
$session = $this->getMockBuilder(SessionInterface::class)->getMock(); |
|
64
|
|
|
$session->expects(self::never())->method(self::anything()); |
|
65
|
|
|
|
|
66
|
|
|
/** @var ServerRequestInterface|MockObject $request */ |
|
67
|
|
|
$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); |
|
68
|
|
|
$request |
|
69
|
|
|
->expects(self::once()) |
|
70
|
|
|
->method('getAttribute') |
|
71
|
|
|
->with(SessionMiddleware::SESSION_ATTRIBUTE, null) |
|
72
|
|
|
->willReturn($session); |
|
73
|
|
|
|
|
74
|
|
|
$persistence = new SessionPersistence(); |
|
75
|
|
|
$persistence->initializeSessionFromRequest($request); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testPersistSession() : void |
|
79
|
|
|
{ |
|
80
|
|
|
/** @var ZendSessionInterface|MockObject $zendSession */ |
|
81
|
|
|
$zendSession = $this->getMockBuilder(ZendSessionInterface::class)->getMock(); |
|
82
|
|
|
$zendSession->expects(self::never())->method(self::anything()); |
|
83
|
|
|
|
|
84
|
|
|
/** @var ResponseInterface|MockObject $response */ |
|
85
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)->getMock(); |
|
86
|
|
|
$response->expects(self::never())->method(self::anything()); |
|
87
|
|
|
|
|
88
|
|
|
$persistence = new SessionPersistence(); |
|
89
|
|
|
|
|
90
|
|
|
self::assertSame($response, $persistence->persistSession($zendSession, $response)); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|