RedisAdapterTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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()
  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. public function testCreateConnection()
  28. {
  29. $redisHost = getenv('REDIS_HOST');
  30. $redis = RedisAdapter::createConnection('redis://'.$redisHost);
  31. $this->assertInstanceOf(\Redis::class, $redis);
  32. $this->assertTrue($redis->isConnected());
  33. $this->assertSame(0, $redis->getDbNum());
  34. $redis = RedisAdapter::createConnection('redis://'.$redisHost.'/2');
  35. $this->assertSame(2, $redis->getDbNum());
  36. $redis = RedisAdapter::createConnection('redis://'.$redisHost, ['timeout' => 3]);
  37. $this->assertEquals(3, $redis->getTimeout());
  38. $redis = RedisAdapter::createConnection('redis://'.$redisHost.'?timeout=4');
  39. $this->assertEquals(4, $redis->getTimeout());
  40. $redis = RedisAdapter::createConnection('redis://'.$redisHost, ['read_timeout' => 5]);
  41. $this->assertEquals(5, $redis->getReadTimeout());
  42. }
  43. /**
  44. * @dataProvider provideFailedCreateConnection
  45. * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
  46. * @expectedExceptionMessage Redis connection failed
  47. */
  48. public function testFailedCreateConnection($dsn)
  49. {
  50. RedisAdapter::createConnection($dsn);
  51. }
  52. public function provideFailedCreateConnection()
  53. {
  54. return [
  55. ['redis://localhost:1234'],
  56. ['redis://foo@localhost'],
  57. ['redis://localhost/123'],
  58. ];
  59. }
  60. /**
  61. * @dataProvider provideInvalidCreateConnection
  62. * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
  63. * @expectedExceptionMessage Invalid Redis DSN
  64. */
  65. public function testInvalidCreateConnection($dsn)
  66. {
  67. RedisAdapter::createConnection($dsn);
  68. }
  69. public function provideInvalidCreateConnection()
  70. {
  71. return [
  72. ['foo://localhost'],
  73. ['redis://'],
  74. ];
  75. }
  76. }