Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3eae606

Browse files
robfrawleynicolas-grekas
authored andcommitted
[cache] Add tests for MemcachedAdapter::createClient()
1 parent e109438 commit 3eae606

File tree

4 files changed

+134
-9
lines changed

4 files changed

+134
-9
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

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<env name="LDAP_HOST" value="127.0.0.1" />
1717
<env name="LDAP_PORT" value="3389" />
1818
<env name="REDIS_HOST" value="localhost" />
19+
<env name="MEMCACHED_HOST" value="localhost" />
1920
</php>
2021

2122
<testsuites>

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

Lines changed: 130 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Cache\Tests\Adapter;
1313

14+
use Symfony\Component\Cache\Adapter\AbstractAdapter;
1415
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
1516

1617
class MemcachedAdapterTest extends AdapterTestCase
@@ -28,23 +29,144 @@ public static function setupBeforeClass()
2829
if (!MemcachedAdapter::isSupported()) {
2930
self::markTestSkipped('Extension memcached >=2.2.0 required.');
3031
}
32+
self::$client = AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'));
33+
self::$client->get('foo');
34+
$code = self::$client->getResultCode();
3135

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();
36+
if (\Memcached::RES_SUCCESS !== $code && \Memcached::RES_NOTFOUND !== $code) {
37+
self::markTestSkipped('Memcached error: '.strtolower(self::$client->getResultMessage()));
38+
}
3939
}
4040

4141
public function createCachePool($defaultLifetime = 0)
4242
{
4343
return new MemcachedAdapter(self::$client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
4444
}
4545

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

src/Symfony/Component/Cache/phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<php>
1010
<ini name="error_reporting" value="-1" />
1111
<env name="REDIS_HOST" value="localhost" />
12+
<env name="MEMCACHED_HOST" value="localhost" />
1213
</php>
1314

1415
<testsuites>

0 commit comments

Comments
 (0)