2018-04-17 02:11:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Cache\Tests\Simple;
|
|
|
|
|
|
|
|
use Symfony\Component\Cache\Adapter\AbstractAdapter;
|
|
|
|
use Symfony\Component\Cache\Simple\MemcachedCache;
|
|
|
|
|
|
|
|
class MemcachedCacheTest extends CacheTestCase
|
|
|
|
{
|
2024-01-12 05:08:24 +00:00
|
|
|
protected $skippedTests = [
|
2018-04-17 02:11:51 +00:00
|
|
|
'testSetTtl' => 'Testing expiration slows down the test suite',
|
|
|
|
'testSetMultipleTtl' => 'Testing expiration slows down the test suite',
|
|
|
|
'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
|
2024-01-12 05:08:24 +00:00
|
|
|
];
|
2018-04-17 02:11:51 +00:00
|
|
|
|
|
|
|
protected static $client;
|
|
|
|
|
2024-01-12 05:08:24 +00:00
|
|
|
public static function setUpBeforeClass()
|
2018-04-17 02:11:51 +00:00
|
|
|
{
|
|
|
|
if (!MemcachedCache::isSupported()) {
|
|
|
|
self::markTestSkipped('Extension memcached >=2.2.0 required.');
|
|
|
|
}
|
|
|
|
self::$client = AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'));
|
|
|
|
self::$client->get('foo');
|
|
|
|
$code = self::$client->getResultCode();
|
|
|
|
|
|
|
|
if (\Memcached::RES_SUCCESS !== $code && \Memcached::RES_NOTFOUND !== $code) {
|
|
|
|
self::markTestSkipped('Memcached error: '.strtolower(self::$client->getResultMessage()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createSimpleCache($defaultLifetime = 0)
|
|
|
|
{
|
2024-01-12 05:08:24 +00:00
|
|
|
$client = $defaultLifetime ? AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'), ['binary_protocol' => false]) : self::$client;
|
2018-04-17 02:11:51 +00:00
|
|
|
|
|
|
|
return new MemcachedCache($client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
|
|
|
|
}
|
|
|
|
|
2024-01-12 05:08:24 +00:00
|
|
|
public function testCreatePersistentConnectionShouldNotDupServerList()
|
|
|
|
{
|
|
|
|
$instance = MemcachedCache::createConnection('memcached://'.getenv('MEMCACHED_HOST'), ['persistent_id' => 'persistent']);
|
|
|
|
$this->assertCount(1, $instance->getServerList());
|
|
|
|
|
|
|
|
$instance = MemcachedCache::createConnection('memcached://'.getenv('MEMCACHED_HOST'), ['persistent_id' => 'persistent']);
|
|
|
|
$this->assertCount(1, $instance->getServerList());
|
|
|
|
}
|
|
|
|
|
2018-04-17 02:11:51 +00:00
|
|
|
public function testOptions()
|
|
|
|
{
|
2024-01-12 05:08:24 +00:00
|
|
|
$client = MemcachedCache::createConnection([], [
|
2018-04-17 02:11:51 +00:00
|
|
|
'libketama_compatible' => false,
|
|
|
|
'distribution' => 'modula',
|
|
|
|
'compression' => true,
|
|
|
|
'serializer' => 'php',
|
|
|
|
'hash' => 'md5',
|
2024-01-12 05:08:24 +00:00
|
|
|
]);
|
2018-04-17 02:11:51 +00:00
|
|
|
|
|
|
|
$this->assertSame(\Memcached::SERIALIZER_PHP, $client->getOption(\Memcached::OPT_SERIALIZER));
|
|
|
|
$this->assertSame(\Memcached::HASH_MD5, $client->getOption(\Memcached::OPT_HASH));
|
|
|
|
$this->assertTrue($client->getOption(\Memcached::OPT_COMPRESSION));
|
|
|
|
$this->assertSame(0, $client->getOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE));
|
|
|
|
$this->assertSame(\Memcached::DISTRIBUTION_MODULA, $client->getOption(\Memcached::OPT_DISTRIBUTION));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideBadOptions
|
|
|
|
*/
|
|
|
|
public function testBadOptions($name, $value)
|
|
|
|
{
|
2024-01-12 05:08:24 +00:00
|
|
|
if (\PHP_VERSION_ID < 80000) {
|
|
|
|
$this->expectException('ErrorException');
|
|
|
|
$this->expectExceptionMessage('constant(): Couldn\'t find constant Memcached::');
|
|
|
|
} else {
|
|
|
|
$this->expectException('Error');
|
|
|
|
$this->expectExceptionMessage('Undefined constant Memcached::');
|
|
|
|
}
|
|
|
|
|
|
|
|
MemcachedCache::createConnection([], [$name => $value]);
|
2018-04-17 02:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideBadOptions()
|
|
|
|
{
|
2024-01-12 05:08:24 +00:00
|
|
|
return [
|
|
|
|
['foo', 'bar'],
|
|
|
|
['hash', 'zyx'],
|
|
|
|
['serializer', 'zyx'],
|
|
|
|
['distribution', 'zyx'],
|
|
|
|
];
|
2018-04-17 02:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefaultOptions()
|
|
|
|
{
|
|
|
|
$this->assertTrue(MemcachedCache::isSupported());
|
|
|
|
|
2024-01-12 05:08:24 +00:00
|
|
|
$client = MemcachedCache::createConnection([]);
|
2018-04-17 02:11:51 +00:00
|
|
|
|
|
|
|
$this->assertTrue($client->getOption(\Memcached::OPT_COMPRESSION));
|
|
|
|
$this->assertSame(1, $client->getOption(\Memcached::OPT_BINARY_PROTOCOL));
|
|
|
|
$this->assertSame(1, $client->getOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOptionSerializer()
|
|
|
|
{
|
2024-01-12 05:08:24 +00:00
|
|
|
$this->expectException('Symfony\Component\Cache\Exception\CacheException');
|
|
|
|
$this->expectExceptionMessage('MemcachedAdapter: "serializer" option must be "php" or "igbinary".');
|
2018-04-17 02:11:51 +00:00
|
|
|
if (!\Memcached::HAVE_JSON) {
|
|
|
|
$this->markTestSkipped('Memcached::HAVE_JSON required');
|
|
|
|
}
|
|
|
|
|
2024-01-12 05:08:24 +00:00
|
|
|
new MemcachedCache(MemcachedCache::createConnection([], ['serializer' => 'json']));
|
2018-04-17 02:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideServersSetting
|
|
|
|
*/
|
|
|
|
public function testServersSetting($dsn, $host, $port)
|
|
|
|
{
|
|
|
|
$client1 = MemcachedCache::createConnection($dsn);
|
2024-01-12 05:08:24 +00:00
|
|
|
$client2 = MemcachedCache::createConnection([$dsn]);
|
|
|
|
$client3 = MemcachedCache::createConnection([[$host, $port]]);
|
|
|
|
$expect = [
|
2018-04-17 02:11:51 +00:00
|
|
|
'host' => $host,
|
|
|
|
'port' => $port,
|
2024-01-12 05:08:24 +00:00
|
|
|
];
|
2018-04-17 02:11:51 +00:00
|
|
|
|
2024-01-12 05:08:24 +00:00
|
|
|
$f = function ($s) { return ['host' => $s['host'], 'port' => $s['port']]; };
|
|
|
|
$this->assertSame([$expect], array_map($f, $client1->getServerList()));
|
|
|
|
$this->assertSame([$expect], array_map($f, $client2->getServerList()));
|
|
|
|
$this->assertSame([$expect], array_map($f, $client3->getServerList()));
|
2018-04-17 02:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideServersSetting()
|
|
|
|
{
|
2024-01-12 05:08:24 +00:00
|
|
|
yield [
|
2018-04-17 02:11:51 +00:00
|
|
|
'memcached://127.0.0.1/50',
|
|
|
|
'127.0.0.1',
|
|
|
|
11211,
|
2024-01-12 05:08:24 +00:00
|
|
|
];
|
|
|
|
yield [
|
2018-04-17 02:11:51 +00:00
|
|
|
'memcached://localhost:11222?weight=25',
|
|
|
|
'localhost',
|
|
|
|
11222,
|
2024-01-12 05:08:24 +00:00
|
|
|
];
|
|
|
|
if (filter_var(ini_get('memcached.use_sasl'), \FILTER_VALIDATE_BOOLEAN)) {
|
|
|
|
yield [
|
2018-04-17 02:11:51 +00:00
|
|
|
'memcached://user:password@127.0.0.1?weight=50',
|
|
|
|
'127.0.0.1',
|
|
|
|
11211,
|
2024-01-12 05:08:24 +00:00
|
|
|
];
|
2018-04-17 02:11:51 +00:00
|
|
|
}
|
2024-01-12 05:08:24 +00:00
|
|
|
yield [
|
2018-04-17 02:11:51 +00:00
|
|
|
'memcached:///var/run/memcached.sock?weight=25',
|
|
|
|
'/var/run/memcached.sock',
|
|
|
|
0,
|
2024-01-12 05:08:24 +00:00
|
|
|
];
|
|
|
|
yield [
|
2018-04-17 02:11:51 +00:00
|
|
|
'memcached:///var/local/run/memcached.socket?weight=25',
|
|
|
|
'/var/local/run/memcached.socket',
|
|
|
|
0,
|
2024-01-12 05:08:24 +00:00
|
|
|
];
|
|
|
|
if (filter_var(ini_get('memcached.use_sasl'), \FILTER_VALIDATE_BOOLEAN)) {
|
|
|
|
yield [
|
2018-04-17 02:11:51 +00:00
|
|
|
'memcached://user:password@/var/local/run/memcached.socket?weight=25',
|
|
|
|
'/var/local/run/memcached.socket',
|
|
|
|
0,
|
2024-01-12 05:08:24 +00:00
|
|
|
];
|
2018-04-17 02:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|