Passed
Pull Request — master (#82)
by Dominik
34:41
created

SessionAdapterTest::testHasChanged()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 Lcobucci\Clock\FrozenClock;
24
use PHPUnit\Framework\MockObject\MockObject;
25
use PHPUnit\Framework\TestCase;
26
use PSR7Sessions\Storageless\Session\SessionInterface;
27
use PSR7Sessions\Storageless\Session\Zend\SessionAdapter;
28
29
/**
30
 * @covers \PSR7Sessions\Storageless\Session\Zend\SessionAdapter
31
 */
32
final class SessionAdapterTest extends TestCase
33
{
34
    public function testToArray() : void
35
    {
36
        $object      = new \stdClass();
37
        $object->key = 'value';
38
39
        /** @var SessionInterface|MockObject $session */
40
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
41
        $session->expects(self::once())->method('jsonSerialize')->with()->willReturn($object);
42
43
        $sessionAdapter = new SessionAdapter($session);
44
45
        self::assertSame(['key' => 'value'], $sessionAdapter->toArray());
46
    }
47
48
    public function testGet() : void
49
    {
50
        /** @var SessionInterface|MockObject $session */
51
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
52
        $session->expects(self::once())->method('get')->with('key', null)->willReturn('value');
53
54
        $sessionAdapter = new SessionAdapter($session);
55
56
        self::assertSame('value', $sessionAdapter->get('key'));
57
    }
58
59
    public function testHas() : void
60
    {
61
        /** @var SessionInterface|MockObject $session */
62
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
63
        $session->expects(self::once())->method('has')->with('key')->willReturn(true);
64
65
        $sessionAdapter = new SessionAdapter($session);
66
67
        self::assertTrue($sessionAdapter->has('key'));
68
    }
69
70
    public function testSet() : void
71
    {
72
        /** @var SessionInterface|MockObject $session */
73
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
74
        $session->expects(self::once())->method('set')->with('key', 'value');
75
76
        $sessionAdapter = new SessionAdapter($session);
77
        $sessionAdapter->set('key', 'value');
78
    }
79
80
    public function testUnset() : void
81
    {
82
        /** @var SessionInterface|MockObject $session */
83
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
84
        $session->expects(self::once())->method('remove')->with('key');
85
86
        $sessionAdapter = new SessionAdapter($session);
87
        $sessionAdapter->unset('key');
88
    }
89
90
    public function testClear() : void
91
    {
92
        /** @var SessionInterface|MockObject $session */
93
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
94
        $session->expects(self::once())->method('clear')->with();
95
96
        $sessionAdapter = new SessionAdapter($session);
97
        $sessionAdapter->clear();
98
    }
99
100
    public function testHasChanged() : void
101
    {
102
        /** @var SessionInterface|MockObject $session */
103
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
104
        $session->expects(self::once())->method('hasChanged')->with()->willReturn(true);
105
106
        $sessionAdapter = new SessionAdapter($session);
107
108
        self::assertTrue($sessionAdapter->hasChanged());
109
    }
110
111
    public function testRegenerate() : void
112
    {
113
        $clock = new FrozenClock(new \DateTimeImmutable());
114
115
        /** @var SessionInterface|MockObject $session */
116
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
117
        $session->expects(self::once())->method('set')->with('_regenerated', $clock->now()->getTimestamp());
118
119
        $sessionAdapter = new SessionAdapter($session, $clock);
120
        $sessionAdapter->regenerate();
121
    }
122
123
    public function testIsRegenerated() : void
124
    {
125
        /** @var SessionInterface|MockObject $session */
126
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
127
        $session->expects(self::once())->method('has')->with('_regenerated')->willReturn(true);
128
129
        $sessionAdapter = new SessionAdapter($session);
130
131
        self::assertTrue($sessionAdapter->isRegenerated());
132
    }
133
}
134