RedisClusterAdapterTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\RedisAdapter;
  13. use Symfony\Component\Cache\Traits\RedisClusterProxy;
  14. class RedisClusterAdapterTest extends AbstractRedisAdapterTest
  15. {
  16. public static function setUpBeforeClass(): void
  17. {
  18. if (!class_exists('RedisCluster')) {
  19. self::markTestSkipped('The RedisCluster class is required.');
  20. }
  21. if (!$hosts = getenv('REDIS_CLUSTER_HOSTS')) {
  22. self::markTestSkipped('REDIS_CLUSTER_HOSTS env var is not defined.');
  23. }
  24. self::$redis = AbstractAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).']', ['lazy' => true, 'redis_cluster' => true]);
  25. }
  26. public function createCachePool($defaultLifetime = 0)
  27. {
  28. $this->assertInstanceOf(RedisClusterProxy::class, self::$redis);
  29. $adapter = new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
  30. return $adapter;
  31. }
  32. /**
  33. * @dataProvider provideFailedCreateConnection
  34. */
  35. public function testFailedCreateConnection($dsn)
  36. {
  37. $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
  38. $this->expectExceptionMessage('Redis connection failed');
  39. RedisAdapter::createConnection($dsn);
  40. }
  41. public function provideFailedCreateConnection()
  42. {
  43. return [
  44. ['redis://localhost:1234?redis_cluster=1'],
  45. ['redis://foo@localhost?redis_cluster=1'],
  46. ['redis://localhost/123?redis_cluster=1'],
  47. ];
  48. }
  49. }