1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Koded\Caching\Client; |
4
|
|
|
|
5
|
|
|
use Koded\Caching\CacheException; |
6
|
|
|
use Koded\Stdlib\Arguments; |
7
|
|
|
use org\bovigo\vfs\{vfsStream, vfsStreamDirectory, vfsStreamWrapper}; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Psr\Log\NullLogger; |
10
|
|
|
use function Koded\Stdlib\now; |
11
|
|
|
|
12
|
|
|
class FileClientTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var vfsStreamDirectory |
17
|
|
|
*/ |
18
|
|
|
private $dir; |
19
|
|
|
|
20
|
|
|
public function test_nonwritable_cache_directory() |
21
|
|
|
{ |
22
|
|
|
$dir = $this->dir->url() . '/fubar'; |
23
|
|
|
$this->expectException(CacheException::class); |
24
|
|
|
$this->expectExceptionMessage('Failed to create a cache directory "' . $dir . '/"'); |
25
|
|
|
|
26
|
|
|
vfsStreamWrapper::getRoot()->chmod(0400); |
27
|
|
|
new FileClient(new NullLogger, $dir); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function test_cache_content() |
31
|
|
|
{ |
32
|
|
|
$client = new FileClient(new NullLogger, $this->dir->url()); |
33
|
|
|
$client->set('foo', new Arguments(['foo' => 'bar'])); |
34
|
|
|
|
35
|
|
|
$raw = $this->dir->getChild('0/beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33.php')->getContent(); |
36
|
|
|
$this->assertContains(var_export([ |
37
|
|
|
'timestamp' => 32503593600, |
38
|
|
|
'key' => 'foo', |
39
|
|
|
'value' => 'O:22:"Koded\\Stdlib\\Arguments":1:{s:10:"' . "\0" . '*' . "\0" . 'storage";a:1:{s:3:"foo";s:3:"bar";}}', |
40
|
|
|
], true), $raw); |
41
|
|
|
|
42
|
|
|
$this->assertEquals(new Arguments(['foo' => 'bar']), $client->get('foo')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function test_global_ttl() |
46
|
|
|
{ |
47
|
|
|
$now = now()->getTimestamp(); |
48
|
|
|
$client = new FileClient(new NullLogger, $this->dir->url(), 2); |
49
|
|
|
$client->set('key', 'value'); |
50
|
|
|
|
51
|
|
|
$data = include $this->dir->getChild('a/62f2225bf70bfaccbc7f1ef2a397836717377de.php')->url(); |
52
|
|
|
$this->assertEquals($now + 2, $data['timestamp']); |
53
|
|
|
$this->assertEquals('value', $client->get('key')); |
54
|
|
|
|
55
|
|
|
sleep(3); |
56
|
|
|
$this->assertFalse($client->has('key')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function setUp(): void |
60
|
|
|
{ |
61
|
|
|
$this->dir = vfsStream::setup(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|