Passed
Pull Request — master (#30)
by Alexander
02:06
created

FileCacheTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 62
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testExpireAdd() 0 11 1
A testExpire() 0 11 1
A testCacheRenewalOnDifferentOwnership() 0 22 2
A createCacheInstance() 0 3 1
1
<?php
2
namespace Yiisoft\CacheOld\Tests;
3
4
use Psr\Log\NullLogger;
5
use Yiisoft\CacheOld\Cache;
6
use Yiisoft\CacheOld\CacheInterface;
7
use Yiisoft\CacheOld\FileCache;
8
use phpmock\phpunit\PHPMock;
9
10
/**
11
 * Class for testing file cache backend.
12
 * @group caching
13
 */
14
class FileCacheTest extends CacheTest
15
{
16
    use PHPMock;
17
18
    protected function createCacheInstance(): CacheInterface
19
    {
20
        return new Cache(new FileCache(__DIR__ . '/runtime/cache', new NullLogger()));
21
    }
22
23
    public function testExpire(): void
24
    {
25
        $cache = $this->createCacheInstance();
26
        $cache->clear();
27
28
        static::$time = \time();
29
        $this->assertTrue($cache->set('expire_test', 'expire_test', 2));
30
        static::$time++;
31
        $this->assertEquals('expire_test', $cache->get('expire_test'));
32
        static::$time++;
33
        $this->assertNull($cache->get('expire_test'));
34
    }
35
36
    public function testExpireAdd(): void
37
    {
38
        $cache = $this->createCacheInstance();
39
        $cache->clear();
40
41
        static::$time = \time();
42
        $this->assertTrue($cache->add('expire_testa', 'expire_testa', 2));
43
        static::$time++;
44
        $this->assertEquals('expire_testa', $cache->get('expire_testa'));
45
        static::$time++;
46
        $this->assertNull($cache->get('expire_testa'));
47
    }
48
49
    /**
50
     * We have to on separate process because of PHPMock not being able to mock a function that
51
     * was already called.
52
     * @runInSeparateProcess
53
     */
54
    public function testCacheRenewalOnDifferentOwnership(): void
55
    {
56
        if (!function_exists('posix_geteuid')) {
57
            $this->markTestSkipped('Can not test without posix extension installed.');
58
        }
59
60
        $cache = $this->createCacheInstance();
61
        $cache->clear();
62
63
        $cacheValue = uniqid('value_', false);
64
        $cacheKey = uniqid('key_', false);
65
66
        static::$time = \time();
67
        $this->assertTrue($cache->set($cacheKey, $cacheValue, 2));
68
        $this->assertSame($cacheValue, $cache->get($cacheKey));
69
70
        // Override fileowner method so it always returns something not equal to the current user
71
        $notCurrentEuid = posix_geteuid() + 15;
72
        $this->getFunctionMock('Yiisoft\CacheOld', 'fileowner')->expects($this->any())->willReturn($notCurrentEuid);
73
        $this->getFunctionMock('Yiisoft\CacheOld', 'unlink')->expects($this->once());
74
75
        $this->assertTrue($cache->set($cacheKey, uniqid('value_2_', false), 2), 'Cannot rebuild cache on different file ownership');
76
    }
77
}
78