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