MemcachedCacheTest.php 6.2 KB

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