Passed
Push — master ( f0915f...7925ec )
by Alexander
01:20
created

testDependencyIsChangedReusable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 9.9332
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