CacheRedisStoreTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Illuminate\Tests\Cache;
  3. use Illuminate\Cache\RedisStore;
  4. use Illuminate\Contracts\Redis\Factory;
  5. use Mockery as m;
  6. use PHPUnit\Framework\TestCase;
  7. class CacheRedisStoreTest extends TestCase
  8. {
  9. protected function tearDown(): void
  10. {
  11. m::close();
  12. }
  13. public function testGetReturnsNullWhenNotFound()
  14. {
  15. $redis = $this->getRedis();
  16. $redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
  17. $redis->getRedis()->shouldReceive('get')->once()->with('prefix:foo')->andReturn(null);
  18. $this->assertNull($redis->get('foo'));
  19. }
  20. public function testRedisValueIsReturned()
  21. {
  22. $redis = $this->getRedis();
  23. $redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
  24. $redis->getRedis()->shouldReceive('get')->once()->with('prefix:foo')->andReturn(serialize('foo'));
  25. $this->assertSame('foo', $redis->get('foo'));
  26. }
  27. public function testRedisMultipleValuesAreReturned()
  28. {
  29. $redis = $this->getRedis();
  30. $redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
  31. $redis->getRedis()->shouldReceive('mget')->once()->with(['prefix:foo', 'prefix:fizz', 'prefix:norf', 'prefix:null'])
  32. ->andReturn([
  33. serialize('bar'),
  34. serialize('buzz'),
  35. serialize('quz'),
  36. null,
  37. ]);
  38. $results = $redis->many(['foo', 'fizz', 'norf', 'null']);
  39. $this->assertSame('bar', $results['foo']);
  40. $this->assertSame('buzz', $results['fizz']);
  41. $this->assertSame('quz', $results['norf']);
  42. $this->assertNull($results['null']);
  43. }
  44. public function testRedisValueIsReturnedForNumerics()
  45. {
  46. $redis = $this->getRedis();
  47. $redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
  48. $redis->getRedis()->shouldReceive('get')->once()->with('prefix:foo')->andReturn(1);
  49. $this->assertEquals(1, $redis->get('foo'));
  50. }
  51. public function testSetMethodProperlyCallsRedis()
  52. {
  53. $redis = $this->getRedis();
  54. $redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
  55. $redis->getRedis()->shouldReceive('setex')->once()->with('prefix:foo', 60, serialize('foo'))->andReturn('OK');
  56. $result = $redis->put('foo', 'foo', 60);
  57. $this->assertTrue($result);
  58. }
  59. public function testSetMultipleMethodProperlyCallsRedis()
  60. {
  61. $redis = $this->getRedis();
  62. /** @var m\MockInterface $connection */
  63. $connection = $redis->getRedis();
  64. $connection->shouldReceive('connection')->with('default')->andReturn($redis->getRedis());
  65. $connection->shouldReceive('multi')->once();
  66. $redis->getRedis()->shouldReceive('setex')->once()->with('prefix:foo', 60, serialize('bar'))->andReturn('OK');
  67. $redis->getRedis()->shouldReceive('setex')->once()->with('prefix:baz', 60, serialize('qux'))->andReturn('OK');
  68. $redis->getRedis()->shouldReceive('setex')->once()->with('prefix:bar', 60, serialize('norf'))->andReturn('OK');
  69. $connection->shouldReceive('exec')->once();
  70. $result = $redis->putMany([
  71. 'foo' => 'bar',
  72. 'baz' => 'qux',
  73. 'bar' => 'norf',
  74. ], 60);
  75. $this->assertTrue($result);
  76. }
  77. public function testSetMethodProperlyCallsRedisForNumerics()
  78. {
  79. $redis = $this->getRedis();
  80. $redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
  81. $redis->getRedis()->shouldReceive('setex')->once()->with('prefix:foo', 60, 1);
  82. $result = $redis->put('foo', 1, 60);
  83. $this->assertFalse($result);
  84. }
  85. public function testIncrementMethodProperlyCallsRedis()
  86. {
  87. $redis = $this->getRedis();
  88. $redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
  89. $redis->getRedis()->shouldReceive('incrby')->once()->with('prefix:foo', 5);
  90. $redis->increment('foo', 5);
  91. }
  92. public function testDecrementMethodProperlyCallsRedis()
  93. {
  94. $redis = $this->getRedis();
  95. $redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
  96. $redis->getRedis()->shouldReceive('decrby')->once()->with('prefix:foo', 5);
  97. $redis->decrement('foo', 5);
  98. }
  99. public function testStoreItemForeverProperlyCallsRedis()
  100. {
  101. $redis = $this->getRedis();
  102. $redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
  103. $redis->getRedis()->shouldReceive('set')->once()->with('prefix:foo', serialize('foo'))->andReturn('OK');
  104. $result = $redis->forever('foo', 'foo', 60);
  105. $this->assertTrue($result);
  106. }
  107. public function testForgetMethodProperlyCallsRedis()
  108. {
  109. $redis = $this->getRedis();
  110. $redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
  111. $redis->getRedis()->shouldReceive('del')->once()->with('prefix:foo');
  112. $redis->forget('foo');
  113. }
  114. public function testFlushesCached()
  115. {
  116. $redis = $this->getRedis();
  117. $redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
  118. $redis->getRedis()->shouldReceive('flushdb')->once()->andReturn('ok');
  119. $result = $redis->flush();
  120. $this->assertTrue($result);
  121. }
  122. public function testGetAndSetPrefix()
  123. {
  124. $redis = $this->getRedis();
  125. $this->assertSame('prefix:', $redis->getPrefix());
  126. $redis->setPrefix('foo');
  127. $this->assertSame('foo:', $redis->getPrefix());
  128. $redis->setPrefix(null);
  129. $this->assertEmpty($redis->getPrefix());
  130. }
  131. protected function getRedis()
  132. {
  133. return new RedisStore(m::mock(Factory::class), 'prefix');
  134. }
  135. }