8000 [cache] Add tests for MemcachedAdapter::createClient() · symfony/symfony@70e509a · GitHub
[go: up one dir, main page]

Skip to content

Commit 70e509a

Browse files
robfrawleynicolas-grekas
authored andcommitted
[cache] Add tests for MemcachedAdapter::createClient()
1 parent d6e4a2c commit 70e509a

File tree

3 files changed

+132
-17
lines changed

3 files changed

+132
-17
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ cache:
3838
- php-$MIN_PHP
3939

4040
services:
41+
- memcached
4142
- mongodb
4243
- redis-server
4344

@@ -60,7 +61,7 @@ before_install:
6061
- if [[ ! $skip && $PHP = 5.* ]]; then (echo yes | pecl install -f apcu-4.0.11 && echo apc.enable_cli = 1 >> $INI_FILE); fi
6162
- if [[ ! $skip && $PHP = 7.* ]]; then (echo yes | pecl install -f apcu-5.1.6 && echo apc.enable_cli = 1 >> $INI_FILE); fi
6263
- if [[ ! $deps && $PHP = 5.* ]]; then (cd src/Symfony/Component/Debug/Resources/ext && phpize && ./configure && make && echo extension = $(pwd)/modules/symfony_debug.so >> $INI_FILE); fi
63-
- if [[ ! $skip && $PHP = 5.* ]]; then pecl install -f memcached-2.1.0; fi
64+
- if [[ ! $skip && ! $PHP = hhvm* ]]; then echo extension = memcached.so >> $INI_FILE; fi
6465
- if [[ ! $skip && ! $PHP = hhvm* ]]; then echo extension = ldap.so >> $INI_FILE; fi
6566
- if [[ ! $skip && ! $PHP = hhvm* ]]; then echo extension = redis.so >> $INI_FILE; fi;
6667
- if [[ ! $skip && ! $PHP = hhvm* ]]; then phpenv config-rm xdebug.ini || echo "xdebug not available"; fi

