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