Completed
Push — master ( df428d...59132f )
by Divine Niiquaye
02:39
created

CacheItemTest::testExpiresAtException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of BiuradPHP opensource projects.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Biurad\Cache\Tests;
19
20
use Biurad\Cache\CacheItem;
21
use Biurad\Cache\Exceptions\InvalidArgumentException;
22
use DateInterval;
23
use DateTime;
24
use Exception;
25
use PHPUnit\Framework\TestCase;
26
use ReflectionProperty;
27
28
class CacheItemTest extends TestCase
29
{
30
    public function testValidKey(): void
31
    {
32
        self::assertSame('foo', CacheItem::validateKey('foo'));
33
    }
34
35
    public function testGetKey(): void
36
    {
37
        $item = new CacheItem();
38
39
        $r = new ReflectionProperty($item, 'key');
40
        $r->setAccessible(true);
41
        $r->setValue($item, 'test_key');
42
43
        self::assertEquals('test_key', $item->getKey());
44
    }
45
46
    public function testSet(): void
47
    {
48
        $item = new CacheItem();
49
        self::assertEquals(null, $item->get());
50
51
        $item->set('data');
52
        self::assertEquals('data', $item->get());
53
    }
54
55
    public function testGet(): void
56
    {
57
        $item = new CacheItem();
58
        self::assertNull($item->get());
59
60
        $item->set('data');
61
        self::assertEquals('data', $item->get());
62
    }
63
64
    public function testHit(): void
65
    {
66
        $item = new CacheItem();
67
        self::assertFalse($item->isHit());
68
69
        $r    = new ReflectionProperty($item, 'isHit');
70
        $r->setAccessible(true);
71
        $r->setValue($item, true);
72
73
        self::assertTrue($item->isHit());
74
    }
75
76
    public function testGetExpirationTimestamp(): void
77
    {
78
        $item = new CacheItem();
79
80
        $r = new ReflectionProperty($item, 'expiry');
81
        $r->setAccessible(true);
82
83
        self::assertNull($r->getValue($item));
84
85
        $timestamp = \time();
86
87
        $r->setValue($item, $timestamp);
88
        self::assertEquals($timestamp, $r->getValue($item));
89
    }
90
91
    public function testExpiresAt(): void
92
    {
93
        $item = new CacheItem();
94
95
        $r = new ReflectionProperty($item, 'expiry');
96
        $r->setAccessible(true);
97
98
        $item->expiresAt(new DateTime('30 seconds'));
99
        self::assertEquals(30, (int) (0.1 + $r->getValue($item) - (float) \microtime(true)));
100
101
        $item->expiresAt(null);
102
        self::assertNull($r->getValue($item));
103
    }
104
105
    public function testExpiresAtException(): void
106
    {
107
        $item = new CacheItem();
108
109
        $this->expectException(InvalidArgumentException::class);
110
        $this->expectExceptionMessage('Expiration date must implement DateTimeInterface or be null.');
111
112
        $item->expiresAt('string');
113
    }
114
115
    public function testExpiresAfter(): void
116
    {
117
        $item      = new CacheItem();
118
        $timestamp = \time() + 1;
119
120
        $r = new ReflectionProperty($item, 'expiry');
121
        $r->setAccessible(true);
122
123
        $item->expiresAfter($timestamp);
124
        self::assertEquals($timestamp, (int) (0.1 + $r->getValue($item) - (float) \microtime(true)));
125
126
        $item->expiresAfter(new DateInterval('PT1S'));
127
        self::assertEquals(1, (int) (0.1 + $r->getValue($item) - (float) \microtime(true)));
128
129
        $item->expiresAfter(null);
130
        self::assertNull($r->getValue($item));
131
    }
132
133
    /**
134
     * @dataProvider provideInvalidKey
135
     *
136
     * @param mixed $key
137
     */
138
    public function testInvalidKey($key): void
139
    {
140
        $this->expectException(InvalidArgumentException::class);
141
        $this->expectExceptionMessage('Cache key');
142
        CacheItem::validateKey($key);
143
    }
144
145
    /**
146
     * @return string[]
147
     */
148
    public function provideInvalidKey(): array
149
    {
150
        return [
151
            [''],
152
            ['{'],
153
            ['}'],
154
            ['('],
155
            [')'],
156
            ['/'],
157
            ['\\'],
158
            ['@'],
159
            [':'],
160
            [true],
161
            [null],
162
            [1],
163
            [1.1],
164
            [[[]]],
165
            [new Exception('foo')],
166
        ];
167
    }
168
}
169