More test coverage:

This commit is contained in:
Timothy Warren 2016-09-06 20:26:28 -04:00
parent 03bf558d62
commit 3728a1f4f4
6 changed files with 146 additions and 34 deletions

View File

@ -31,10 +31,12 @@ class MemcacheDriver extends AbstractDriver {
*/ */
public function __construct(array $config = []) public function __construct(array $config = [])
{ {
// @codeCoverageIgnoreStart
if ( ! class_exists('Memcache')) if ( ! class_exists('Memcache'))
{ {
throw new CacheException('Memcache driver requires the PHP memcache extension'); throw new CacheException('Memcache driver requires the PHP memcache extension');
} }
// @codeCoverageIgnoreEnd
$this->conn = new \Memcache(); $this->conn = new \Memcache();
@ -102,7 +104,7 @@ class MemcacheDriver extends AbstractDriver {
{ {
$this->conn->set($key, $value, 0, $expires); $this->conn->set($key, $value, 0, $expires);
} }
return $this; return $this;
} }
@ -141,7 +143,7 @@ class MemcacheDriver extends AbstractDriver {
{ {
return $this->conn->flush(); return $this->conn->flush();
} }
/** /**
* Set the specified key to expire at the given time * Set the specified key to expire at the given time
* *
@ -153,7 +155,7 @@ class MemcacheDriver extends AbstractDriver {
{ {
$value = $this->get($key); $value = $this->get($key);
$timediff = $expires - time(); $timediff = $expires - time();
return $this->set($key, $value, $timediff); return $this->set($key, $value, $timediff);
} }
} }

View File

@ -34,10 +34,12 @@ class MemcachedDriver extends AbstractDriver {
*/ */
public function __construct(array $config = []) public function __construct(array $config = [])
{ {
// @codeCoverageIgnoreStart
if ( ! class_exists('Memcached')) if ( ! class_exists('Memcached'))
{ {
throw new CacheException("Memcached driver requires memcached extensions"); throw new CacheException("Memcached driver requires memcached extensions");
} }
// @codeCoverageIgnoreEnd
try try
{ {
@ -45,12 +47,14 @@ class MemcachedDriver extends AbstractDriver {
$this->conn->setOption(Memcached::OPT_BINARY_PROTOCOL, true); $this->conn->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$this->conn->addServer($config['host'], $config['port']); $this->conn->addServer($config['host'], $config['port']);
} }
// @codeCoverageIgnoreStart
catch (MemcachedException $e) catch (MemcachedException $e)
{ {
// Rewrite MemcachedException as a CacheException to // Rewrite MemcachedException as a CacheException to
// match the requirements of the interface // 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(); return $this->conn->flush();
} }
/** /**
* Set the specified key to expire at the given time * Set the specified key to expire at the given time
* *
@ -161,9 +165,9 @@ class MemcachedDriver extends AbstractDriver {
{ {
return $this->conn->touch($key, $expires); return $this->conn->touch($key, $expires);
} }
$this->getLogger()->warn("Tried to set expiration on a key that does not exist"); $this->getLogger()->warn("Tried to set expiration on a key that does not exist");
return FALSE; return FALSE;
} }
} }

View File

@ -39,10 +39,12 @@ class RedisDriver extends AbstractDriver {
*/ */
public function __construct(array $config = []) public function __construct(array $config = [])
{ {
// @codeCoverageIgnoreStart
if ( ! class_exists('Predis\\Client')) if ( ! class_exists('Predis\\Client'))
{ {
throw new CacheException("The redis driver requires the predis/predis composer package to be installed."); throw new CacheException("The redis driver requires the predis/predis composer package to be installed.");
} }
// @codeCoverageIgnoreEnd
$this->conn = new Client($config); $this->conn = new Client($config);
} }
@ -107,16 +109,16 @@ class RedisDriver extends AbstractDriver {
public function set($key, $value, $expires = 0) public function set($key, $value, $expires = 0)
{ {
$value = \serialize($value); $value = \serialize($value);
if ($expires !== 0) if ($expires !== 0)
{ {
$this->conn->set($key, $value, "EX {$expires}"); $this->conn->set($key, $value, "EX", $expires);
} }
else else
{ {
$this->conn->set($key, $value); $this->conn->set($key, $value);
} }
return $this; return $this;
} }

View File

@ -104,7 +104,7 @@ class Item implements CacheItemInterface {
{ {
return $this->driver->get($this->key); return $this->driver->get($this->key);
} }
return NULL; return NULL;
} }
@ -155,13 +155,13 @@ class Item implements CacheItemInterface {
*/ */
public function expiresAt($expiration = NULL) 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; return $this;
} }
@ -178,19 +178,19 @@ class Item implements CacheItemInterface {
* @return static * @return static
* The called object. * The called object.
*/ */
public function expiresAfter($time = 0) public function expiresAfter($time = NULL)
{ {
if ($time instanceof \DateInterval) if ($time instanceof \DateInterval)
{ {
$time = $time->format("%s"); $time = $time->format("%s");
} }
$this->ttl = (int) $time; $this->ttl = (int) $time;
return $this; return $this;
} }
/** /**
* Save the current value to the cache * Save the current value to the cache
* *
* @return bool * @return bool
*/ */
@ -200,14 +200,14 @@ class Item implements CacheItemInterface {
{ {
$setResult = $this->driver->set($this->key, $this->value); $setResult = $this->driver->set($this->key, $this->value);
$expResult = $this->driver->expiresAt($this->key, $this->expiresAt); $expResult = $this->driver->expiresAt($this->key, $this->expiresAt);
return $setResult && $expResult; return $setResult && $expResult;
} }
else if ($this->ttl !== NULL && $this->ttl !== 0) 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);
} }
} }

View File

@ -7,7 +7,7 @@ use PHPUnit\Framework\TestCase;
class DriverTestBase extends TestCase { class DriverTestBase extends TestCase {
protected $driver; protected $driver;
public function tearDown() public function tearDown()
{ {
$this->driver->__destruct(); $this->driver->__destruct();
@ -28,4 +28,37 @@ class DriverTestBase extends TestCase {
$this->driver->set('bar', $bar); $this->driver->set('bar', $bar);
$this->assertEquals($bar, $this->driver->get('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'));
}
} }

View File

@ -3,37 +3,108 @@
namespace Aviat\Banker\Tests; namespace Aviat\Banker\Tests;
use Aviat\Banker\Item; use Aviat\Banker\Item;
use Aviat\Banker\ItemCollection;
use Aviat\Banker\Driver\NullDriver; use Aviat\Banker\Driver\NullDriver;
use Aviat\Banker\Exception\InvalidArgumentException; use Aviat\Banker\Tests\Friend;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use DateTime;
use DateInterval;
class ItemTest extends TestCase { class ItemTest extends TestCase {
protected $key = 'foo'; protected $key = 'foo';
protected $item; protected $item;
protected $driver; protected $driver;
public function setUp() public function setUp()
{ {
$this->driver = new NullDriver(); $this->driver = new NullDriver();
$this->item = new Item($this->driver, $this->key); $this->item = new Item($this->driver, $this->key);
} }
public function testGetKey() public function testGetKey()
{ {
$this->assertEquals($this->key, $this->item->getKey()); $this->assertEquals($this->key, $this->item->getKey());
} }
public function testGet() public function testGet()
{ {
// No value set yet // No value set yet
$this->assertNull($this->item->get()); $this->assertNull($this->item->get());
// Set a value // Set a value
$this->item->set('bar') $this->item->set('bar')
->save(); ->save();
$this->assertEquals('bar', $this->item->get()); $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());
}
} }