MemcachedAdapterTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. */
  55. public function testBadOptions($name, $value)
  56. {
  57. if (\PHP_VERSION_ID < 80000) {
  58. $this->expectException('ErrorException');
  59. $this->expectExceptionMessage('constant(): Couldn\'t find constant Memcached::');
  60. } else {
  61. $this->expectException('Error');
  62. $this->expectExceptionMessage('Undefined constant Memcached::');
  63. }
  64. MemcachedAdapter::createConnection([], [$name => $value]);
  65. }
  66. public function provideBadOptions()
  67. {
  68. return [
  69. ['foo', 'bar'],
  70. ['hash', 'zyx'],
  71. ['serializer', 'zyx'],
  72. ['distribution', 'zyx'],
  73. ];
  74. }
  75. public function testDefaultOptions()
  76. {
  77. $this->assertTrue(MemcachedAdapter::isSupported());
  78. $client = MemcachedAdapter::createConnection([]);
  79. $this->assertTrue($client->getOption(\Memcached::OPT_COMPRESSION));
  80. $this->assertSame(1, $client->getOption(\Memcached::OPT_BINARY_PROTOCOL));
  81. $this->assertSame(1, $client->getOption(\Memcached::OPT_TCP_NODELAY));
  82. $this->assertSame(1, $client->getOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE));
  83. }
  84. public function testOptionSerializer()
  85. {
  86. $this->expectException('Symfony\Component\Cache\Exception\CacheException');
  87. $this->expectExceptionMessage('MemcachedAdapter: "serializer" option must be "php" or "igbinary".');
  88. if (!\Memcached::HAVE_JSON) {
  89. $this->markTestSkipped('Memcached::HAVE_JSON required');
  90. }
  91. new MemcachedAdapter(MemcachedAdapter::createConnection([], ['serializer' => 'json']));
  92. }
  93. /**
  94. * @dataProvider provideServersSetting
  95. */
  96. public function testServersSetting($dsn, $host, $port)
  97. {
  98. $client1 = MemcachedAdapter::createConnection($dsn);
  99. $client2 = MemcachedAdapter::createConnection([$dsn]);
  100. $client3 = MemcachedAdapter::createConnection([[$host, $port]]);
  101. $expect = [
  102. 'host' => $host,
  103. 'port' => $port,
  104. ];
  105. $f = function ($s) { return ['host' => $s['host'], 'port' => $s['port']]; };
  106. $this->assertSame([$expect], array_map($f, $client1->getServerList()));
  107. $this->assertSame([$expect], array_map($f, $client2->getServerList()));
  108. $this->assertSame([$expect], array_map($f, $client3->getServerList()));
  109. }
  110. public function provideServersSetting()
  111. {
  112. yield [
  113. 'memcached://127.0.0.1/50',
  114. '127.0.0.1',
  115. 11211,
  116. ];
  117. yield [
  118. 'memcached://localhost:11222?weight=25',
  119. 'localhost',
  120. 11222,
  121. ];
  122. if (filter_var(ini_get('memcached.use_sasl'), \FILTER_VALIDATE_BOOLEAN)) {
  123. yield [
  124. 'memcached://user:password@127.0.0.1?weight=50',
  125. '127.0.0.1',
  126. 11211,
  127. ];
  128. }
  129. yield [
  130. 'memcached:///var/run/memcached.sock?weight=25',
  131. '/var/run/memcached.sock',
  132. 0,
  133. ];
  134. yield [
  135. 'memcached:///var/local/run/memcached.socket?weight=25',
  136. '/var/local/run/memcached.socket',
  137. 0,
  138. ];
  139. if (filter_var(ini_get('memcached.use_sasl'), \FILTER_VALIDATE_BOOLEAN)) {
  140. yield [
  141. 'memcached://user:password@/var/local/run/memcached.socket?weight=25',
  142. '/var/local/run/memcached.socket',
  143. 0,
  144. ];
  145. }
  146. }
  147. /**
  148. * @dataProvider provideDsnWithOptions
  149. */
  150. public function testDsnWithOptions($dsn, array $options, array $expectedOptions)
  151. {
  152. $client = MemcachedAdapter::createConnection($dsn, $options);
  153. foreach ($expectedOptions as $option => $expect) {
  154. $this->assertSame($expect, $client->getOption($option));
  155. }
  156. }
  157. public function provideDsnWithOptions()
  158. {
  159. if (!class_exists('\Memcached')) {
  160. self::markTestSkipped('Extension memcached required.');
  161. }
  162. yield [
  163. 'memcached://localhost:11222?retry_timeout=10',
  164. [\Memcached::OPT_RETRY_TIMEOUT => 8],
  165. [\Memcached::OPT_RETRY_TIMEOUT => 10],
  166. ];
  167. yield [
  168. 'memcached://localhost:11222?socket_recv_size=1&socket_send_size=2',
  169. [\Memcached::OPT_RETRY_TIMEOUT => 8],
  170. [\Memcached::OPT_SOCKET_RECV_SIZE => 1, \Memcached::OPT_SOCKET_SEND_SIZE => 2, \Memcached::OPT_RETRY_TIMEOUT => 8],
  171. ];
  172. }
  173. public function testClear()
  174. {
  175. $this->assertTrue($this->createCachePool()->clear());
  176. }
  177. }