2013-01-10 15:28:59 -06:00
|
|
|
<?php
|
|
|
|
|
|
2017-01-17 14:30:19 +00:00
|
|
|
namespace Illuminate\Tests\Cache;
|
|
|
|
|
|
2018-09-30 23:03:32 +03:00
|
|
|
use Illuminate\Cache\RedisStore;
|
|
|
|
|
use Illuminate\Contracts\Redis\Factory;
|
2019-09-10 17:16:05 +02:00
|
|
|
use Mockery as m;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2013-01-10 15:28:59 -06:00
|
|
|
|
2016-12-30 21:31:11 +01:00
|
|
|
class CacheRedisStoreTest extends TestCase
|
2015-06-01 16:26:53 +01:00
|
|
|
{
|
2015-06-01 15:56:31 +01:00
|
|
|
public function testGetReturnsNullWhenNotFound()
|
|
|
|
|
{
|
|
|
|
|
$redis = $this->getRedis();
|
|
|
|
|
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
|
|
|
|
|
$redis->getRedis()->shouldReceive('get')->once()->with('prefix:foo')->andReturn(null);
|
|
|
|
|
$this->assertNull($redis->get('foo'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRedisValueIsReturned()
|
|
|
|
|
{
|
|
|
|
|
$redis = $this->getRedis();
|
|
|
|
|
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
|
|
|
|
|
$redis->getRedis()->shouldReceive('get')->once()->with('prefix:foo')->andReturn(serialize('foo'));
|
2019-08-27 14:48:17 +02:00
|
|
|
$this->assertSame('foo', $redis->get('foo'));
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
|
|
|
|
|
2015-11-27 14:44:47 +00:00
|
|
|
public function testRedisMultipleValuesAreReturned()
|
|
|
|
|
{
|
|
|
|
|
$redis = $this->getRedis();
|
|
|
|
|
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
|
2017-04-28 17:12:39 +02:00
|
|
|
$redis->getRedis()->shouldReceive('mget')->once()->with(['prefix:foo', 'prefix:fizz', 'prefix:norf', 'prefix:null'])
|
2015-11-27 14:44:47 +00:00
|
|
|
->andReturn([
|
|
|
|
|
serialize('bar'),
|
|
|
|
|
serialize('buzz'),
|
|
|
|
|
serialize('quz'),
|
2017-04-28 17:12:39 +02:00
|
|
|
null,
|
2015-11-27 14:44:47 +00:00
|
|
|
]);
|
2017-04-28 17:12:39 +02:00
|
|
|
|
|
|
|
|
$results = $redis->many(['foo', 'fizz', 'norf', 'null']);
|
|
|
|
|
|
2019-08-27 14:48:17 +02:00
|
|
|
$this->assertSame('bar', $results['foo']);
|
|
|
|
|
$this->assertSame('buzz', $results['fizz']);
|
|
|
|
|
$this->assertSame('quz', $results['norf']);
|
2017-04-28 17:12:39 +02:00
|
|
|
$this->assertNull($results['null']);
|
2015-11-27 14:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-01 15:56:31 +01:00
|
|
|
public function testRedisValueIsReturnedForNumerics()
|
|
|
|
|
{
|
|
|
|
|
$redis = $this->getRedis();
|
|
|
|
|
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
|
|
|
|
|
$redis->getRedis()->shouldReceive('get')->once()->with('prefix:foo')->andReturn(1);
|
|
|
|
|
$this->assertEquals(1, $redis->get('foo'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSetMethodProperlyCallsRedis()
|
|
|
|
|
{
|
|
|
|
|
$redis = $this->getRedis();
|
|
|
|
|
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
|
2019-01-22 17:10:50 +01:00
|
|
|
$redis->getRedis()->shouldReceive('setex')->once()->with('prefix:foo', 60, serialize('foo'))->andReturn('OK');
|
2018-12-01 20:30:44 +01:00
|
|
|
$result = $redis->put('foo', 'foo', 60);
|
|
|
|
|
$this->assertTrue($result);
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
|
|
|
|
|
2015-11-27 14:44:47 +00:00
|
|
|
public function testSetMultipleMethodProperlyCallsRedis()
|
|
|
|
|
{
|
|
|
|
|
$redis = $this->getRedis();
|
|
|
|
|
/** @var m\MockInterface $connection */
|
|
|
|
|
$connection = $redis->getRedis();
|
|
|
|
|
$connection->shouldReceive('connection')->with('default')->andReturn($redis->getRedis());
|
|
|
|
|
$connection->shouldReceive('multi')->once();
|
2019-01-22 17:10:50 +01:00
|
|
|
$redis->getRedis()->shouldReceive('setex')->once()->with('prefix:foo', 60, serialize('bar'))->andReturn('OK');
|
|
|
|
|
$redis->getRedis()->shouldReceive('setex')->once()->with('prefix:baz', 60, serialize('qux'))->andReturn('OK');
|
|
|
|
|
$redis->getRedis()->shouldReceive('setex')->once()->with('prefix:bar', 60, serialize('norf'))->andReturn('OK');
|
2015-11-27 14:44:47 +00:00
|
|
|
$connection->shouldReceive('exec')->once();
|
|
|
|
|
|
2018-12-01 20:30:44 +01:00
|
|
|
$result = $redis->putMany([
|
2021-07-05 23:30:07 +08:00
|
|
|
'foo' => 'bar',
|
|
|
|
|
'baz' => 'qux',
|
2015-11-27 14:44:47 +00:00
|
|
|
'bar' => 'norf',
|
|
|
|
|
], 60);
|
2018-12-01 20:30:44 +01:00
|
|
|
$this->assertTrue($result);
|
2015-11-27 14:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-01 15:56:31 +01:00
|
|
|
public function testSetMethodProperlyCallsRedisForNumerics()
|
|
|
|
|
{
|
|
|
|
|
$redis = $this->getRedis();
|
|
|
|
|
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
|
2019-01-22 17:10:50 +01:00
|
|
|
$redis->getRedis()->shouldReceive('setex')->once()->with('prefix:foo', 60, 1);
|
2018-12-01 20:30:44 +01:00
|
|
|
$result = $redis->put('foo', 1, 60);
|
|
|
|
|
$this->assertFalse($result);
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testIncrementMethodProperlyCallsRedis()
|
|
|
|
|
{
|
|
|
|
|
$redis = $this->getRedis();
|
|
|
|
|
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
|
|
|
|
|
$redis->getRedis()->shouldReceive('incrby')->once()->with('prefix:foo', 5);
|
|
|
|
|
$redis->increment('foo', 5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDecrementMethodProperlyCallsRedis()
|
|
|
|
|
{
|
|
|
|
|
$redis = $this->getRedis();
|
|
|
|
|
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
|
|
|
|
|
$redis->getRedis()->shouldReceive('decrby')->once()->with('prefix:foo', 5);
|
|
|
|
|
$redis->decrement('foo', 5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testStoreItemForeverProperlyCallsRedis()
|
|
|
|
|
{
|
|
|
|
|
$redis = $this->getRedis();
|
|
|
|
|
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
|
2018-12-01 20:30:44 +01:00
|
|
|
$redis->getRedis()->shouldReceive('set')->once()->with('prefix:foo', serialize('foo'))->andReturn('OK');
|
|
|
|
|
$result = $redis->forever('foo', 'foo', 60);
|
|
|
|
|
$this->assertTrue($result);
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-12 13:59:56 -04:00
|
|
|
public function testTouchMethodProperlyCallsRedis(): void
|
|
|
|
|
{
|
|
|
|
|
$key = 'key';
|
|
|
|
|
$ttl = 60;
|
|
|
|
|
|
|
|
|
|
$redis = $this->getRedis();
|
|
|
|
|
|
|
|
|
|
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
|
|
|
|
|
$redis->getRedis()->shouldReceive('expire')->once()->with("prefix:$key", $ttl)->andReturn(true);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($redis->touch($key, $ttl));
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 15:56:31 +01:00
|
|
|
public function testForgetMethodProperlyCallsRedis()
|
|
|
|
|
{
|
|
|
|
|
$redis = $this->getRedis();
|
|
|
|
|
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
|
|
|
|
|
$redis->getRedis()->shouldReceive('del')->once()->with('prefix:foo');
|
|
|
|
|
$redis->forget('foo');
|
|
|
|
|
}
|
|
|
|
|
|
Change cache flush return type
Change flush return type
If a flush operation fails, then you now have chance of knowing about it.
Change flush method, return false on failure
Change flush test
Ensuring that flush method does return false, should the delete
directories return false.
Change wrapper flush return type
Both apcu_clear_cache() and apc_clear_cache() return bool values, which
can indicate if flush operation fails.
Change flush method, return bool
Add Apc flush test
Change flush method return type
Change flush test, check return of flush method
Change flush method return type
The table delete() returns the number of affected rows. If the number is
higher that zero, then the operation has succeeded. If not, it means that
there was nothing to delete... Not too sure about this one, however, it
should be the most correct response, if you do expect something to be
deleted and zero is returned.
Change flush test, check flush return
Change flush method return type
Memcached's flush method also returns true.
Add flush test
Change flush method return type
Change flush method return type
Not sure about flushdb() return. However, from what I understand, it never
fails and thus should return something like "ok" back.
Add flush test
Add release note about cache flush change
Revert "Ignore PHPStorm IDE file"
This reverts commit 8176c328d5316e8508ad1c382b20841c2699b95e.
2016-10-09 10:40:47 +02:00
|
|
|
public function testFlushesCached()
|
|
|
|
|
{
|
|
|
|
|
$redis = $this->getRedis();
|
|
|
|
|
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
|
|
|
|
|
$redis->getRedis()->shouldReceive('flushdb')->once()->andReturn('ok');
|
|
|
|
|
$result = $redis->flush();
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 23:27:43 +03:30
|
|
|
public function testFlushesCachedLocks()
|
|
|
|
|
{
|
|
|
|
|
$redis = $this->getRedis();
|
|
|
|
|
$redis->getRedis()->shouldReceive('connection')->once()->with('locks')->andReturn($redis->getRedis());
|
|
|
|
|
$redis->getRedis()->shouldReceive('flushdb')->once()->andReturn('ok');
|
|
|
|
|
$redis->setLockConnection('locks');
|
|
|
|
|
$result = $redis->flushLocks();
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-02 11:21:51 -03:00
|
|
|
public function testGetAndSetPrefix()
|
|
|
|
|
{
|
|
|
|
|
$redis = $this->getRedis();
|
2019-08-27 14:48:17 +02:00
|
|
|
$this->assertSame('prefix:', $redis->getPrefix());
|
2015-08-02 11:21:51 -03:00
|
|
|
$redis->setPrefix('foo');
|
2023-06-08 14:42:04 -05:00
|
|
|
$this->assertSame('foo', $redis->getPrefix());
|
2015-08-02 20:33:45 -03:00
|
|
|
$redis->setPrefix(null);
|
2015-09-25 16:08:19 +02:00
|
|
|
$this->assertEmpty($redis->getPrefix());
|
2015-08-02 11:21:51 -03:00
|
|
|
}
|
|
|
|
|
|
2015-06-01 15:56:31 +01:00
|
|
|
protected function getRedis()
|
|
|
|
|
{
|
2023-06-08 14:42:04 -05:00
|
|
|
return new RedisStore(m::mock(Factory::class), 'prefix:');
|
2015-06-01 15:56:31 +01:00
|
|
|
}
|
2014-02-27 19:30:59 +00:00
|
|
|
}
|