Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd2565af

Browse files
robfrawleynicolas-grekas
authored andcommitted
[cache] Add tests for MemcachedAdapter::createClient()
1 parentd4e109b commitd2565af

File tree

5 files changed

+125
-9
lines changed

5 files changed

+125
-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.* ]]; thenpecl install -f memcached-2.1.0; fi
64+
-if [[ ! $skip &&!$PHP =hhvm* ]]; thenecho 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
<envname="LDAP_HOST"value="127.0.0.1" />
1717
<envname="LDAP_PORT"value="3389" />
1818
<envname="REDIS_HOST"value="localhost" />
19+
<envname="MEMCACHED_HOST"value="localhost" />
1920
</php>
2021

2122
<testsuites>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public static function createConnection($servers, array $options = array())
6767
}
6868
set_error_handler(function ($type,$msg,$file,$line) {thrownew \ErrorException($msg,0,$type,$file,$line); });
6969
try {
70+
if (!static::isSupported()) {
71+
thrownewtrigger_error('Memcached >= 2.2.0 is required');
72+
}
7073
$options +=static::$defaultClientOptions;
7174
$client =new \Memcached($options['persistent_id']);
7275
$username =$options['username'];

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

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

1212
namespaceSymfony\Component\Cache\Tests\Adapter;
1313

14+
useSymfony\Component\Cache\Adapter\AbstractAdapter;
1415
useSymfony\Component\Cache\Adapter\MemcachedAdapter;
16+
useSymfony\Component\Cache\Exception\CacheException;
1517

1618
class MemcachedAdapterTestextends AdapterTestCase
1719
{
@@ -28,23 +30,131 @@ public static function setupBeforeClass()
2830
if (!MemcachedAdapter::isSupported()) {
2931
self::markTestSkipped('Extension memcached >=2.2.0 required.');
3032
}
33+
self::$client = AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'));
34+
self::$client->get('foo');
35+
$code =self::$client->getResultCode();
3136

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

4142
publicfunctioncreateCachePool($defaultLifetime =0)
4243
{
4344
returnnewMemcachedAdapter(self::$client,str_replace('\\','.',__CLASS__),$defaultLifetime);
4445
}
4546

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

‎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
<ininame="error_reporting"value="-1" />
1111
<envname="REDIS_HOST"value="localhost" />
12+
<envname="MEMCACHED_HOST"value="localhost" />
1213
</php>
1314

1415
<testsuites>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp