MemcachedAdapterTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Tests\Adapter;
  11. use Symfony\Component\Cache\Adapter\AbstractAdapter;
  12. use Symfony\Component\Cache\Adapter\MemcachedAdapter;
  13. class MemcachedAdapterTest extends AdapterTestCase
  14. {
  15. protected $skippedTests = [
  16. 'testHasItemReturnsFalseWhenDeferredItemIsExpired' => 'Testing expiration slows down the test suite',
  17. 'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
  18. ];
  19. protected static $client;
  20. public static function setupBeforeClass()
  21. {
  22. if (!MemcachedAdapter::isSupported()) {
  23. self::markTestSkipped('Extension memcached >=2.2.0 required.');
  24. }
  25. self::$client = AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'), ['binary_protocol' => false]);
  26. self::$client->get('foo');
  27. $code = self::$client->getResultCode();
  28. if (\Memcached::RES_SUCCESS !== $code && \Memcached::RES_NOTFOUND !== $code) {
  29. self::markTestSkipped('Memcached error: '.strtolower(self::$client->getResultMessage()));
  30. }
  31. }
  32. public function createCachePool($defaultLifetime = 0)
  33. {
  34. $client = $defaultLifetime ? AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST')) : self::$client;
  35. return new MemcachedAdapter($client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
  36. }
  37. public function testOptions()
  38. {
  39. $client = MemcachedAdapter::createConnection([], [
  40. 'libketama_compatible' => false,
  41. 'distribution' => 'modula',
  42. 'compression' => true,
  43. 'serializer' => 'php',
  44. 'hash' => 'md5',
  45. ]);
  46. $this->assertSame(\Memcached::SERIALIZER_PHP, $client->getOption(\Memcached::OPT_SERIALIZER));
  47. $this->assertSame(\Memcached::HASH_MD5, $client->getOption(\Memcached::OPT_HASH));
  48. $this->assertTrue($client->getOption(\Memcached::OPT_COMPRESSION));
  49. $this->assertSame(0, $client->getOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE));
  50. $this->assertSame(\Memcached::DISTRIBUTION_MODULA, $client->getOption(\Memcached::OPT_DISTRIBUTION));
  51. }
  52. /**
  53. * @dataProvider provideBadOptions
  54. * @expectedException \ErrorException
  55. * @expectedExceptionMessage constant(): Couldn't find constant Memcached::
  56. */
  57. public function testBadOptions($name, $value)
  58. {
  59. MemcachedAdapter::createConnection([], [$name => $value]);
  60. }
  61. public function provideBadOptions()
  62. {
  63. return [
  64. ['foo', 'bar'],
  65. ['hash', 'zyx'],
  66. ['serializer', 'zyx'],
  67. ['distribution', 'zyx'],
  68. ];
  69. }
  70. public function testDefaultOptions()
  71. {
  72. $this->assertTrue(MemcachedAdapter::isSupported());
  73. $client = MemcachedAdapter::createConnection([]);
  74. $this->assertTrue($client->getOption(\Memcached::OPT_COMPRESSION));
  75. $this->assertSame(1, $client->getOption(\Memcached::OPT_BINARY_PROTOCOL));
  76. $this->assertSame(1, $client->getOption(\Memcached::OPT_TCP_NODELAY));
  77. $this->assertSame(1, $client->getOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE));
  78. }
  79. /**
  80. * @expectedException \Symfony\Component\Cache\Exception\CacheException
  81. * @expectedExceptionMessage MemcachedAdapter: "serializer" option must be "php" or "igbinary".
  82. */
  83. public function testOptionSerializer()
  84. {
  85. if (!\Memcached::HAVE_JSON) {
  86. $this->markTestSkipped('Memcached::HAVE_JSON required');
  87. }
  88. new MemcachedAdapter(MemcachedAdapter::createConnection([], ['serializer' => 'json']));
  89. }
  90. /**
  91. * @dataProvider provideServersSetting
  92. */
  93. public function testServersSetting($dsn, $host, $port)
  94. {
  95. $client1 = MemcachedAdapter::createConnection($dsn);
  96. $client2 = MemcachedAdapter::createConnection([$dsn]);
  97. $client3 = MemcachedAdapter::createConnection([[$host, $port]]);
  98. $expect = [
  99. 'host' => $host,
  100. 'port' => $port,
  101. ];
  102. $f = function ($s) { return ['host' => $s['host'], 'port' => $s['port']]; };
  103. $this->assertSame([$expect], array_map($f, $client1->getServerList()));
  104. $this->assertSame([$expect], array_map($f, $client2->getServerList()));
  105. $this->assertSame([$expect], array_map($f, $client3->getServerList()));
  106. }
  107. public function provideServersSetting()
  108. {
  109. yield [
  110. 'memcached://127.0.0.1/50',
  111. '127.0.0.1',
  112. 11211,
  113. ];
  114. yield [
  115. 'memcached://localhost:11222?weight=25',
  116. 'localhost',
  117. 11222,
  118. ];
  119. if (filter_var(ini_get('memcached.use_sasl'), FILTER_VALIDATE_BOOLEAN)) {
  120. yield [
  121. 'memcached://user:password@127.0.0.1?weight=50',
  122. '127.0.0.1',
  123. 11211,
  124. ];
  125. }
  126. yield [
  127. 'memcached:///var/run/memcached.sock?weight=25',
  128. '/var/run/memcached.sock',
  129. 0,
  130. ];
  131. yield [
  132. 'memcached:///var/local/run/memcached.socket?weight=25',
  133. '/var/local/run/memcached.socket',
  134. 0,
  135. ];
  136. if (filter_var(ini_get('memcached.use_sasl'), FILTER_VALIDATE_BOOLEAN)) {
  137. yield [
  138. 'memcached://user:password@/var/local/run/memcached.socket?weight=25',
  139. '/var/local/run/memcached.socket',
  140. 0,
  141. ];
  142. }
  143. }
  144. /**
  145. * @dataProvider provideDsnWithOptions
  146. */
  147. public function testDsnWithOptions($dsn, array $options, array $expectedOptions)
  148. {
  149. $client = MemcachedAdapter::createConnection($dsn, $options);
  150. foreach ($expectedOptions as $option => $expect) {
  151. $this->assertSame($expect, $client->getOption($option));
  152. }
  153. }
  154. public function provideDsnWithOptions()
  155. {
  156. if (!class_exists('\Memcached')) {
  157. self::markTestSkipped('Extension memcached required.');
  158. }
  159. yield [
  160. 'memcached://localhost:11222?retry_timeout=10',
  161. [\Memcached::OPT_RETRY_TIMEOUT => 8],
  162. [\Memcached::OPT_RETRY_TIMEOUT => 10],
  163. ];
  164. yield [
  165. 'memcached://localhost:11222?socket_recv_size=1&socket_send_size=2',
  166. [\Memcached::OPT_RETRY_TIMEOUT => 8],
  167. [\Memcached::OPT_SOCKET_RECV_SIZE => 1, \Memcached::OPT_SOCKET_SEND_SIZE => 2, \Memcached::OPT_RETRY_TIMEOUT => 8],
  168. ];
  169. }
  170. public function testClear()
  171. {
  172. $this->assertTrue($this->createCachePool()->clear());
  173. }
  174. }