RedisManagerExtensionTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Illuminate\Tests\Redis;
  3. use Illuminate\Contracts\Redis\Connector;
  4. use Illuminate\Foundation\Application;
  5. use Illuminate\Redis\RedisManager;
  6. use Mockery as m;
  7. use PHPUnit\Framework\TestCase;
  8. class RedisManagerExtensionTest extends TestCase
  9. {
  10. /**
  11. * @var \Illuminate\Redis\RedisManager
  12. */
  13. protected $redis;
  14. protected function setUp(): void
  15. {
  16. parent::setUp();
  17. $this->redis = new RedisManager(new Application, 'my_custom_driver', [
  18. 'default' => [
  19. 'host' => 'some-host',
  20. 'port' => 'some-port',
  21. 'database' => 5,
  22. 'timeout' => 0.5,
  23. ],
  24. 'clusters' => [
  25. 'my-cluster' => [
  26. [
  27. 'host' => 'some-host',
  28. 'port' => 'some-port',
  29. 'database' => 5,
  30. 'timeout' => 0.5,
  31. ],
  32. ],
  33. ],
  34. ]);
  35. $this->redis->extend('my_custom_driver', function () {
  36. return new FakeRedisConnector;
  37. });
  38. }
  39. protected function tearDown(): void
  40. {
  41. m::close();
  42. }
  43. public function testUsingCustomRedisConnectorWithSingleRedisInstance()
  44. {
  45. $this->assertSame(
  46. 'my-redis-connection', $this->redis->resolve()
  47. );
  48. }
  49. public function testUsingCustomRedisConnectorWithRedisClusterInstance()
  50. {
  51. $this->assertSame(
  52. 'my-redis-cluster-connection', $this->redis->resolve('my-cluster')
  53. );
  54. }
  55. public function test_parse_connection_configuration_for_cluster()
  56. {
  57. $name = 'my-cluster';
  58. $config = [
  59. [
  60. 'url1',
  61. 'url2',
  62. 'url3',
  63. ],
  64. ];
  65. $redis = new RedisManager(new Application, 'my_custom_driver', [
  66. 'clusters' => [
  67. $name => $config,
  68. ],
  69. ]);
  70. $redis->extend('my_custom_driver', function () use ($config) {
  71. return m::mock(Connector::class)
  72. ->shouldReceive('connectToCluster')
  73. ->once()
  74. ->withArgs(function ($configArg) use ($config) {
  75. return $config === $configArg;
  76. })
  77. ->getMock();
  78. });
  79. $redis->resolve($name);
  80. }
  81. }
  82. class FakeRedisConnector implements Connector
  83. {
  84. /**
  85. * Create a new clustered Predis connection.
  86. *
  87. * @param array $config
  88. * @param array $options
  89. * @return \Illuminate\Contracts\Redis\Connection
  90. */
  91. public function connect(array $config, array $options)
  92. {
  93. return 'my-redis-connection';
  94. }
  95. /**
  96. * Create a new clustered Predis connection.
  97. *
  98. * @param array $config
  99. * @param array $clusterOptions
  100. * @param array $options
  101. * @return \Illuminate\Contracts\Redis\Connection
  102. */
  103. public function connectToCluster(array $config, array $clusterOptions, array $options)
  104. {
  105. return 'my-redis-cluster-connection';
  106. }
  107. }