RedisConnectorTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace Illuminate\Tests\Redis;
  3. use Illuminate\Foundation\Application;
  4. use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis;
  5. use Illuminate\Redis\RedisManager;
  6. use PHPUnit\Framework\TestCase;
  7. class RedisConnectorTest extends TestCase
  8. {
  9. use InteractsWithRedis;
  10. protected function setUp(): void
  11. {
  12. parent::setUp();
  13. $this->setUpRedis();
  14. }
  15. protected function tearDown(): void
  16. {
  17. parent::tearDown();
  18. $this->tearDownRedis();
  19. }
  20. public function testDefaultConfiguration()
  21. {
  22. $host = env('REDIS_HOST', '127.0.0.1');
  23. $port = env('REDIS_PORT', 6379);
  24. $predisClient = $this->redis['predis']->connection()->client();
  25. $parameters = $predisClient->getConnection()->getParameters();
  26. $this->assertSame('tcp', $parameters->scheme);
  27. $this->assertEquals($host, $parameters->host);
  28. $this->assertEquals($port, $parameters->port);
  29. $phpRedisClient = $this->redis['phpredis']->connection()->client();
  30. $this->assertEquals($host, $phpRedisClient->getHost());
  31. $this->assertEquals($port, $phpRedisClient->getPort());
  32. $this->assertSame('default', $phpRedisClient->client('GETNAME'));
  33. }
  34. public function testUrl()
  35. {
  36. $host = env('REDIS_HOST', '127.0.0.1');
  37. $port = env('REDIS_PORT', 6379);
  38. $predis = new RedisManager(new Application, 'predis', [
  39. 'cluster' => false,
  40. 'options' => [
  41. 'prefix' => 'test_',
  42. ],
  43. 'default' => [
  44. 'url' => "redis://{$host}:{$port}",
  45. 'database' => 5,
  46. 'timeout' => 0.5,
  47. ],
  48. ]);
  49. $predisClient = $predis->connection()->client();
  50. $parameters = $predisClient->getConnection()->getParameters();
  51. $this->assertSame('tcp', $parameters->scheme);
  52. $this->assertEquals($host, $parameters->host);
  53. $this->assertEquals($port, $parameters->port);
  54. $phpRedis = new RedisManager(new Application, 'phpredis', [
  55. 'cluster' => false,
  56. 'options' => [
  57. 'prefix' => 'test_',
  58. ],
  59. 'default' => [
  60. 'url' => "redis://{$host}:{$port}",
  61. 'database' => 5,
  62. 'timeout' => 0.5,
  63. ],
  64. ]);
  65. $phpRedisClient = $phpRedis->connection()->client();
  66. $this->assertSame("tcp://{$host}", $phpRedisClient->getHost());
  67. $this->assertEquals($port, $phpRedisClient->getPort());
  68. }
  69. public function testUrlWithScheme()
  70. {
  71. $host = env('REDIS_HOST', '127.0.0.1');
  72. $port = env('REDIS_PORT', 6379);
  73. $predis = new RedisManager(new Application, 'predis', [
  74. 'cluster' => false,
  75. 'options' => [
  76. 'prefix' => 'test_',
  77. ],
  78. 'default' => [
  79. 'url' => "tls://{$host}:{$port}",
  80. 'database' => 5,
  81. 'timeout' => 0.5,
  82. ],
  83. ]);
  84. $predisClient = $predis->connection()->client();
  85. $parameters = $predisClient->getConnection()->getParameters();
  86. $this->assertSame('tls', $parameters->scheme);
  87. $this->assertEquals($host, $parameters->host);
  88. $this->assertEquals($port, $parameters->port);
  89. $phpRedis = new RedisManager(new Application, 'phpredis', [
  90. 'cluster' => false,
  91. 'options' => [
  92. 'prefix' => 'test_',
  93. ],
  94. 'default' => [
  95. 'url' => "tcp://{$host}:{$port}",
  96. 'database' => 5,
  97. 'timeout' => 0.5,
  98. ],
  99. ]);
  100. $phpRedisClient = $phpRedis->connection()->client();
  101. $this->assertSame("tcp://{$host}", $phpRedisClient->getHost());
  102. $this->assertEquals($port, $phpRedisClient->getPort());
  103. }
  104. public function testScheme()
  105. {
  106. $host = env('REDIS_HOST', '127.0.0.1');
  107. $port = env('REDIS_PORT', 6379);
  108. $predis = new RedisManager(new Application, 'predis', [
  109. 'cluster' => false,
  110. 'options' => [
  111. 'prefix' => 'test_',
  112. ],
  113. 'default' => [
  114. 'scheme' => 'tls',
  115. 'host' => $host,
  116. 'port' => $port,
  117. 'database' => 5,
  118. 'timeout' => 0.5,
  119. ],
  120. ]);
  121. $predisClient = $predis->connection()->client();
  122. $parameters = $predisClient->getConnection()->getParameters();
  123. $this->assertSame('tls', $parameters->scheme);
  124. $this->assertEquals($host, $parameters->host);
  125. $this->assertEquals($port, $parameters->port);
  126. $phpRedis = new RedisManager(new Application, 'phpredis', [
  127. 'cluster' => false,
  128. 'options' => [
  129. 'prefix' => 'test_',
  130. ],
  131. 'default' => [
  132. 'scheme' => 'tcp',
  133. 'host' => $host,
  134. 'port' => $port,
  135. 'database' => 5,
  136. 'timeout' => 0.5,
  137. ],
  138. ]);
  139. $phpRedisClient = $phpRedis->connection()->client();
  140. $this->assertSame("tcp://{$host}", $phpRedisClient->getHost());
  141. $this->assertEquals($port, $phpRedisClient->getPort());
  142. }
  143. public function testPredisConfigurationWithUsername()
  144. {
  145. $host = env('REDIS_HOST', '127.0.0.1');
  146. $port = env('REDIS_PORT', 6379);
  147. $username = 'testuser';
  148. $password = 'testpw';
  149. $predis = new RedisManager(new Application, 'predis', [
  150. 'default' => [
  151. 'host' => $host,
  152. 'port' => $port,
  153. 'username' => $username,
  154. 'password' => $password,
  155. 'database' => 5,
  156. 'timeout' => 0.5,
  157. ],
  158. ]);
  159. $predisClient = $predis->connection()->client();
  160. $parameters = $predisClient->getConnection()->getParameters();
  161. $this->assertEquals($username, $parameters->username);
  162. $this->assertEquals($password, $parameters->password);
  163. }
  164. public function testPredisConfigurationWithSentinel()
  165. {
  166. $host = env('REDIS_HOST', '127.0.0.1');
  167. $port = env('REDIS_PORT', 6379);
  168. $predis = new RedisManager(new Application, 'predis', [
  169. 'cluster' => false,
  170. 'options' => [
  171. 'replication' => 'sentinel',
  172. 'service' => 'mymaster',
  173. 'parameters' => [
  174. 'default' => [
  175. 'database' => 5,
  176. ],
  177. ],
  178. ],
  179. 'default' => [
  180. "tcp://{$host}:{$port}",
  181. ],
  182. ]);
  183. $predisClient = $predis->connection()->client();
  184. $parameters = $predisClient->getConnection()->getSentinelConnection()->getParameters();
  185. $this->assertEquals($host, $parameters->host);
  186. }
  187. }