1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Biurad 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 PHPUnit\Framework\TestCase; |
23
|
|
|
|
24
|
|
|
class CacheItemTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
public function testGetKey(): void |
27
|
|
|
{ |
28
|
|
|
$item = new CacheItem(); |
29
|
|
|
|
30
|
|
|
$r = new \ReflectionProperty($item, 'key'); |
31
|
|
|
$r->setAccessible(true); |
32
|
|
|
$r->setValue($item, 'test_key'); |
33
|
|
|
|
34
|
|
|
self::assertEquals('test_key', $item->getKey()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testSet(): void |
38
|
|
|
{ |
39
|
|
|
$item = new CacheItem(); |
40
|
|
|
self::assertNull($item->get()); |
41
|
|
|
|
42
|
|
|
$item->set('data'); |
43
|
|
|
self::assertEquals('data', $item->get()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testGet(): void |
47
|
|
|
{ |
48
|
|
|
$item = new CacheItem(); |
49
|
|
|
self::assertNull($item->get()); |
50
|
|
|
|
51
|
|
|
$item->set('data'); |
52
|
|
|
self::assertEquals('data', $item->get()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testHit(): void |
56
|
|
|
{ |
57
|
|
|
$item = new CacheItem(); |
58
|
|
|
self::assertFalse($item->isHit()); |
59
|
|
|
|
60
|
|
|
$r = new \ReflectionProperty($item, 'isHit'); |
61
|
|
|
$r->setAccessible(true); |
62
|
|
|
$r->setValue($item, true); |
63
|
|
|
|
64
|
|
|
self::assertTrue($item->isHit()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testGetExpirationTimestamp(): void |
68
|
|
|
{ |
69
|
|
|
$item = new CacheItem(); |
70
|
|
|
|
71
|
|
|
$r = new \ReflectionProperty($item, 'expiry'); |
72
|
|
|
$r->setAccessible(true); |
73
|
|
|
|
74
|
|
|
self::assertNull($r->getValue($item)); |
75
|
|
|
|
76
|
|
|
$timestamp = \time(); |
77
|
|
|
|
78
|
|
|
$r->setValue($item, $timestamp); |
79
|
|
|
self::assertEquals($timestamp, $r->getValue($item)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testExpiresAt(): void |
83
|
|
|
{ |
84
|
|
|
$item = new CacheItem(); |
85
|
|
|
|
86
|
|
|
$r = new \ReflectionProperty($item, 'expiry'); |
87
|
|
|
$r->setAccessible(true); |
88
|
|
|
|
89
|
|
|
$item->expiresAt(new \DateTime('30 seconds')); |
90
|
|
|
self::assertEquals(30, (int) (0.1 + $r->getValue($item) - (float) \microtime(true))); |
91
|
|
|
|
92
|
|
|
$item->expiresAt(null); |
93
|
|
|
self::assertNull($r->getValue($item)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testExpiresAtException(): void |
97
|
|
|
{ |
98
|
|
|
$item = new CacheItem(); |
99
|
|
|
|
100
|
|
|
$this->expectException(InvalidArgumentException::class); |
101
|
|
|
$this->expectExceptionMessage('Expiration date must implement DateTimeInterface or be null.'); |
102
|
|
|
|
103
|
|
|
$item->expiresAt('string'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testExpiresAfter(): void |
107
|
|
|
{ |
108
|
|
|
$item = new CacheItem(); |
109
|
|
|
$timestamp = \time() + 1; |
110
|
|
|
|
111
|
|
|
$r = new \ReflectionProperty($item, 'expiry'); |
112
|
|
|
$r->setAccessible(true); |
113
|
|
|
|
114
|
|
|
$item->expiresAfter($timestamp); |
115
|
|
|
self::assertEquals($timestamp, (int) (0.1 + $r->getValue($item) - (float) \microtime(true))); |
116
|
|
|
|
117
|
|
|
$item->expiresAfter(new \DateInterval('PT1S')); |
118
|
|
|
self::assertEquals(1, (int) (0.1 + $r->getValue($item) - (float) \microtime(true))); |
119
|
|
|
|
120
|
|
|
$item->expiresAfter(null); |
121
|
|
|
self::assertNull($r->getValue($item)); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|