1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Cache\Tests\Dependency; |
4
|
|
|
|
5
|
|
|
use Yiisoft\Cache\ArrayCache; |
6
|
|
|
use Yiisoft\Cache\Cache; |
7
|
|
|
use Yiisoft\Cache\Dependency\FileDependency; |
8
|
|
|
|
9
|
|
|
class FileDependencyTest extends DependencyTestCase |
10
|
|
|
{ |
11
|
|
|
private function getFilePath(): string |
12
|
|
|
{ |
13
|
|
|
return dirname(__DIR__) . '/runtime/file.txt'; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
private function createDependency(): FileDependency |
17
|
|
|
{ |
18
|
|
|
return new FileDependency($this->getFilePath()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
private function touchFile(): void |
22
|
|
|
{ |
23
|
|
|
$this->createDirectory(dirname($this->getFilePath()), 0775); |
24
|
|
|
touch($this->getFilePath()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testTouchingFileMarksDependencyAsChanged(): void |
28
|
|
|
{ |
29
|
|
|
$this->touchFile(); |
30
|
|
|
$dependency = $this->createDependency(); |
31
|
|
|
$this->touchFile(); |
32
|
|
|
|
33
|
|
|
$this->assertDependencyChanged($dependency); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testDependencyIsChangedReusable() |
37
|
|
|
{ |
38
|
|
|
$cache = new Cache(new ArrayCache()); |
39
|
|
|
$this->touchFile(); |
40
|
|
|
$dependency = $this->createDependency(); |
41
|
|
|
$dependency->markAsReusable(); |
42
|
|
|
$cache->set('a', 1, null, $dependency); |
43
|
|
|
$cache->set('b', 2, null, $dependency); |
44
|
|
|
$this->assertSame(1, $cache->get('a')); |
45
|
|
|
sleep(1); |
46
|
|
|
$this->touchFile(); |
47
|
|
|
$this->assertSame(2, $cache->get('b')); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function createDirectory(string $path, int $mode): bool |
51
|
|
|
{ |
52
|
|
|
return is_dir($path) || (mkdir($path, $mode, true) && is_dir($path)); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|