shlink/module/Common/test/Cache/CacheFactoryTest.php

63 lines
2 KiB
PHP
Raw Normal View History

2016-05-01 18:54:56 +03:00
<?php
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Common\Cache;
2016-05-01 18:54:56 +03:00
use Doctrine\Common\Cache;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
use Predis\ClientInterface;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Container\ContainerInterface;
use Shlinkio\Shlink\Common\Cache\CacheFactory;
use Shlinkio\Shlink\Common\Cache\RedisFactory;
2016-05-01 18:54:56 +03:00
class CacheFactoryTest extends TestCase
{
/** @var ObjectProphecy */
private $container;
2016-05-01 18:54:56 +03:00
2019-02-16 12:53:45 +03:00
public function setUp(): void
2016-05-01 18:54:56 +03:00
{
$this->container = $this->prophesize(ContainerInterface::class);
2016-05-01 18:54:56 +03:00
}
/**
* @test
* @dataProvider provideCacheConfig
*/
public function expectedCacheAdapterIsReturned(
array $config,
string $expectedAdapterClass,
string $expectedNamespace,
?callable $apcuEnabled = null
): void {
$factory = new CacheFactory($apcuEnabled);
$getConfig = $this->container->get('config')->willReturn($config);
$getRedis = $this->container->get(RedisFactory::SERVICE_NAME)->willReturn(
$this->prophesize(ClientInterface::class)->reveal()
);
$cache = $factory($this->container->reveal());
$this->assertInstanceOf($expectedAdapterClass, $cache);
$this->assertEquals($expectedNamespace, $cache->getNamespace());
$getConfig->shouldHaveBeenCalledOnce();
$getRedis->shouldHaveBeenCalledTimes($expectedAdapterClass === Cache\PredisCache::class ? 1 :0);
2016-05-01 18:54:56 +03:00
}
public function provideCacheConfig(): iterable
2016-05-01 18:54:56 +03:00
{
yield 'debug true' => [['debug' => true], Cache\ArrayCache::class, ''];
yield 'debug false' => [['debug' => false], Cache\ApcuCache::class, ''];
yield 'no debug' => [[], Cache\ApcuCache::class, ''];
yield 'with redis' => [['cache' => [
'namespace' => $namespace = 'some_namespace',
'redis' => [],
]], Cache\PredisCache::class, $namespace];
yield 'debug false and no apcu' => [['debug' => false], Cache\ArrayCache::class, '', function () {
return false;
}];
}
2016-05-01 18:54:56 +03:00
}