Completed
Pull Request — master (#82)
by Dominik
51:33 queued 16:28
created

SessionPersistenceTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 56
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testInitializeSessionFromRequestWithMissingPsr7SessionAttribute() 0 20 1
A testInitializeSessionFromRequestWithPsr7SessionAttribute() 0 17 1
A testPersistSession() 0 14 1
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