|
12 | 12 | namespace Symfony\Component\Cache\Tests\Adapter;
|
13 | 13 |
|
14 | 14 | use Symfony\Component\Cache\Adapter\MemcachedAdapter;
|
| 15 | +use Symfony\Component\Cache\Exception\CacheException; |
15 | 16 |
|
16 | 17 | class MemcachedAdapterTest extends AdapterTestCase
|
17 | 18 | {
|
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 |
| - |
24 | 19 | private static $client;
|
25 | 20 |
|
26 | 21 | public static function setupBeforeClass()
|
27 | 22 | {
|
28 | 23 | if (!MemcachedAdapter::isSupported()) {
|
29 |
| - self::markTestSkipped('Extension memcached >=2.2.0 required.'); |
| 24 | + self::markTestSkipped('memcached >= 2.2.0 required'); |
30 | 25 | }
|
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))); |
39 | 27 | }
|
40 | 28 |
|
41 | 29 | public function createCachePool($defaultLifetime = 0)
|
42 | 30 | {
|
| 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 | + |
43 | 38 | return new MemcachedAdapter(self::$client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
|
44 | 39 | }
|
45 | 40 |
|
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() |
47 | 97 | {
|
48 | 98 | $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 | + } |
49 | 160 | }
|
50 | 161 | }
|
0 commit comments