CacheFactoryTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 22
c 2
b 0
f 0
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A expectedCacheAdapterIsReturned() 0 19 2
A setUp() 0 3 1
A provideCacheConfig() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Common\Cache;
6
7
use Doctrine\Common\Cache;
8
use PHPUnit\Framework\TestCase;
9
use Predis\ClientInterface;
10
use Prophecy\PhpUnit\ProphecyTrait;
11
use Prophecy\Prophecy\ObjectProphecy;
12
use Psr\Container\ContainerInterface;
13
use Shlinkio\Shlink\Common\Cache\CacheFactory;
14
use Shlinkio\Shlink\Common\Cache\RedisFactory;
15
16
use function Functional\const_function;
17
18
class CacheFactoryTest extends TestCase
19
{
20
    use ProphecyTrait;
21
22
    private ObjectProphecy $container;
23
24
    public function setUp(): void
25
    {
26
        $this->container = $this->prophesize(ContainerInterface::class);
27
    }
28
29
    /**
30
     * @test
31
     * @dataProvider provideCacheConfig
32
     */
33
    public function expectedCacheAdapterIsReturned(
34
        array $config,
35
        string $expectedAdapterClass,
36
        string $expectedNamespace,
37
        callable $apcuEnabled
38
    ): void {
39
        $factory = new CacheFactory($apcuEnabled);
40
41
        $getConfig = $this->container->get('config')->willReturn($config);
42
        $getRedis = $this->container->get(RedisFactory::SERVICE_NAME)->willReturn(
43
            $this->prophesize(ClientInterface::class)->reveal(),
44
        );
45
46
        $cache = $factory($this->container->reveal());
47
48
        $this->assertInstanceOf($expectedAdapterClass, $cache);
49
        $this->assertEquals($expectedNamespace, $cache->getNamespace());
50
        $getConfig->shouldHaveBeenCalledOnce();
51
        $getRedis->shouldHaveBeenCalledTimes($expectedAdapterClass === Cache\PredisCache::class ? 1 : 0);
52
    }
53
54
    public function provideCacheConfig(): iterable
55
    {
56
        $withApcu = const_function(true);
57
        $withoutApcu = const_function(false);
58
59
        yield 'debug true' => [['debug' => true], Cache\ArrayCache::class, '', $withApcu];
60
        yield 'debug false' => [['debug' => false], Cache\ApcuCache::class, '', $withApcu];
61
        yield 'no debug' => [[], Cache\ApcuCache::class, '', $withApcu];
62
        yield 'with redis' => [['cache' => [
63
            'namespace' => $namespace = 'some_namespace',
64
            'redis' => [],
65
        ]], Cache\PredisCache::class, $namespace, $withApcu];
66
        yield 'debug false and no apcu' => [['debug' => false], Cache\ArrayCache::class, '', $withoutApcu];
67
    }
68
}
69