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 DateTime; |
23
|
|
|
use Exception; |
24
|
|
|
use PHPUnit\Framework\TestCase; |
25
|
|
|
use ReflectionProperty; |
26
|
|
|
|
27
|
|
|
class CacheItemTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
public function testValidKey(): void |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
self::assertSame('foo', CacheItem::validateKey('foo')); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @dataProvider provideInvalidKey |
37
|
|
|
* @param mixed $key |
38
|
|
|
*/ |
39
|
|
|
public function testInvalidKey($key): void |
40
|
|
|
{ |
41
|
|
|
$this->expectException(InvalidArgumentException::class); |
42
|
|
|
$this->expectExceptionMessage('Cache key'); |
43
|
|
|
CacheItem::validateKey($key); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function provideInvalidKey(): array |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
[''], |
50
|
|
|
['{'], |
51
|
|
|
['}'], |
52
|
|
|
['('], |
53
|
|
|
[')'], |
54
|
|
|
['/'], |
55
|
|
|
['\\'], |
56
|
|
|
['@'], |
57
|
|
|
[':'], |
58
|
|
|
[true], |
59
|
|
|
[null], |
60
|
|
|
[1], |
61
|
|
|
[1.1], |
62
|
|
|
[[[]]], |
63
|
|
|
[new Exception('foo')], |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @throws \ReflectionException |
69
|
|
|
*/ |
70
|
|
|
public function testItem(): void |
71
|
|
|
{ |
72
|
|
|
$item = new CacheItem(); |
73
|
|
|
$r = new ReflectionProperty($item, 'key'); |
74
|
|
|
$r->setAccessible(true); |
75
|
|
|
$r->setValue($item, 'foo'); |
76
|
|
|
|
77
|
|
|
$r = new ReflectionProperty($item, 'defaultLifetime'); |
78
|
|
|
$r->setAccessible(true); |
79
|
|
|
$r->setValue($item, 1); |
80
|
|
|
|
81
|
|
|
$item->set('data'); |
82
|
|
|
|
83
|
|
|
self::assertEquals('foo', $item->getKey()); |
84
|
|
|
self::assertEquals('data', $item->get()); |
85
|
|
|
|
86
|
|
|
$item->expiresAt(new DateTime('30 seconds')); |
87
|
|
|
$r = new ReflectionProperty(CacheItem::class, 'expiry'); |
88
|
|
|
$r->setAccessible(true); |
89
|
|
|
self::assertIsFloat($r->getValue($item)); |
90
|
|
|
self::assertEquals(30, (int) (0.1 + $r->getValue($item) - microtime(true))); |
91
|
|
|
|
92
|
|
|
$item->expiresAfter(null); |
93
|
|
|
$r = new ReflectionProperty($item, 'expiry'); |
94
|
|
|
$r->setAccessible(true); |
95
|
|
|
self::assertIsFloat($r->getValue($item)); |
96
|
|
|
|
97
|
|
|
$this->expectException(InvalidArgumentException::class); |
98
|
|
|
$this->expectExceptionMessage('Expiration date must implement DateTimeInterface or be null.'); |
99
|
|
|
$item->expiresAt('string'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|