MemcachedCacheTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\Simple;
  11. use Symfony\Component\Cache\Adapter\AbstractAdapter;
  12. use Symfony\Component\Cache\Simple\MemcachedCache;
  13. /**
  14. * @group legacy
  15. */
  16. class MemcachedCacheTest extends CacheTestCase
  17. {
  18. protected $skippedTests = [
  19. 'testSetTtl' => 'Testing expiration slows down the test suite',
  20. 'testSetMultipleTtl' => 'Testing expiration slows down the test suite',
  21. 'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
  22. ];
  23. protected static $client;
  24. public static function setUpBeforeClass(): void
  25. {
  26. if (!MemcachedCache::isSupported()) {
  27. self::markTestSkipped('Extension memcached >=2.2.0 required.');
  28. }
  29. self::$client = AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'));
  30. self::$client->get('foo');
  31. $code = self::$client->getResultCode();
  32. if (\Memcached::RES_SUCCESS !== $code && \Memcached::RES_NOTFOUND !== $code) {
  33. self::markTestSkipped('Memcached error: '.strtolower(self::$client->getResultMessage()));
  34. }
  35. }
  36. public function createSimpleCache($defaultLifetime = 0)
  37. {
  38. $client = $defaultLifetime ? AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'), ['binary_protocol' => false]) : self::$client;
  39. return new MemcachedCache($client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
  40. }
  41. public function testCreatePersistentConnectionShouldNotDupServerList()
  42. {
  43. $instance = MemcachedCache::createConnection('memcached://'.getenv('MEMCACHED_HOST'), ['persistent_id' => 'persistent']);
  44. $this->assertCount(1, $instance->getServerList());
  45. $instance = MemcachedCache::createConnection('memcached://'.getenv('MEMCACHED_HOST'), ['persistent_id' => 'persistent']);
  46. $this->assertCount(1, $instance->getServerList());
  47. }
  48. public function testOptions()
  49. {
  50. $client = MemcachedCache::createConnection([], [
  51. 'libketama_compatible' => false,
  52. 'distribution' => 'modula',
  53. 'compression' => true,
  54. 'serializer' => 'php',
  55. 'hash' => 'md5',
  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. * @dataProvider provideBadOptions
  65. */
  66. public function testBadOptions($name, $value)
  67. {
  68. $this->expectException('ErrorException');
  69. $this->expectExceptionMessage('constant(): Couldn\'t find constant Memcached::');
  70. MemcachedCache::createConnection([], [$name => $value]);
  71. }
  72. public function provideBadOptions()
  73. {
  74. return [
  75. ['foo', 'bar'],
  76. ['hash', 'zyx'],
  77. ['serializer', 'zyx'],
  78. ['distribution', 'zyx'],
  79. ];
  80. }
  81. public function testDefaultOptions()
  82. {
  83. $this->assertTrue(MemcachedCache::isSupported());
  84. $client = MemcachedCache::createConnection([]);
  85. $this->assertTrue($client->getOption(\Memcached::OPT_COMPRESSION));
  86. $this->assertSame(1, $client->getOption(\Memcached::OPT_BINARY_PROTOCOL));
  87. $this->assertSame(1, $client->getOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE));
  88. }
  89. public function testOptionSerializer()
  90. {
  91. $this->expectException('Symfony\Component\Cache\Exception\CacheException');
  92. $this->expectExceptionMessage('MemcachedAdapter: "serializer" option must be "php" or "igbinary".');
  93. if (!\Memcached::HAVE_JSON) {
  94. $this->markTestSkipped('Memcached::HAVE_JSON required');
  95. }
  96. new MemcachedCache(MemcachedCache::createConnection([], ['serializer' => 'json']));
  97. }
  98. /**
  99. * @dataProvider provideServersSetting
  100. */
  101. public function testServersSetting($dsn, $host, $port)
  102. {
  103. $client1 = MemcachedCache::createConnection($dsn);
  104. $client2 = MemcachedCache::createConnection([$dsn]);
  105. $client3 = MemcachedCache::createConnection([[$host, $port]]);
  106. $expect = [
  107. 'host' => $host,
  108. 'port' => $port,
  109. ];
  110. $f = function ($s) { return ['host' => $s['host'], 'port' => $s['port']]; };
  111. $this->assertSame([$expect], array_map($f, $client1->getServerList()));
  112. $this->assertSame([$expect], array_map($f, $client2->getServerList()));
  113. $this->assertSame([$expect], array_map($f, $client3->getServerList()));
  114. }
  115. public function provideServersSetting()
  116. {
  117. yield [
  118. 'memcached://127.0.0.1/50',
  119. '127.0.0.1',
  120. 11211,
  121. ];
  122. yield [
  123. 'memcached://localhost:11222?weight=25',
  124. 'localhost',
  125. 11222,
  126. ];
  127. if (filter_var(ini_get('memcached.use_sasl'), FILTER_VALIDATE_BOOLEAN)) {
  128. yield [
  129. 'memcached://user:password@127.0.0.1?weight=50',
  130. '127.0.0.1',
  131. 11211,
  132. ];
  133. }
  134. yield [
  135. 'memcached:///var/run/memcached.sock?weight=25',
  136. '/var/run/memcached.sock',
  137. 0,
  138. ];
  139. yield [
  140. 'memcached:///var/local/run/memcached.socket?weight=25',
  141. '/var/local/run/memcached.socket',
  142. 0,
  143. ];
  144. if (filter_var(ini_get('memcached.use_sasl'), FILTER_VALIDATE_BOOLEAN)) {
  145. yield [
  146. 'memcached://user:password@/var/local/run/memcached.socket?weight=25',
  147. '/var/local/run/memcached.socket',
  148. 0,
  149. ];
  150. }
  151. }
  152. }