RedisCacheTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Simple\RedisCache;
  12. /**
  13. * @group legacy
  14. */
  15. class RedisCacheTest extends AbstractRedisCacheTest
  16. {
  17. public static function setUpBeforeClass(): void
  18. {
  19. parent::setupBeforeClass();
  20. self::$redis = RedisCache::createConnection('redis://'.getenv('REDIS_HOST'));
  21. }
  22. public function testCreateConnection()
  23. {
  24. $redisHost = getenv('REDIS_HOST');
  25. $redis = RedisCache::createConnection('redis://'.$redisHost);
  26. $this->assertInstanceOf(\Redis::class, $redis);
  27. $this->assertTrue($redis->isConnected());
  28. $this->assertSame(0, $redis->getDbNum());
  29. $redis = RedisCache::createConnection('redis://'.$redisHost.'/2');
  30. $this->assertSame(2, $redis->getDbNum());
  31. $redis = RedisCache::createConnection('redis://'.$redisHost, ['timeout' => 3]);
  32. $this->assertEquals(3, $redis->getTimeout());
  33. $redis = RedisCache::createConnection('redis://'.$redisHost.'?timeout=4');
  34. $this->assertEquals(4, $redis->getTimeout());
  35. $redis = RedisCache::createConnection('redis://'.$redisHost, ['read_timeout' => 5]);
  36. $this->assertEquals(5, $redis->getReadTimeout());
  37. }
  38. /**
  39. * @dataProvider provideFailedCreateConnection
  40. */
  41. public function testFailedCreateConnection($dsn)
  42. {
  43. $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
  44. $this->expectExceptionMessage('Redis connection failed');
  45. RedisCache::createConnection($dsn);
  46. }
  47. public function provideFailedCreateConnection()
  48. {
  49. return [
  50. ['redis://localhost:1234'],
  51. ['redis://foo@localhost'],
  52. ['redis://localhost/123'],
  53. ];
  54. }
  55. /**
  56. * @dataProvider provideInvalidCreateConnection
  57. */
  58. public function testInvalidCreateConnection($dsn)
  59. {
  60. $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
  61. $this->expectExceptionMessage('Invalid Redis DSN');
  62. RedisCache::createConnection($dsn);
  63. }
  64. public function provideInvalidCreateConnection()
  65. {
  66. return [
  67. ['foo://localhost'],
  68. ['redis://'],
  69. ];
  70. }
  71. }