Initial commit cache

This commit is contained in:
2018-02-28 15:01:45 +01:00
commit 01a13b7f2b
20 changed files with 1117 additions and 0 deletions

View File

@@ -0,0 +1,121 @@
<?php
declare(strict_types=1);
namespace IQParts\CacheTest\Unit\Adapter;
use IQParts\Cache\Adapter\FilesystemAdapter;
use IQParts\Cache\Serializer\JsonSerializer;
use IQParts\CacheTest\AbstractTestCase;
final class FilesystemAdapterTest extends AbstractTestCase
{
public function testGetSet()
{
$location = $this->getTmpDirectory();
$adapter = new FilesystemAdapter(
$location,
new JsonSerializer(),
0777
);
$adapter->set('a', 'b');
$this->assertEquals('b', $adapter->get('a'));
$adapter->delete('a');
$this->assertEquals(null, $adapter->get('a'));
$adapter->set('ttl', 'b', 200);
$this->assertTrue($adapter->ttl('ttl') > 0);
$this->assertTrue($adapter->ttl('b') === $adapter::NO_TTL);
$adapter->set('a-keys', 'a');
$adapter->set('b-keys', 'a');
$this->assertEquals(['a-keys'], $adapter->keys('a*'));
$adapter->delete('b');
$adapter->delete('a-keys');
$adapter->delete('b-keys');
}
public function testTtl()
{
$location = $this->getTmpDirectory();
$adapter = new FilesystemAdapter(
$location,
new JsonSerializer(),
0777
);
$adapter->set('a', 'b', 10);
$this->assertEquals('b', $adapter->get('a'));
$this->assertTrue($adapter->ttl('a') > 0);
$adapter->set('b', 'a', -10);
$this->assertNull($adapter->get('b'));
$adapter->delete('b');
$adapter->delete('a');
}
public function testDefaultTtl()
{
$location = $this->getTmpDirectory();
$adapter = new FilesystemAdapter(
$location,
new JsonSerializer(),
0777,
10
);
$adapter->set('a', 'b');
$this->assertTrue($adapter->ttl('a') > 0);
$adapter->delete('a');
}
public function testDelete()
{
$location = $this->getTmpDirectory();
$adapter = new FilesystemAdapter(
$location,
new JsonSerializer(),
0777
);
$adapter->set('del-a', 'a');
$adapter->set('del-b', 'b');
$adapter->delete('del*');
$this->assertNull($adapter->get('del-a'));
$this->assertNull($adapter->get('del-b'));
}
public function testSubFolder()
{
$location = $this->getTmpDirectory();
$adapter = new FilesystemAdapter(
$location,
new JsonSerializer(),
0777
);
$adapter->set('a/b', 'a');
$this->assertEquals('a', $adapter->get('a/b'));
$adapter->delete('a/b');
}
public function testSubFolderWithSeperator()
{
$location = $this->getTmpDirectory();
$adapter = new FilesystemAdapter(
$location,
new JsonSerializer(),
0777,
null,
'/'
);
$adapter->set('a/b', 'a');
$this->assertEquals('a', $adapter->get('a/b'));
$adapter->delete('a/b');
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace IQParts\CacheTest\Unit\Adapter;
use IQParts\Cache\Adapter\MemoryAdapter;
use IQParts\CacheTest\AbstractTestCase;
final class MemoryAdapterTest extends AbstractTestCase
{
public function testAdapter()
{
$adapter = new MemoryAdapter();
$this->assertEquals(null, $adapter->get('a'));
$adapter->set('a', 'b');
$this->assertEquals('b', $adapter->get('a'));
$adapter->delete('a');
$this->assertEquals(null, $adapter->get('a'));
$adapter->set('a-keys', 'a');
$adapter->set('b-keys', 'a');
$this->assertEquals(['a-keys'], $adapter->keys('a*'));
$adapter->set('a', 'b', 200);
$this->assertTrue($adapter->ttl('a') > 0);
$this->assertTrue($adapter->ttl('c') === MemoryAdapter::NO_TTL);
}
public function testTtl()
{
$adapter = new MemoryAdapter();
$adapter->set('a', 'b', 10);
$this->assertEquals('b', $adapter->get('a'));
$this->assertTrue($adapter->ttl('a') > 0);
$adapter->set('b', 'a', -10);
$this->assertNull($adapter->get('b'));
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace IQParts\CacheTest\Unit\Adapter;
use IQParts\Cache\Adapter\MemoryAdapter;
use IQParts\CacheTest\AbstractTestCase;
use IQParts\Cache\Adapter\CacheAdapterInterface;
use IQParts\Cache\Adapter\NamespaceAdapter;
use PhpParser\Node\Name;
final class NamespaceAdapterTest extends AbstractTestCase
{
/**
* @var CacheAdapterInterface
*/
private $adapter;
public function setUp()
{
$this->adapter = new MemoryAdapter();
}
public function testGet()
{
$adapter = new NamespaceAdapter('test', $this->adapter);
$adapter->set('mykey', 'a');
$this->assertEquals('test:mykey', $this->adapter->keys()[0]);
$this->assertEquals('a', $adapter->get('mykey'));
}
public function testSet()
{
$adapter = new NamespaceAdapter('test', $this->adapter);
$adapter->set('mykey', null);
$this->assertEquals('test:mykey', $this->adapter->keys()[0]);
}
public function testDelete()
{
$adapter = new NamespaceAdapter('test', $this->adapter);
$adapter->delete('mykey');
$this->assertEquals(null, $this->adapter->get('mykey'));
}
public function testKeys()
{
$adapter = new NamespaceAdapter('test', $this->adapter);
$adapter->set('a-keys', 'a');
$adapter->set('b-keys', 'b');
$this->assertEquals(['a-keys'], $adapter->keys('a-*'));
$this->assertEquals(['a-keys'], $adapter->keys('a-'));
}
public function testTtl()
{
$adapter = new NamespaceAdapter('test', $this->adapter);
$adapter->set('a', 'b', 10);
$this->assertTrue($adapter->ttl('a') > 0);
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace IQParts\CacheTest\Unit\Adapter;
use IQParts\CacheTest\AbstractTestCase;
use IQParts\Cache\Adapter\NullAdapter;
final class NullAdapterTest extends AbstractTestCase
{
public function testNullAdapter()
{
$adapter = new NullAdapter();
$adapter->set('a', 'b');
$this->assertEquals(null, $adapter->get('a'));
$adapter->delete('a');
$this->assertEquals([], $adapter->keys());
$this->assertEquals(0, $adapter->ttl('a'));
}
}

View 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*'));
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace IQParts\CacheTest\Unit\Serializer;
use IQParts\CacheTest\AbstractTestCase;
use IQParts\Cache\Serializer\JsonSerializer;
final class JsonSerializerTest extends AbstractTestCase
{
public function testSerialize()
{
$serializer = new JsonSerializer();
$string = "myString";
$array = [0 => '1', 1 => '2', 2 => '3'];
$this->assertTrue(is_string($serializer->serialize($array)));
$this->assertEquals($string, $serializer->serialize($string));
$this->assertEquals($array, $serializer->deserialize($serializer->serialize($array)));
}
}

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace IQParts\CacheTest\Unit\Serializer;
use IQParts\Cache\Serializer\NoSerializer;
use IQParts\CacheTest\AbstractTestCase;
final class NoSerializerTest extends AbstractTestCase
{
public function testSerialize()
{
$serializer = new NoSerializer();
$this->assertEquals('a', $serializer->serialize('a'));
}
public function testDeserialize()
{
$serializer = new NoSerializer();
$this->assertEquals('a', $serializer->deserialize('a'));
}
}