1111
1212namespace Symfony \Component \Cache \Tests \Adapter ;
1313
14+ use Symfony \Component \Cache \Adapter \AbstractAdapter ;
1415use Symfony \Component \Cache \Adapter \MemcachedAdapter ;
16+ use Symfony \Component \Cache \Exception \CacheException ;
1517
1618class MemcachedAdapterTestextends AdapterTestCase
1719{
@@ -28,23 +30,131 @@ public static function setupBeforeClass()
2830if (!MemcachedAdapter::isSupported ()) {
2931self ::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
4142public function createCachePool ($ defaultLifetime =0 )
4243 {
4344return new MemcachedAdapter (self ::$ client ,str_replace ('\\' ,'. ' ,__CLASS__ ),$ defaultLifetime );
4445 }
4546
46- public function testIsSupported ()
47+ public function testOptions ()
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+ public function testBadOptions ($ name ,$ value )
70+ {
71+ MemcachedAdapter::createConnection (array (),array ($ name =>$ value ));
72+ }
73+
74+ public function provideBadOptions ()
75+ {
76+ return array (
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+ public function testBinaryProtocol ()
89+ {
90+ new MemcachedAdapter (MemcachedAdapter::createConnection (array (),array ('binary_protocol ' =>false )));
91+ }
92+
93+ public function testDefaultOptions ()
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+ public function testServersSetting ($ 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 ) {return array ('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+ public function provideServersSetting ()
124+ {
125+ yield array (
126+ 'memcached://127.0.0.1/50 ' ,
127+ '127.0.0.1 ' ,
128+ 11211 ,
129+ );
130+ yield array (
131+ 'memcached://localhost:11222?weight=25 ' ,
132+ 'localhost ' ,
133+ 11222 ,
134+ );
135+ if (ini_get ('memcached.use_sasl ' )) {
136+ yield array (
137+ 'memcached://user:password@127.0.0.1?weight=50 ' ,
138+ '127.0.0.1 ' ,
139+ 11211 ,
140+ );
141+ }
142+ yield array (
143+ 'memcached:///var/run/memcached.sock?weight=25 ' ,
144+ '/var/run/memcached.sock ' ,
145+ 0 ,
146+ );
147+ yield array (
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+ yield array (
154+ 'memcached://user:password@/var/local/run/memcached.socket?weight=25 ' ,
155+ '/var/local/run/memcached.socket ' ,
156+ 0 ,
157+ );
158+ }
49159 }
50160}