2016-05-01 18:54:56 +03:00
|
|
|
<?php
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-08-07 14:50:38 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Common\Cache;
|
2016-05-01 18:54:56 +03:00
|
|
|
|
2019-08-10 18:44:09 +03:00
|
|
|
use Doctrine\Common\Cache;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-08-10 18:44:09 +03:00
|
|
|
use Predis\ClientInterface;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Psr\Container\ContainerInterface;
|
2019-08-07 14:50:38 +03:00
|
|
|
use Shlinkio\Shlink\Common\Cache\CacheFactory;
|
2019-08-10 18:44:09 +03:00
|
|
|
use Shlinkio\Shlink\Common\Cache\RedisFactory;
|
2016-05-01 18:54:56 +03:00
|
|
|
|
|
|
|
class CacheFactoryTest extends TestCase
|
|
|
|
{
|
2019-08-10 18:44:09 +03:00
|
|
|
/** @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
|
|
|
{
|
2019-08-10 18:44:09 +03:00
|
|
|
$this->container = $this->prophesize(ContainerInterface::class);
|
2016-05-01 18:54:56 +03:00
|
|
|
}
|
|
|
|
|
2019-08-10 18:44:09 +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
|
|
|
}
|
|
|
|
|
2019-08-10 18:44:09 +03:00
|
|
|
public function provideCacheConfig(): iterable
|
2016-05-01 18:54:56 +03:00
|
|
|
{
|
2019-08-10 18:44:09 +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-08-01 21:16:13 +03:00
|
|
|
}
|
2016-05-01 18:54:56 +03:00
|
|
|
}
|