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 Zend\Expressive\Session\SessionInterface as ZendSessionInterface; |
31
|
|
|
use function sprintf; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @covers \PSR7Sessions\Storageless\Session\Zend\SessionPersistence |
35
|
|
|
*/ |
36
|
|
|
final class SessionPersistenceTest extends TestCase |
37
|
|
|
{ |
38
|
|
|
public function testInitializeSessionFromRequestWithMissingPsr7SessionAttribute() : void |
39
|
|
|
{ |
40
|
|
|
$this->expectException(\UnexpectedValueException::class); |
41
|
|
|
$this->expectExceptionMessage(sprintf( |
42
|
|
|
'Please add the following middleware "%s" before execute this method "%s::initializeSessionFromRequest"', |
43
|
|
|
SessionMiddleware::class, |
44
|
|
|
SessionPersistence::class |
45
|
|
|
)); |
46
|
|
|
|
47
|
|
|
/** @var ServerRequestInterface|MockObject $request */ |
48
|
|
|
$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); |
49
|
|
|
$request |
|
|
|
|
50
|
|
|
->expects(self::once()) |
51
|
|
|
->method('getAttribute') |
52
|
|
|
->with(SessionMiddleware::SESSION_ATTRIBUTE, null) |
53
|
|
|
->willReturn(null); |
54
|
|
|
|
55
|
|
|
$persistence = new SessionPersistence(); |
56
|
|
|
$persistence->initializeSessionFromRequest($request); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testInitializeSessionFromRequestWithPsr7SessionAttribute() : void |
60
|
|
|
{ |
61
|
|
|
/** @var SessionInterface|MockObject $session */ |
62
|
|
|
$session = $this->getMockBuilder(SessionInterface::class)->getMock(); |
63
|
|
|
$session->expects(self::never())->method(self::anything()); |
|
|
|
|
64
|
|
|
|
65
|
|
|
/** @var ServerRequestInterface|MockObject $request */ |
66
|
|
|
$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); |
67
|
|
|
$request |
|
|
|
|
68
|
|
|
->expects(self::once()) |
69
|
|
|
->method('getAttribute') |
70
|
|
|
->with(SessionMiddleware::SESSION_ATTRIBUTE, null) |
71
|
|
|
->willReturn($session); |
72
|
|
|
|
73
|
|
|
$persistence = new SessionPersistence(); |
74
|
|
|
$persistence->initializeSessionFromRequest($request); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testPersistSession() : void |
78
|
|
|
{ |
79
|
|
|
/** @var ZendSessionInterface|MockObject $zendSession */ |
80
|
|
|
$zendSession = $this->getMockBuilder(ZendSessionInterface::class)->getMock(); |
81
|
|
|
$zendSession->expects(self::never())->method(self::anything()); |
|
|
|
|
82
|
|
|
|
83
|
|
|
/** @var ResponseInterface|MockObject $response */ |
84
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)->getMock(); |
85
|
|
|
$response->expects(self::never())->method(self::anything()); |
|
|
|
|
86
|
|
|
|
87
|
|
|
$persistence = new SessionPersistence(); |
88
|
|
|
|
89
|
|
|
self::assertSame($response, $persistence->persistSession($zendSession, $response)); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: