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

FileDependencyTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
c 2
b 0
f 0
dl 0
loc 44
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createDependency() 0 3 1
A getFilePath() 0 3 1
A testDependencyIsChangedReusable() 0 12 1
A testTouchingFileMarksDependencyAsChanged() 0 7 1
A touchFile() 0 4 1
A createDirectory() 0 3 3
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