MemcachedCacheTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * @expectedException \ErrorException
  63. * @expectedExceptionMessage constant(): Couldn't find constant Memcached::
  64. */
  65. public function testBadOptions($name, $value)
  66. {
  67. MemcachedCache::createConnection([], [$name => $value]);
  68. }
  69. public function provideBadOptions()
  70. {
  71. return [
  72. ['foo', 'bar'],
  73. ['hash', 'zyx'],
  74. ['serializer', 'zyx'],
  75. ['distribution', 'zyx'],
  76. ];
  77. }
  78. public function testDefaultOptions()
  79. {
  80. $this->assertTrue(MemcachedCache::isSupported());
  81. $client = MemcachedCache::createConnection([]);
  82. $this->assertTrue($client->getOption(\Memcached::OPT_COMPRESSION));
  83. $this->assertSame(1, $client->getOption(\Memcached::OPT_BINARY_PROTOCOL));
  84. $this->assertSame(1, $client->getOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE));
  85. }
  86. /**
  87. * @expectedException \Symfony\Component\Cache\Exception\CacheException
  88. * @expectedExceptionMessage MemcachedAdapter: "serializer" option must be "php" or "igbinary".
  89. */
  90. public function testOptionSerializer()
  91. {
  92. if (!\Memcached::HAVE_JSON) {
  93. $this->markTestSkipped('Memcached::HAVE_JSON required');
  94. }
  95. new MemcachedCache(MemcachedCache::createConnection([], ['serializer' => 'json']));
  96. }
  97. /**
  98. * @dataProvider provideServersSetting
  99. */
  100. public function testServersSetting($dsn, $host, $port)
  101. {
  102. $client1 = MemcachedCache::createConnection($dsn);
  103. $client2 = MemcachedCache::createConnection([$dsn]);
  104. $client3 = MemcachedCache::createConnection([[$host, $port]]);
  105. $expect = [
  106. 'host' => $host,
  107. 'port' => $port,
  108. ];
  109. $f = function ($s) { return ['host' => $s['host'], 'port' => $s['port']]; };
  110. $this->assertSame([$expect], array_map($f, $client1->getServerList()));
  111. $this->assertSame([$expect], array_map($f, $client2->getServerList()));
  112. $this->assertSame([$expect], array_map($f, $client3->getServerList()));
  113. }
  114. public function provideServersSetting()
  115. {
  116. yield [
  117. 'memcached://127.0.0.1/50',
  118. '127.0.0.1',
  119. 11211,
  120. ];
  121. yield [
  122. 'memcached://localhost:11222?weight=25',
  123. 'localhost',
  124. 11222,
  125. ];
  126. if (filter_var(ini_get('memcached.use_sasl'), FILTER_VALIDATE_BOOLEAN)) {
  127. yield [
  128. 'memcached://user:password@127.0.0.1?weight=50',
  129. '127.0.0.1',
  130. 11211,
  131. ];
  132. }
  133. yield [
  134. 'memcached:///var/run/memcached.sock?weight=25',
  135. '/var/run/memcached.sock',
  136. 0,
  137. ];
  138. yield [
  139. 'memcached:///var/local/run/memcached.socket?weight=25',
  140. '/var/local/run/memcached.socket',
  141. 0,
  142. ];
  143. if (filter_var(ini_get('memcached.use_sasl'), FILTER_VALIDATE_BOOLEAN)) {
  144. yield [
  145. 'memcached://user:password@/var/local/run/memcached.socket?weight=25',
  146. '/var/local/run/memcached.socket',
  147. 0,
  148. ];
  149. }
  150. }
  151. }