More tests and fixes
Some checks failed
Gitea - aviat/banker/pipeline/head There was a failure building this commit

This commit is contained in:
Timothy Warren 2020-05-08 18:58:25 -04:00
parent 7c121934a2
commit c4e4ce938e
8 changed files with 76 additions and 29 deletions

View File

@ -73,7 +73,9 @@ abstract class AbstractDriver implements DriverInterface, LoggerAwareInterface {
$setResults = []; $setResults = [];
foreach ($items as $k => $v) foreach ($items as $k => $v)
{ {
$setResults[] = $this->set($k, $v, $expires); $setResults[] = ($expires === NULL)
? $this->set($k, $v)
: $this->set($k, $v, $expires);
} }
// Only return true if all the results are true // Only return true if all the results are true

View File

@ -104,9 +104,11 @@ class ApcuDriver extends AbstractDriver {
{ {
$ttl = $this->getTTLFromExpiration((int)$expires); $ttl = $this->getTTLFromExpiration((int)$expires);
return ($expires === NULL) $errorKeys = ($expires === NULL)
? apcu_store($items) ? apcu_store($items)
: apcu_store($items, NULL, $ttl); : apcu_store($items, NULL, $ttl);
return empty($errorKeys);
} }
/** /**
@ -128,8 +130,8 @@ class ApcuDriver extends AbstractDriver {
*/ */
public function deleteMultiple(array $keys = []): bool public function deleteMultiple(array $keys = []): bool
{ {
$deleted = apcu_delete($keys); $failedToDelete = apcu_delete($keys);
return ($keys <=> $deleted) === 0; return empty($failedToDelete);
} }
/** /**

View File

@ -162,7 +162,16 @@ class MemcachedDriver extends AbstractDriver {
public function deleteMultiple(array $keys = []): bool public function deleteMultiple(array $keys = []): bool
{ {
$deleted = $this->conn->deleteMulti($keys); $deleted = $this->conn->deleteMulti($keys);
return ($keys <=> $deleted) === 0;
foreach ($deleted as $key => $status)
{
if ($status !== TRUE)
{
return FALSE;
}
}
return TRUE;
} }
/** /**

View File

@ -86,6 +86,12 @@ class NullDriver extends AbstractDriver {
*/ */
public function delete(string $key): bool public function delete(string $key): bool
{ {
// Don't return true if the key didn't exist to begin with
if ( ! array_key_exists($key, $this->store))
{
return FALSE;
}
unset($this->store[$key]); unset($this->store[$key]);
return ( ! array_key_exists($key, $this->store)); return ( ! array_key_exists($key, $this->store));
} }

View File

@ -92,12 +92,11 @@ class RedisDriver extends AbstractDriver {
{ {
$value = serialize($value); $value = serialize($value);
if ($expires !== NULL) $status = ($expires !== NULL)
{ ? $this->conn->set($key, $value, 'EX', $expires)
return $this->conn->set($key, $value, 'EX', $expires) === 'OK'; : $this->conn->set($key, $value);
}
return $this->conn->set($key, $value) === 'OK'; return (string)$status === 'OK';
} }
/** /**
@ -108,7 +107,8 @@ class RedisDriver extends AbstractDriver {
*/ */
public function delete(string $key): bool public function delete(string $key): bool
{ {
return (bool) $this->conn->del([$key]); // This call returns the number of keys deleted
return $this->conn->del([$key]) === 1;
} }
/** /**

View File

@ -109,11 +109,6 @@ final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
$items = []; $items = [];
foreach($keys as $key) foreach($keys as $key)
{ {
if ( ! is_string($key))
{
throw new InvalidArgumentException();
}
$items[$key] = array_key_exists($key, $this->deferred) $items[$key] = array_key_exists($key, $this->deferred)
? $this->deferred[$key] ? $this->deferred[$key]
: new Item($this->driver, $key); : new Item($this->driver, $key);

View File

@ -148,15 +148,6 @@ class Teller implements SimpleCache\CacheInterface, LoggerAwareInterface {
return ($ttl === NULL) return ($ttl === NULL)
? $this->driver->setMultiple((array)$values) ? $this->driver->setMultiple((array)$values)
: $this->driver->setMultiple((array)$values, $ttl); : $this->driver->setMultiple((array)$values, $ttl);
$setResults = [];
foreach ($values as $k => $v)
{
$setResults[] = $this->set($k, $v, $ttl);
}
// Only return true if all the results are true
return array_reduce($setResults, fn ($carry, $item) => $item && $carry, TRUE);
} }
/** /**

View File

@ -22,8 +22,6 @@ class DriverTestBase extends TestCase {
protected DriverInterface $driver; protected DriverInterface $driver;
public function testGetSet(): void public function testGetSet(): void
{ {
$this->driver->set('foo', 'bar'); $this->driver->set('foo', 'bar');
@ -37,7 +35,7 @@ class DriverTestBase extends TestCase {
'bar' => 'baz' 'bar' => 'baz'
]; ];
$this->driver->set('bar', $bar); $this->assertTrue($this->driver->set('bar', $bar));
$this->assertEquals($bar, $this->driver->get('bar')); $this->assertEquals($bar, $this->driver->get('bar'));
} }
@ -74,6 +72,42 @@ class DriverTestBase extends TestCase {
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
public function testSetMultiple(): void
{
$data = [
'foo' => [
'apple' => 'orange'
],
'bar' => 'baz',
'baz' => 123456,
'a' => [1, 2, 3],
'b' => false,
'c' => true,
'd' => null,
];
$this->assertTrue($this->driver->setMultiple($data));
$this->assertEquals($data, $this->driver->getMultiple(array_keys($data)));
}
public function testSetMultipleExpires(): void
{
$data = [
'foo' => [
'apple' => 'orange'
],
'bar' => 'baz',
'baz' => 123456,
'a' => [1, 2, 3],
'b' => false,
'c' => true,
'd' => null,
];
$this->assertTrue($this->driver->setMultiple($data, 30));
$this->assertEquals($data, $this->driver->getMultiple(array_keys($data)));
}
public function testSetWithExpires(): void public function testSetWithExpires(): void
{ {
$this->driver->set('foo', 'bar', 30); $this->driver->set('foo', 'bar', 30);
@ -98,12 +132,20 @@ class DriverTestBase extends TestCase {
$this->assertTrue($this->driver->exists('a')); $this->assertTrue($this->driver->exists('a'));
$this->assertTrue($this->driver->exists('b')); $this->assertTrue($this->driver->exists('b'));
/*$this->assertTrue(*/$this->driver->deleteMultiple(['a', 'b']);//); $this->assertTrue($this->driver->deleteMultiple(['a', 'b']));
$this->assertFalse($this->driver->exists('a')); $this->assertFalse($this->driver->exists('a'));
$this->assertFalse($this->driver->exists('b')); $this->assertFalse($this->driver->exists('b'));
} }
public function testDeleteMultipleBadKey(): void
{
$this->assertFalse($this->driver->exists('foo'));
$this->assertFalse($this->driver->exists('bar'));
$this->assertFalse($this->driver->deleteMultiple(['foo', 'bar']));
}
public function testExpiresAt(): void public function testExpiresAt(): void
{ {
$this->driver->set('abc', 'def'); $this->driver->set('abc', 'def');