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 = [])
{
// @codeCoverageIgnoreStart
if ( ! class_exists('Memcache'))
{
throw new CacheException('Memcache driver requires the PHP memcache extension');
}
// @codeCoverageIgnoreEnd
$this->conn = new \Memcache();

View File

@ -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
// match the requirements of the interface
throw new CacheException($e->getMessage(), $e->getCode(), $e);
}
// @codeCoverageIgnoreEnd
}
/**

View File

@ -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);
}
@ -110,7 +112,7 @@ class RedisDriver extends AbstractDriver {
if ($expires !== 0)
{
$this->conn->set($key, $value, "EX {$expires}");
$this->conn->set($key, $value, "EX", $expires);
}
else
{

View File

@ -155,12 +155,12 @@ 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,7 +178,7 @@ class Item implements CacheItemInterface {
* @return static
* The called object.
*/
public function expiresAfter($time = 0)
public function expiresAfter($time = NULL)
{
if ($time instanceof \DateInterval)
{
@ -205,9 +205,9 @@ class Item implements CacheItemInterface {
}
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

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

View File

@ -3,11 +3,13 @@
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';
@ -36,4 +38,73 @@ class ItemTest extends TestCase {
$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());
}
}