RedisFactoryTest::provideRedisConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 18
rs 9.7333
c 2
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Common\Cache;
6
7
use PHPUnit\Framework\TestCase;
8
use Predis\Connection\Aggregate\PredisCluster;
9
use Predis\Connection\Aggregate\RedisCluster;
10
use Prophecy\PhpUnit\ProphecyTrait;
11
use Prophecy\Prophecy\ObjectProphecy;
12
use Psr\Container\ContainerInterface;
13
use Shlinkio\Shlink\Common\Cache\RedisFactory;
14
15
class RedisFactoryTest extends TestCase
16
{
17
    use ProphecyTrait;
18
19
    private RedisFactory $factory;
20
    private ObjectProphecy $container;
21
22
    public function setUp(): void
23
    {
24
        $this->container = $this->prophesize(ContainerInterface::class);
25
        $this->factory = new RedisFactory();
26
    }
27
28
    /**
29
     * @test
30
     * @dataProvider provideRedisConfig
31
     */
32
    public function createsRedisClientBasedOnRedisConfig(?array $config, string $expectedCluster): void
33
    {
34
        $getConfig = $this->container->get('config')->willReturn([
35
            'redis' => $config,
36
        ]);
37
38
        $client = ($this->factory)($this->container->reveal());
39
40
        $getConfig->shouldHaveBeenCalledOnce();
41
        $this->assertInstanceOf($expectedCluster, $client->getOptions()->cluster);
42
    }
43
44
    /**
45
     * @test
46
     * @dataProvider provideRedisConfig
47
     */
48
    public function createsRedisClientBasedOnCacheConfig(?array $config, string $expectedCluster): void
49
    {
50
        $getConfig = $this->container->get('config')->willReturn([
51
            'cache' => [
52
                'redis' => $config,
53
            ],
54
        ]);
55
56
        $client = ($this->factory)($this->container->reveal());
57
58
        $getConfig->shouldHaveBeenCalledOnce();
59
        $this->assertInstanceOf($expectedCluster, $client->getOptions()->cluster);
60
    }
61
62
    public function provideRedisConfig(): iterable
63
    {
64
        yield 'no config' => [null, PredisCluster::class];
65
        yield 'single server as string' => [[
66
            'servers' => 'tcp://127.0.0.1:6379',
67
        ], PredisCluster::class];
68
        yield 'single server as array' => [[
69
            'servers' => ['tcp://127.0.0.1:6379'],
70
        ], PredisCluster::class];
71
        yield 'cluster of servers' => [[
72
            'servers' => ['tcp://1.1.1.1:6379', 'tcp://2.2.2.2:6379'],
73
        ], RedisCluster::class];
74
        yield 'empty cluster of servers' => [[
75
            'servers' => [],
76
        ], PredisCluster::class];
77
        yield 'cluster of servers as string' => [[
78
            'servers' => 'tcp://1.1.1.1:6379,tcp://2.2.2.2:6379',
79
        ], RedisCluster::class];
80
    }
81
}
82