CacheFactoryTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 20
dl 0
loc 68
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetGetValidCacheKey() 0 7 1
A getTempDir() 0 3 1
A tearDownAfterClass() 0 3 1
A setUpBeforeClass() 0 3 1
A testFilesystem() 0 7 1
A deleteTempDir() 0 8 2
A testGeneral() 0 5 1
A testOpcache() 0 9 3
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Tests\Cache;
12
13
use PHPUnit\Framework\TestCase;
14
use Symfony\Component\Filesystem\Filesystem;
15
use Symfony\Component\Cache\Adapter\AdapterInterface;
16
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
17
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
18
use Yarhon\RouteGuardBundle\Cache\CacheFactory;
19
20
/**
21
 * @author Yaroslav Honcharuk <[email protected]>
22
 */
23
class CacheFactoryTest extends TestCase
24
{
25
    public static function setUpBeforeClass()
26
    {
27
        static::deleteTempDir();
28
    }
29
30
    public static function tearDownAfterClass()
31
    {
32
        static::deleteTempDir();
33
    }
34
35
    public function testGeneral()
36
    {
37
        $cache = CacheFactory::createCache(self::getTempDir(), 'test');
38
39
        $this->assertInstanceOf(AdapterInterface::class, $cache);
40
    }
41
42
    /**
43
     * @requires function opcache_invalidate
44
     * @runInSeparateProcess
45
     */
46
    public function testOpcache()
47
    {
48
        if (!ini_get('opcache.enable') || !ini_get('opcache.enable_cli')) {
49
            $this->markTestSkipped();
50
        }
51
52
        $cache = CacheFactory::createCache(self::getTempDir(), 'test');
53
54
        $this->assertInstanceOf(PhpFilesAdapter::class, $cache);
55
    }
56
57
    public function testGetGetValidCacheKey()
58
    {
59
        $key = 'index{}()/\\@:route';
60
61
        $validKey = CacheFactory::getValidCacheKey($key);
62
63
        $this->assertEquals('index%7B%7D%28%29%2F%5C%40%3Aroute', $validKey);
64
    }
65
66
    /**
67
     * @runInSeparateProcess
68
     */
69
    public function testFilesystem()
70
    {
71
        ini_set('opcache.enable', 0);
72
73
        $cache = CacheFactory::createCache(self::getTempDir(), 'test');
74
75
        $this->assertInstanceOf(FilesystemAdapter::class, $cache);
76
    }
77
78
    protected static function getTempDir()
79
    {
80
        return sys_get_temp_dir().'/route-guard-'.substr(strrchr(static::class, '\\'), 1);
81
    }
82
83
    protected static function deleteTempDir()
84
    {
85
        if (!file_exists($dir = static::getTempDir())) {
86
            return;
87
        }
88
89
        $fs = new Filesystem();
90
        $fs->remove($dir);
91
    }
92
}
93