RedisCacheTest.php 2.4 KB

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