RedisAdapterTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\RedisProxy;
  14. class RedisAdapterTest extends AbstractRedisAdapterTest
  15. {
  16. public static function setUpBeforeClass(): void
  17. {
  18. parent::setUpBeforeClass();
  19. self::$redis = AbstractAdapter::createConnection('redis://'.getenv('REDIS_HOST'), ['lazy' => true]);
  20. }
  21. public function createCachePool($defaultLifetime = 0)
  22. {
  23. $adapter = parent::createCachePool($defaultLifetime);
  24. $this->assertInstanceOf(RedisProxy::class, self::$redis);
  25. return $adapter;
  26. }
  27. /**
  28. * @dataProvider provideValidSchemes
  29. */
  30. public function testCreateConnection($dsnScheme)
  31. {
  32. $redis = RedisAdapter::createConnection($dsnScheme.':?host[h1]&host[h2]&host[/foo:]');
  33. $this->assertInstanceOf(\RedisArray::class, $redis);
  34. $this->assertSame(['h1:6379', 'h2:6379', '/foo'], $redis->_hosts());
  35. @$redis = null; // some versions of phpredis connect on destruct, let's silence the warning
  36. $redisHost = getenv('REDIS_HOST');
  37. $redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost);
  38. $this->assertInstanceOf(\Redis::class, $redis);
  39. $this->assertTrue($redis->isConnected());
  40. $this->assertSame(0, $redis->getDbNum());
  41. $redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost.'/2');
  42. $this->assertSame(2, $redis->getDbNum());
  43. $redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost, ['timeout' => 3]);
  44. $this->assertEquals(3, $redis->getTimeout());
  45. $redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost.'?timeout=4');
  46. $this->assertEquals(4, $redis->getTimeout());
  47. $redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost, ['read_timeout' => 5]);
  48. $this->assertEquals(5, $redis->getReadTimeout());
  49. }
  50. /**
  51. * @dataProvider provideFailedCreateConnection
  52. */
  53. public function testFailedCreateConnection($dsn)
  54. {
  55. $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
  56. $this->expectExceptionMessage('Redis connection failed');
  57. RedisAdapter::createConnection($dsn);
  58. }
  59. public function provideFailedCreateConnection()
  60. {
  61. return [
  62. ['redis://localhost:1234'],
  63. ['redis://foo@localhost'],
  64. ['redis://localhost/123'],
  65. ];
  66. }
  67. /**
  68. * @dataProvider provideInvalidCreateConnection
  69. */
  70. public function testInvalidCreateConnection($dsn)
  71. {
  72. $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
  73. $this->expectExceptionMessage('Invalid Redis DSN');
  74. RedisAdapter::createConnection($dsn);
  75. }
  76. public function provideValidSchemes()
  77. {
  78. return [
  79. ['redis'],
  80. ['rediss'],
  81. ];
  82. }
  83. public function provideInvalidCreateConnection()
  84. {
  85. return [
  86. ['foo://localhost'],
  87. ['redis://'],
  88. ];
  89. }
  90. }