-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheItemTest.php
123 lines (92 loc) · 3.19 KB
/
CacheItemTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
/*
* This file is part of Biurad opensource projects.
*
* PHP version 7.1 and above required
*
* @author Divine Niiquaye Ibok <[email protected]>
* @copyright 2019 Biurad Group (https://biurad.com/)
* @license https://opensource.org/licenses/BSD-3-Clause License
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Biurad\Cache\Tests;
use Biurad\Cache\CacheItem;
use Biurad\Cache\Exceptions\InvalidArgumentException;
use PHPUnit\Framework\TestCase;
class CacheItemTest extends TestCase
{
public function testGetKey(): void
{
$item = new CacheItem();
$r = new \ReflectionProperty($item, 'key');
$r->setAccessible(true);
$r->setValue($item, 'test_key');
self::assertEquals('test_key', $item->getKey());
}
public function testSet(): void
{
$item = new CacheItem();
self::assertNull($item->get());
$item->set('data');
self::assertEquals('data', $item->get());
}
public function testGet(): void
{
$item = new CacheItem();
self::assertNull($item->get());
$item->set('data');
self::assertEquals('data', $item->get());
}
public function testHit(): void
{
$item = new CacheItem();
self::assertFalse($item->isHit());
$r = new \ReflectionProperty($item, 'isHit');
$r->setAccessible(true);
$r->setValue($item, true);
self::assertTrue($item->isHit());
}
public function testGetExpirationTimestamp(): void
{
$item = new CacheItem();
$r = new \ReflectionProperty($item, 'expiry');
$r->setAccessible(true);
self::assertNull($r->getValue($item));
$timestamp = \time();
$r->setValue($item, $timestamp);
self::assertEquals($timestamp, $r->getValue($item));
}
public function testExpiresAt(): void
{
$item = new CacheItem();
$r = new \ReflectionProperty($item, 'expiry');
$r->setAccessible(true);
$item->expiresAt(new \DateTime('30 seconds'));
self::assertEquals(30, (int) (0.1 + $r->getValue($item) - (float) \microtime(true)));
$item->expiresAt(null);
self::assertNull($r->getValue($item));
}
public function testExpiresAtException(): void
{
$item = new CacheItem();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Expiration date must implement DateTimeInterface or be null.');
$item->expiresAt('string');
}
public function testExpiresAfter(): void
{
$item = new CacheItem();
$timestamp = \time() + 1;
$r = new \ReflectionProperty($item, 'expiry');
$r->setAccessible(true);
$item->expiresAfter($timestamp);
self::assertEquals($timestamp, (int) (0.1 + $r->getValue($item) - (float) \microtime(true)));
$item->expiresAfter(new \DateInterval('PT1S'));
self::assertEquals(1, (int) (0.1 + $r->getValue($item) - (float) \microtime(true)));
$item->expiresAfter(null);
self::assertNull($r->getValue($item));
}
}