Completed
Push — master ( 0629e4...73c381 )
by David
17s queued 10s
created

ClassBoundCacheContractTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 18
dl 0
loc 33
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A testClassBoundCacheContract() 0 31 1
1
<?php
2
3
namespace TheCodingMachine\CacheUtils;
4
5
use function clearstatcache;
6
use PHPUnit\Framework\TestCase;
7
use ReflectionClass;
8
use function sleep;
9
use Symfony\Component\Cache\Simple\ArrayCache;
10
use TheCodingMachine\CacheUtils\Fixtures\A;
11
use function touch;
12
13
class ClassBoundCacheContractTest extends TestCase
14
{
15
    public function testClassBoundCacheContract()
16
    {
17
        $cache = new ArrayCache();
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Cache\Simple\ArrayCache has been deprecated: since Symfony 4.3, use ArrayAdapter and type-hint for CacheInterface instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

17
        $cache = /** @scrutinizer ignore-deprecated */ new ArrayCache();
Loading history...
18
        $fileBoundCache = new FileBoundCache($cache, 'prefix');
19
        $classBoundCache = new ClassBoundCache($fileBoundCache, true, true, true);
20
        $classBoundCacheContract = new ClassBoundCacheContract($classBoundCache);
21
22
        $val = 0;
23
24
        $newVal = $classBoundCacheContract->get(new ReflectionClass(A::class), function() use (&$val) {
25
            return ++$val;
26
        });
27
28
        $this->assertSame(1, $newVal);
29
30
        $newVal2 = $classBoundCacheContract->get(new ReflectionClass(A::class), function() use (&$val) {
31
            return ++$val;
32
        });
33
34
        $this->assertSame(1, $newVal2);
35
36
        $classToTouch = new ReflectionClass(A::class);
37
        sleep(1);
38
        clearstatcache($classToTouch->getFileName());
0 ignored issues
show
Bug introduced by
$classToTouch->getFileName() of type string is incompatible with the type boolean expected by parameter $clear_realpath_cache of clearstatcache(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        clearstatcache(/** @scrutinizer ignore-type */ $classToTouch->getFileName());
Loading history...
39
        touch($classToTouch->getFileName());
40
41
        $newVal3 = $classBoundCacheContract->get(new ReflectionClass(A::class), function() use (&$val) {
42
            return ++$val;
43
        });
44
45
        $this->assertSame(2, $newVal3);
46
    }
47
48
}
49