More test coverage:
This commit is contained in:
parent
03bf558d62
commit
3728a1f4f4
@ -31,10 +31,12 @@ class MemcacheDriver extends AbstractDriver {
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
// @codeCoverageIgnoreStart
|
||||
if ( ! class_exists('Memcache'))
|
||||
{
|
||||
throw new CacheException('Memcache driver requires the PHP memcache extension');
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
$this->conn = new \Memcache();
|
||||
|
||||
@ -102,7 +104,7 @@ class MemcacheDriver extends AbstractDriver {
|
||||
{
|
||||
$this->conn->set($key, $value, 0, $expires);
|
||||
}
|
||||
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -141,7 +143,7 @@ class MemcacheDriver extends AbstractDriver {
|
||||
{
|
||||
return $this->conn->flush();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the specified key to expire at the given time
|
||||
*
|
||||
@ -153,7 +155,7 @@ class MemcacheDriver extends AbstractDriver {
|
||||
{
|
||||
$value = $this->get($key);
|
||||
$timediff = $expires - time();
|
||||
|
||||
|
||||
return $this->set($key, $value, $timediff);
|
||||
}
|
||||
}
|
@ -34,10 +34,12 @@ class MemcachedDriver extends AbstractDriver {
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
// @codeCoverageIgnoreStart
|
||||
if ( ! class_exists('Memcached'))
|
||||
{
|
||||
throw new CacheException("Memcached driver requires memcached extensions");
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
try
|
||||
{
|
||||
@ -45,12 +47,14 @@ class MemcachedDriver extends AbstractDriver {
|
||||
$this->conn->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
|
||||
$this->conn->addServer($config['host'], $config['port']);
|
||||
}
|
||||
// @codeCoverageIgnoreStart
|
||||
catch (MemcachedException $e)
|
||||
{
|
||||
// Rewrite MemcachedException as a CacheException to
|
||||
// Rewrite MemcachedException as a CacheException to
|
||||
// match the requirements of the interface
|
||||
throw new CacheException($e->getMessage(), $e->getCode(), $e);
|
||||
throw new CacheException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
/**
|
||||
@ -147,7 +151,7 @@ class MemcachedDriver extends AbstractDriver {
|
||||
{
|
||||
return $this->conn->flush();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the specified key to expire at the given time
|
||||
*
|
||||
@ -161,9 +165,9 @@ class MemcachedDriver extends AbstractDriver {
|
||||
{
|
||||
return $this->conn->touch($key, $expires);
|
||||
}
|
||||
|
||||
|
||||
$this->getLogger()->warn("Tried to set expiration on a key that does not exist");
|
||||
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
@ -39,10 +39,12 @@ class RedisDriver extends AbstractDriver {
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
// @codeCoverageIgnoreStart
|
||||
if ( ! class_exists('Predis\\Client'))
|
||||
{
|
||||
throw new CacheException("The redis driver requires the predis/predis composer package to be installed.");
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
$this->conn = new Client($config);
|
||||
}
|
||||
@ -107,16 +109,16 @@ class RedisDriver extends AbstractDriver {
|
||||
public function set($key, $value, $expires = 0)
|
||||
{
|
||||
$value = \serialize($value);
|
||||
|
||||
|
||||
if ($expires !== 0)
|
||||
{
|
||||
$this->conn->set($key, $value, "EX {$expires}");
|
||||
$this->conn->set($key, $value, "EX", $expires);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->conn->set($key, $value);
|
||||
}
|
||||
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
28
src/Item.php
28
src/Item.php
@ -104,7 +104,7 @@ class Item implements CacheItemInterface {
|
||||
{
|
||||
return $this->driver->get($this->key);
|
||||
}
|
||||
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -155,13 +155,13 @@ class Item implements CacheItemInterface {
|
||||
*/
|
||||
public function expiresAt($expiration = NULL)
|
||||
{
|
||||
if ($time instanceof \DateTimeInterface)
|
||||
if ($expiration instanceof \DateTimeInterface)
|
||||
{
|
||||
$time = $time->getTimestamp();
|
||||
$expiration = $expiration->getTimestamp();
|
||||
}
|
||||
|
||||
$this->expiresAt = (int) $time;
|
||||
|
||||
|
||||
$this->expiresAt = (int) $expiration;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -178,19 +178,19 @@ class Item implements CacheItemInterface {
|
||||
* @return static
|
||||
* The called object.
|
||||
*/
|
||||
public function expiresAfter($time = 0)
|
||||
public function expiresAfter($time = NULL)
|
||||
{
|
||||
if ($time instanceof \DateInterval)
|
||||
{
|
||||
$time = $time->format("%s");
|
||||
}
|
||||
|
||||
|
||||
$this->ttl = (int) $time;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save the current value to the cache
|
||||
* Save the current value to the cache
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@ -200,14 +200,14 @@ class Item implements CacheItemInterface {
|
||||
{
|
||||
$setResult = $this->driver->set($this->key, $this->value);
|
||||
$expResult = $this->driver->expiresAt($this->key, $this->expiresAt);
|
||||
|
||||
|
||||
return $setResult && $expResult;
|
||||
}
|
||||
else if ($this->ttl !== NULL && $this->ttl !== 0)
|
||||
{
|
||||
return $this->driver->set($this->key, $this->value, $this->ttl);
|
||||
return (bool) $this->driver->set($this->key, $this->value, $this->ttl);
|
||||
}
|
||||
|
||||
return $this->driver->set($this->key, $this->value);
|
||||
|
||||
return (bool) $this->driver->set($this->key, $this->value);
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@ use PHPUnit\Framework\TestCase;
|
||||
class DriverTestBase extends TestCase {
|
||||
|
||||
protected $driver;
|
||||
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
$this->driver->__destruct();
|
||||
@ -28,4 +28,37 @@ class DriverTestBase extends TestCase {
|
||||
$this->driver->set('bar', $bar);
|
||||
$this->assertEquals($bar, $this->driver->get('bar'));
|
||||
}
|
||||
|
||||
public function testGetMultiple()
|
||||
{
|
||||
$this->driver->set('foo', ['bar']);
|
||||
$this->driver->set('bar', (object) [
|
||||
'foo' => [
|
||||
'bar' => 'baz'
|
||||
]
|
||||
]);
|
||||
|
||||
// Intentionally set the same key with different values
|
||||
$this->driver->set('baz', 34);
|
||||
$this->driver->set('baz', 42);
|
||||
|
||||
$expected = [
|
||||
'foo' => ['bar'],
|
||||
'bar' => (object) [
|
||||
'foo' => [
|
||||
'bar' => 'baz'
|
||||
]
|
||||
],
|
||||
];
|
||||
|
||||
$actual = $this->driver->getMultiple(['foo', 'bar']);
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
public function testSetWithExpires()
|
||||
{
|
||||
$this->driver->set('foo', 'bar', 30);
|
||||
$this->assertEquals('bar', $this->driver->get('foo'));
|
||||
}
|
||||
}
|
@ -3,37 +3,108 @@
|
||||
namespace Aviat\Banker\Tests;
|
||||
|
||||
use Aviat\Banker\Item;
|
||||
use Aviat\Banker\ItemCollection;
|
||||
use Aviat\Banker\Driver\NullDriver;
|
||||
use Aviat\Banker\Exception\InvalidArgumentException;
|
||||
use Aviat\Banker\Tests\Friend;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use DateTime;
|
||||
use DateInterval;
|
||||
|
||||
class ItemTest extends TestCase {
|
||||
|
||||
|
||||
protected $key = 'foo';
|
||||
protected $item;
|
||||
protected $driver;
|
||||
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->driver = new NullDriver();
|
||||
$this->item = new Item($this->driver, $this->key);
|
||||
}
|
||||
|
||||
|
||||
public function testGetKey()
|
||||
{
|
||||
$this->assertEquals($this->key, $this->item->getKey());
|
||||
}
|
||||
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
// No value set yet
|
||||
$this->assertNull($this->item->get());
|
||||
|
||||
|
||||
// Set a value
|
||||
$this->item->set('bar')
|
||||
->save();
|
||||
|
||||
|
||||
$this->assertEquals('bar', $this->item->get());
|
||||
}
|
||||
|
||||
public function testExpiresAt()
|
||||
{
|
||||
$time = new DateTime("Next Tuesday");
|
||||
$expected = $time->getTimestamp();
|
||||
$this->item->expiresAt($time);
|
||||
$friend = new Friend($this->item);
|
||||
$this->assertEquals($expected, $friend->expiresAt, "DateTimeInterface");
|
||||
|
||||
$time2 = strtotime("July 16, 2024");
|
||||
$this->item->expiresAt($time2);
|
||||
$friend2 = new Friend($this->item);
|
||||
$this->assertEquals($time2, $friend2->expiresAt, "Unix Timestamp");
|
||||
}
|
||||
|
||||
public function testExpiresAfter()
|
||||
{
|
||||
$interval = new DateInterval('P5W');
|
||||
$expected = (int) $interval->format("%s");
|
||||
$this->item->expiresAfter($interval);
|
||||
$friend = new Friend($this->item);
|
||||
$this->assertEquals($expected, $friend->ttl);
|
||||
|
||||
$interval2 = 500;
|
||||
$this->item->expiresAfter($interval2);
|
||||
$friend2 = new Friend($this->item);
|
||||
$this->assertEquals($interval2, $friend2->ttl);
|
||||
}
|
||||
|
||||
public function testSaveWithExpiresAt()
|
||||
{
|
||||
$this->setUp();
|
||||
|
||||
$expires = new DateTime("6 weeks");
|
||||
$this->item->expiresAt($expires);
|
||||
|
||||
$this->item->set('barbazfoo');
|
||||
|
||||
$result = $this->item->save();
|
||||
|
||||
$this->assertTrue($result);
|
||||
$this->assertTrue($this->item->isHit());
|
||||
$this->assertEquals('barbazfoo', $this->item->get());
|
||||
}
|
||||
|
||||
public function testSaveWithExpiresAfter()
|
||||
{
|
||||
$this->setUp();
|
||||
|
||||
$interval = new DateInterval('P2D');
|
||||
$expected = $interval->format("%s");
|
||||
$this->item->expiresAfter($interval);
|
||||
$friend = new Friend($this->item);
|
||||
$this->assertEquals($expected, $friend->ttl);
|
||||
|
||||
$expected = [
|
||||
'foo' => [
|
||||
'bar' => []
|
||||
],
|
||||
'baz' => (object) []
|
||||
];
|
||||
$this->item->set($expected);
|
||||
|
||||
$result = $this->item->save();
|
||||
|
||||
$this->assertTrue($result);
|
||||
$this->assertTrue($this->item->isHit());
|
||||
$this->assertEquals($expected, $this->item->get());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user