2017-05-02 23:54:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Core\Tests\Cache;
|
|
|
|
|
2023-05-17 00:18:28 +02:00
|
|
|
use Behat\Gherkin\Cache\MemoryCache;
|
2017-05-02 23:54:14 +02:00
|
|
|
use Psr\SimpleCache\CacheInterface;
|
|
|
|
use SilverStripe\Core\Cache\ApcuCacheFactory;
|
|
|
|
use SilverStripe\Core\Cache\MemcachedCacheFactory;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-09-27 17:43:01 +02:00
|
|
|
use SilverStripe\Core\Tests\Cache\CacheTest\MockCache;
|
2017-05-02 23:54:14 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2023-05-17 00:18:28 +02:00
|
|
|
use Symfony\Component\Cache\Psr16Cache;
|
|
|
|
use Symfony\Component\Cache\Adapter\ApcuAdapter;
|
|
|
|
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
|
|
|
|
use Memcached;
|
2017-05-02 23:54:14 +02:00
|
|
|
|
|
|
|
class CacheTest extends SapphireTest
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2017-05-02 23:54:14 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-05-11 07:38:29 +02:00
|
|
|
Injector::inst()
|
|
|
|
->load([
|
|
|
|
ApcuCacheFactory::class => [
|
|
|
|
'constructor' => [ 'version' => 'ss40test' ]
|
|
|
|
],
|
2023-05-17 00:18:28 +02:00
|
|
|
'MemcachedClient' => Memcached::class,
|
|
|
|
MemcachedCacheFactory::class => [
|
|
|
|
'constructor' => [ 'memcachedClient' => '%$MemcachedClient' ]
|
|
|
|
],
|
2017-05-11 07:38:29 +02:00
|
|
|
CacheInterface::class . '.TestApcuCache' => [
|
2017-05-02 23:54:14 +02:00
|
|
|
'factory' => ApcuCacheFactory::class,
|
|
|
|
'constructor' => [
|
|
|
|
'namespace' => 'TestApcuCache',
|
|
|
|
'defaultLifetime' => 2600,
|
|
|
|
],
|
2017-05-11 07:38:29 +02:00
|
|
|
],
|
|
|
|
CacheInterface::class . '.TestMemcache' => [
|
2017-05-02 23:54:14 +02:00
|
|
|
'factory' => MemcachedCacheFactory::class,
|
|
|
|
'constructor' => [
|
|
|
|
'namespace' => 'TestMemCache',
|
|
|
|
'defaultLifetime' => 5600,
|
|
|
|
],
|
2017-05-11 07:38:29 +02:00
|
|
|
],
|
2023-05-17 00:18:28 +02:00
|
|
|
Psr16Cache::class => MockCache::class,
|
|
|
|
ApcuAdapter::class => MockCache::class,
|
|
|
|
MemcachedAdapter::class => MockCache::class,
|
2017-05-11 07:38:29 +02:00
|
|
|
]);
|
2017-05-02 23:54:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testApcuCacheFactory()
|
|
|
|
{
|
2023-05-17 00:18:28 +02:00
|
|
|
$psr16Cache = Injector::inst()->get(CacheInterface::class . '.TestApcuCache');
|
|
|
|
$this->assertInstanceOf(MockCache::class, $psr16Cache);
|
|
|
|
$this->assertEquals(MockCache::class, get_class($psr16Cache->getArgs()[0]));
|
2017-05-02 23:54:14 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
[
|
2018-01-16 19:39:30 +01:00
|
|
|
'TestApcuCache_' . md5(BASE_PATH),
|
2017-05-02 23:54:14 +02:00
|
|
|
2600,
|
2017-05-11 07:38:29 +02:00
|
|
|
'ss40test'
|
2017-05-02 23:54:14 +02:00
|
|
|
],
|
2023-05-17 00:18:28 +02:00
|
|
|
$psr16Cache->getArgs()[0]->getArgs()
|
2017-05-02 23:54:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMemCacheFactory()
|
|
|
|
{
|
2023-05-17 00:18:28 +02:00
|
|
|
if (!class_exists(Memcached::class)) {
|
|
|
|
$this->markTestSkipped('Memcached is not installed');
|
|
|
|
}
|
|
|
|
$psr16Cache = Injector::inst()->get(CacheInterface::class . '.TestMemcache');
|
|
|
|
$this->assertInstanceOf(MockCache::class, $psr16Cache);
|
|
|
|
$this->assertEquals(MockCache::class, get_class($psr16Cache->getArgs()[0]));
|
2017-05-02 23:54:14 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
[
|
2023-05-17 00:18:28 +02:00
|
|
|
new MemCached(),
|
2018-01-16 19:39:30 +01:00
|
|
|
'TestMemCache_' . md5(BASE_PATH),
|
2017-05-02 23:54:14 +02:00
|
|
|
5600
|
|
|
|
],
|
2023-05-17 00:18:28 +02:00
|
|
|
$psr16Cache->getArgs()[0]->getArgs()
|
2017-05-02 23:54:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|