Initial commit cache
This commit is contained in:
114
test/Unit/Adapter/PredisAdapterTest.php
Normal file
114
test/Unit/Adapter/PredisAdapterTest.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace IQParts\CacheTest\Unit\Adapter;
|
||||
|
||||
use IQParts\Cache\Adapter\PredisAdapter;
|
||||
use IQParts\Cache\Serializer\JsonSerializer;
|
||||
use IQParts\CacheTest\AbstractTestCase;
|
||||
use Predis\ClientInterface;
|
||||
|
||||
final class PredisAdapterTest extends AbstractTestCase
|
||||
{
|
||||
public function testGet()
|
||||
{
|
||||
$client = $this->createMock(ClientInterface::class);
|
||||
$client
|
||||
->expects($this->at(0))
|
||||
->method('__call')
|
||||
->with('get', ['a'])
|
||||
->willReturn(null);
|
||||
|
||||
/** @var ClientInterface $client */
|
||||
$cache = new PredisAdapter($client, new JsonSerializer());
|
||||
$this->assertNull($cache->get('a'));
|
||||
}
|
||||
|
||||
public function testSet()
|
||||
{
|
||||
$client = $this->createMock(ClientInterface::class);
|
||||
$client
|
||||
->expects($this->at(0))
|
||||
->method('__call')
|
||||
->with('set', ['a', 'b']);
|
||||
$client
|
||||
->expects($this->at(1))
|
||||
->method('__call')
|
||||
->with('get', ['a'])
|
||||
->willReturn('b');
|
||||
|
||||
/** @var ClientInterface $client */
|
||||
$cache = new PredisAdapter($client, new JsonSerializer());
|
||||
$cache->set('a', 'b');
|
||||
$this->assertEquals('b', $cache->get('a'));
|
||||
}
|
||||
|
||||
public function testDelete()
|
||||
{
|
||||
$client = $this->createMock(ClientInterface::class);
|
||||
$client
|
||||
->expects($this->at(0))
|
||||
->method('__call')
|
||||
->with('set', ['a', 'b']);
|
||||
$client
|
||||
->expects($this->at(1))
|
||||
->method('__call')
|
||||
->with('del', [['a']])
|
||||
->willReturn(1);
|
||||
$client
|
||||
->expects($this->at(2))
|
||||
->method('__call')
|
||||
->with('get', ['a'])
|
||||
->willReturn(null);
|
||||
|
||||
/** @var ClientInterface $client */
|
||||
$cache = new PredisAdapter($client, new JsonSerializer());
|
||||
$cache->set('a', 'b');
|
||||
$cache->delete('a');
|
||||
$this->assertNull($cache->get('a'));
|
||||
}
|
||||
|
||||
public function testTtl()
|
||||
{
|
||||
$client = $this->createMock(ClientInterface::class);
|
||||
$client
|
||||
->expects($this->at(0))
|
||||
->method('__call')
|
||||
->with('setex', ['a', 200, 'b']);
|
||||
$client
|
||||
->expects($this->at(1))
|
||||
->method('__call')
|
||||
->with('ttl', ['a'])
|
||||
->willReturn(200);
|
||||
|
||||
/** @var ClientInterface $client */
|
||||
$cache = new PredisAdapter($client, new JsonSerializer());
|
||||
$cache->set('a', 'b', 200);
|
||||
$this->assertEquals(200, $cache->ttl('a'));
|
||||
}
|
||||
|
||||
public function testKeys()
|
||||
{
|
||||
$client = $this->createMock(ClientInterface::class);
|
||||
$client
|
||||
->expects($this->at(0))
|
||||
->method('__call')
|
||||
->with('set', ['a-keys', '1']);
|
||||
$client
|
||||
->expects($this->at(1))
|
||||
->method('__call')
|
||||
->with('set', ['b-keys', '1']);
|
||||
$client
|
||||
->expects($this->at(2))
|
||||
->method('__call')
|
||||
->with('keys', ['a*'])
|
||||
->willReturn(['a-keys']);
|
||||
|
||||
/** @var ClientInterface $client */
|
||||
$cache = new PredisAdapter($client, new JsonSerializer());
|
||||
$cache->set('a-keys', '1');
|
||||
$cache->set('b-keys', '1');
|
||||
$this->assertEquals(['a-keys'], $cache->keys('a*'));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user