2016-05-01 18:54:56 +03:00
|
|
|
<?php
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-07-19 18:38:41 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Common\Factory;
|
2016-05-01 18:54:56 +03:00
|
|
|
|
|
|
|
use Doctrine\Common\Cache\ApcuCache;
|
|
|
|
use Doctrine\Common\Cache\ArrayCache;
|
2016-08-01 21:16:13 +03:00
|
|
|
use Doctrine\Common\Cache\FilesystemCache;
|
2016-08-09 18:58:47 +03:00
|
|
|
use Doctrine\Common\Cache\MemcachedCache;
|
|
|
|
use Doctrine\Common\Cache\RedisCache;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-07-19 18:38:41 +03:00
|
|
|
use Shlinkio\Shlink\Common\Factory\CacheFactory;
|
2016-08-16 00:40:49 +03:00
|
|
|
use Shlinkio\Shlink\Core\Options\AppOptions;
|
2016-05-01 18:54:56 +03:00
|
|
|
use Zend\ServiceManager\ServiceManager;
|
2018-10-28 10:34:02 +03:00
|
|
|
use function count;
|
|
|
|
use function putenv;
|
|
|
|
use function realpath;
|
|
|
|
use function sys_get_temp_dir;
|
2016-05-01 18:54:56 +03:00
|
|
|
|
|
|
|
class CacheFactoryTest extends TestCase
|
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var CacheFactory */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $factory;
|
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->factory = new CacheFactory();
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public static function tearDownAfterClass(): void
|
2016-05-01 18:54:56 +03:00
|
|
|
{
|
|
|
|
putenv('APP_ENV');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function productionReturnsApcAdapter()
|
|
|
|
{
|
|
|
|
putenv('APP_ENV=pro');
|
2016-08-01 21:16:13 +03:00
|
|
|
$instance = $this->factory->__invoke($this->createSM(), '');
|
2016-05-01 18:54:56 +03:00
|
|
|
$this->assertInstanceOf(ApcuCache::class, $instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function developmentReturnsArrayAdapter()
|
|
|
|
{
|
|
|
|
putenv('APP_ENV=dev');
|
2016-08-01 21:16:13 +03:00
|
|
|
$instance = $this->factory->__invoke($this->createSM(), '');
|
2016-05-01 18:54:56 +03:00
|
|
|
$this->assertInstanceOf(ArrayCache::class, $instance);
|
|
|
|
}
|
2016-08-01 21:16:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function adapterDefinedInConfigIgnoresEnvironment()
|
|
|
|
{
|
|
|
|
putenv('APP_ENV=pro');
|
|
|
|
$instance = $this->factory->__invoke($this->createSM(ArrayCache::class), '');
|
|
|
|
$this->assertInstanceOf(ArrayCache::class, $instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function invalidAdapterDefinedInConfigFallbacksToEnvironment()
|
|
|
|
{
|
|
|
|
putenv('APP_ENV=pro');
|
2016-08-09 18:58:47 +03:00
|
|
|
$instance = $this->factory->__invoke($this->createSM(RedisCache::class), '');
|
2016-08-01 21:16:13 +03:00
|
|
|
$this->assertInstanceOf(ApcuCache::class, $instance);
|
|
|
|
}
|
|
|
|
|
2016-08-09 18:58:47 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function filesystemCacheAdaptersReadDirOption()
|
|
|
|
{
|
2017-10-17 12:03:12 +03:00
|
|
|
$dir = realpath(sys_get_temp_dir());
|
2016-08-09 18:58:47 +03:00
|
|
|
/** @var FilesystemCache $instance */
|
|
|
|
$instance = $this->factory->__invoke($this->createSM(FilesystemCache::class, ['dir' => $dir]), '');
|
|
|
|
$this->assertInstanceOf(FilesystemCache::class, $instance);
|
|
|
|
$this->assertEquals($dir, $instance->getDirectory());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function memcachedCacheAdaptersReadServersOption()
|
|
|
|
{
|
|
|
|
$servers = [
|
|
|
|
[
|
|
|
|
'host' => '1.2.3.4',
|
2017-10-12 11:13:20 +03:00
|
|
|
'port' => 123,
|
2016-08-09 18:58:47 +03:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'host' => '4.3.2.1',
|
2017-10-12 11:13:20 +03:00
|
|
|
'port' => 321,
|
2016-08-09 18:58:47 +03:00
|
|
|
],
|
|
|
|
];
|
|
|
|
/** @var MemcachedCache $instance */
|
|
|
|
$instance = $this->factory->__invoke($this->createSM(MemcachedCache::class, ['servers' => $servers]), '');
|
|
|
|
$this->assertInstanceOf(MemcachedCache::class, $instance);
|
2016-08-09 19:56:01 +03:00
|
|
|
$this->assertEquals(count($servers), count($instance->getMemcached()->getServerList()));
|
2016-08-09 18:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function createSM($cacheAdapter = null, array $options = [])
|
2016-08-01 21:16:13 +03:00
|
|
|
{
|
|
|
|
return new ServiceManager(['services' => [
|
|
|
|
'config' => isset($cacheAdapter) ? [
|
2016-08-09 18:58:47 +03:00
|
|
|
'cache' => [
|
|
|
|
'adapter' => $cacheAdapter,
|
|
|
|
'options' => $options,
|
|
|
|
],
|
2016-08-01 21:16:13 +03:00
|
|
|
] : [],
|
2016-08-16 00:40:49 +03:00
|
|
|
AppOptions::class => new AppOptions(),
|
2016-08-01 21:16:13 +03:00
|
|
|
]]);
|
|
|
|
}
|
2016-05-01 18:54:56 +03:00
|
|
|
}
|