src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public static function createClient($servers, array $options = array())
7272
}
7373
set_error_handler(function ($type, $msg, $file, $line) { throw new \ErrorException($msg, 0, $type, $file, $line); });
7474
try {
75+
if (!static::isSupported()) {
76+
throw new trigger_error('Memcached >= 2.2.0 is required');
77+
}
7578
$options += static::$defaultClientOptions;
7679
$client = new \Memcached($options['persistent_id']);
7780
$username = $options['username'];

src/Symfony/Component/Cache/Tests/Adapter/MemcachedAdapterTest.php

Lines changed: 127 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,150 @@
1212
namespace Symfony\Component\Cache\Tests\Adapter;
1313

1414
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
15+
use Symfony\Component\Cache\Exception\CacheException;
1516

1617
class MemcachedAdapterTest extends AdapterTestCase
1718
{
18-
protected $skippedTests = array(
19-
'testExpiration' => 'Testing expiration slows down the test suite',
20-
'testHasItemReturnsFalseWhenDeferredItemIsExpired' => 'Testing expiration slows down the test suite',
21-
'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
22-
);
23-
2419
private static $client;
2520

2621
public static function setupBeforeClass()
2722
{
2823
if (!MemcachedAdapter::isSupported()) {
29-
self::markTestSkipped('Extension memcached >=2.2.0 required.');
24+
self::markTestSkipped('memcached >= 2.2.0 required');
3025
}
31-
32-
self::$client = new \Memcached();
33-
self::$client->addServers(array(array(
34-
getenv('MEMCACHED_HOST') ?: '127.0.0.1',
35-
getenv('MEMCACHED_PORT') ?: 11211,
36-
)));
37-
38-
parent::setupBeforeClass();
26+
self::$client = MemcachedAdapter::createClient(array(array(getenv('MEMCACHED_HOST') ?: '127.0.0.1', getenv('MEMCACHED_PORT') ?: 11211)));
3927
}
4028

4129
public function createCachePool($defaultLifetime = 0)
4230
{
31+
self::$client->get('foo');
32+
$code = self::$client->getResultCode();
33+
34+
if (\Memcached::RES_SUCCESS !== $code && \Memcached::RES_NOTFOUND !== $code) {
35+
self::markTestSkipped('Memcached error: '.strtolower(self::$client->getResultMessage()));
36+
}
37+
4338
return new MemcachedAdapter(self::$client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
4439
}
4540

46-
public function testIsSupported()
41+
public function testOptions()
42+
{
43+
$client = MemcachedAdapter::createClient(array(), array(
44+
'libketama_compatible' => false,
45+
'distribution' => 'modula',
46+
'compression' => true,
47+
'serializer' => 'php',
48+
'hash' => 'md5',
49+
));
50+
51+
$this->assertSame(\Memcached::SERIALIZER_PHP, $client->getOption(\Memcached::OPT_SERIALIZER));
52+
$this->assertSame(\Memcached::HASH_MD5, $client->getOption(\Memcached::OPT_HASH));
53+
$this->assertTrue($client->getOption(\Memcached::OPT_COMPRESSION));
54+
$this->assertSame(0, $client->getOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE));
55+
$this->assertSame(\Memcached::DISTRIBUTION_MODULA, $client->getOption(\Memcached::OPT_DISTRIBUTION));
56+
}
57+
58+
/**
59+
* @dataProvider provideBadOptions
60+
* @expectedException \ErrorException
61+
* @expectedExceptionMessage constant(): Couldn't find constant Memcached::
62+
*/
63+
public function testBadOptions($name, $value)
64+
{
65+
MemcachedAdapter::createClient(array(), array($name => $value));
66+
}
67+
68+
public function provideBadOptions()
69+
{
70+
return array(
71+
array('foo', 'bar'),
72+
array('hash', 'zyx'),
73+
array('serializer', 'zyx'),
74+
array('distribution', 'zyx'),
75+
);
76+
}
77+
78+
/**
79+
* @dataProvider provideInvalidOptions
80+
* @expectedException \Symfony\Component\Cache\Exception\CacheException
81+
*/
82+
public function testInvalidOptions($name, $value, $expectedMessage)
83+
{
84+
$this->setExpectedException(CacheException::class, $expectedMessage);
85+
new MemcachedAdapter(MemcachedAdapter::createClient(array(), array($name => $value)));
86+
}
87+
88+
public function provideInvalidOptions()
89+
{
90+
return array(
91+
array('binary_protocol', false, 'MemcachedAdapter: OPT_BINARY_PROTOCOL must be used.'),
92+
array('no_block', true, 'MemcachedAdapter: OPT_NO_BLOCK must be disabled.'),
93+
);
94+
}
95+
96+
public function testDefaultOptions()
4797
{
4898
$this->assertTrue(MemcachedAdapter::isSupported());
99+
100+
$client = MemcachedAdapter::createClient(array());
101+
102+
$this->assertTrue($client->getOption(\Memcached::OPT_COMPRESSION));
103+
$this->assertSame(1, $client->getOption(\Memcached::OPT_BINARY_PROTOCOL));
104+
$this->assertSame(1, $client->getOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE));
105+
}
106+
107+
/**
108+
* @dataProvider provideServersSetting
109+
*/
110+
public function testServersSetting($dsn, $host, $port)
111+
{
112+
$client1 = MemcachedAdapter::createClient($dsn);
113+
$client2 = MemcachedAdapter::createClient(array($dsn));
114+
$expect = array(
115+
'host' => $host,
116+
'port' => $port,
117+
);
118+
119+
$f = function ($s) { return array('host' => $s['host'], 'port' => $s['port']); };
120+
$this->assertSame(array($expect), array_map($f, $client1->getServerList()));
121+
$this->assertSame(array($expect), array_map($f, $client2->getServerList()));
122+
}
123+
124+
public function provideServersSetting()
125+
{
126+
yield array(
127+
'memcached://127.0.0.1/50',
128+
'127.0.0.1',
129+
11211,
130+
);
131+
yield array(
132+
'memcached://localhost:11222?weight=25',
133+
'localhost',
134+
11222,
135+
);
136+
if (ini_get('memcached.use_sasl')) {
137+
yield array(
138+
'memcached://user:password@127.0.0.1?weight=50',
139+
'127.0.0.1',
140+
11211,
141+
);
142+
}
143+
yield array(
144+
'memcached:///var/run/memcached.sock?weight=25',
145+
'/var/run/memcached.sock',
146+
0,
147+
);
148+
yield array(
149+
'memcached:///var/local/run/memcached.socket?weight=25',
150+
'/var/local/run/memcached.socket',
151+
0,
152+
);
153+
if (ini_get('memcached.use_sasl')) {
154+
yield array(
155+
'memcached://user:password@/var/local/run/memcached.socket?weight=25',
156+
'/var/local/run/memcached.socket',
157+
0,
158+
);
159+
}
49160
}
50161
}

0 commit comments

Comments
 (0)
0