Fix tests related to logging checks
All checks were successful
Gitea - aviat/banker/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2021-12-01 12:56:28 -05:00
parent 6f36f572a0
commit 274ad3e2e4
3 changed files with 121 additions and 122 deletions

View File

@ -22,116 +22,9 @@ use Psr\Log\{LoggerInterface, LoggerAwareInterface};
* Actual implementations for Simple Cache interface, so that TypeErrors can be caught and replaced with the * Actual implementations for Simple Cache interface, so that TypeErrors can be caught and replaced with the
* PSR-specified exceptions * PSR-specified exceptions
*/ */
abstract class AbstractTeller implements LoggerAwareInterface { abstract class AbstractTeller {
use _Driver; use _Driver;
use LoggerTrait; use LoggerTrait;
/**
* Set up the cache backend
*
* @param array $config
* @param LoggerInterface|null $logger
*/
public function __construct(array $config, ?LoggerInterface $logger = NULL)
{
$this->driver = $this->loadDriver($config);
if ($logger !== NULL)
{
$this->setLogger($logger);
}
}
/**
* Wipes clean the entire cache's keys.
*
* @return bool True on success and false on failure.
*/
public function clear(): bool
{
return $this->driver->flush();
}
/**
* Fetches a value from the cache.
* @throws Exception\InvalidArgumentException
*/
protected function get(string $key, mixed $default = null): mixed
{
$this->validateKey($key);
return ($this->driver->exists($key)) ? $this->driver->get($key) : $default;
}
/**
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
* @throws Exception\InvalidArgumentException
*/
protected function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
{
$this->validateKey($key);
return $this->driver->set($key, $value, $ttl);
}
/**
* Delete an item from the cache by its unique key.
* @throws Exception\InvalidArgumentException
*/
protected function delete(string $key): bool
{
$this->validateKey($key);
return $this->driver->delete($key);
}
/**
* Obtains multiple cache items by their unique keys.
* @throws Exception\InvalidArgumentException
*/
protected function getMultiple(iterable $keys, mixed $default = null): iterable
{
// Check type of keys
if ( ! is_iterable($keys))
{
throw new Exception\InvalidArgumentException('Keys must be an array or a traversable object');
}
$this->validateKeys($keys);
return $this->driver->getMultiple((array)$keys);
}
/**
* Persists a set of key => value pairs in the cache, with an optional TTL.
*/
protected function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
{
$this->validateKeys($values, TRUE);
return ($ttl === NULL)
? $this->driver->setMultiple((array)$values)
: $this->driver->setMultiple((array)$values, $ttl);
}
/**
* Deletes multiple cache items in a single operation.
*/
protected function deleteMultiple(iterable $keys): bool
{
$this->validateKeys($keys);
return $this->driver->deleteMultiple((array)$keys);
}
/**
* Determines whether an item is present in the cache.
* @throws Exception\InvalidArgumentException
*/
protected function has(string $key): bool
{
$this->validateKey($key);
return $this->driver->exists($key);
}
} }

View File

@ -17,13 +17,42 @@ namespace Aviat\Banker;
use Aviat\Banker\Exception\InvalidArgumentException; use Aviat\Banker\Exception\InvalidArgumentException;
use DateInterval; use DateInterval;
use Psr\Log\{LoggerInterface, LoggerAwareInterface};
use Psr\SimpleCache; use Psr\SimpleCache;
use TypeError; use TypeError;
/** /**
* Implements PSR-16 (SimpleCache) * Implements PSR-16 (SimpleCache)
*/ */
class Teller extends AbstractTeller implements SimpleCache\CacheInterface { class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
use _Driver;
use LoggerTrait;
/**
* Set up the cache backend
*
* @param array $config
* @param LoggerInterface|null $logger
*/
public function __construct(array $config, ?LoggerInterface $logger = NULL)
{
$this->driver = $this->loadDriver($config);
if ($logger !== NULL)
{
$this->setLogger($logger);
}
}
/**
* Wipes clean the entire cache's keys.
*
* @return bool True on success and false on failure.
*/
public function clear(): bool
{
return $this->driver->flush();
}
/** /**
* Fetches a value from the cache. * Fetches a value from the cache.
@ -40,7 +69,7 @@ class Teller extends AbstractTeller implements SimpleCache\CacheInterface {
{ {
try try
{ {
return parent::get($key, $default); return $this->_get($key, $default);
} }
catch (TypeError $e) catch (TypeError $e)
{ {
@ -66,7 +95,7 @@ class Teller extends AbstractTeller implements SimpleCache\CacheInterface {
{ {
try try
{ {
return parent::set($key, $value, $ttl); return $this->_set($key, $value, $ttl);
} }
catch (TypeError $e) catch (TypeError $e)
{ {
@ -88,7 +117,7 @@ class Teller extends AbstractTeller implements SimpleCache\CacheInterface {
{ {
try try
{ {
return parent::delete($key); return $this->_delete($key);
} }
catch (TypeError $e) catch (TypeError $e)
{ {
@ -112,7 +141,7 @@ class Teller extends AbstractTeller implements SimpleCache\CacheInterface {
{ {
try try
{ {
return parent::getMultiple($keys, $default); return $this->_getMultiple($keys, $default);
} }
catch (TypeError $e) catch (TypeError $e)
{ {
@ -138,7 +167,7 @@ class Teller extends AbstractTeller implements SimpleCache\CacheInterface {
{ {
try try
{ {
return parent::setMultiple($values, $ttl); return $this->_setMultiple($values, $ttl);
} }
catch (TypeError $e) catch (TypeError $e)
{ {
@ -161,7 +190,7 @@ class Teller extends AbstractTeller implements SimpleCache\CacheInterface {
{ {
try try
{ {
return parent::deleteMultiple($keys); return $this->_deleteMultiple($keys);
} }
catch (TypeError $e) catch (TypeError $e)
{ {
@ -188,11 +217,94 @@ class Teller extends AbstractTeller implements SimpleCache\CacheInterface {
{ {
try try
{ {
return parent::has($key); return $this->_has($key);
} }
catch (TypeError $e) catch (TypeError $e)
{ {
throw new InvalidArgumentException($e->getMessage(), $e->getCode()); throw new InvalidArgumentException($e->getMessage(), $e->getCode());
} }
} }
/**
* Fetches a value from the cache.
* @throws Exception\InvalidArgumentException
*/
protected function _get(string $key, mixed $default = null): mixed
{
$this->validateKey($key);
return ($this->driver->exists($key)) ? $this->driver->get($key) : $default;
}
/**
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
* @throws Exception\InvalidArgumentException
*/
protected function _set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
{
$this->validateKey($key);
return $this->driver->set($key, $value, $ttl);
}
/**
* Delete an item from the cache by its unique key.
* @throws Exception\InvalidArgumentException
*/
protected function _delete(string $key): bool
{
$this->validateKey($key);
return $this->driver->delete($key);
}
/**
* Obtains multiple cache items by their unique keys.
* @throws Exception\InvalidArgumentException
*/
protected function _getMultiple(iterable $keys, mixed $default = null): iterable
{
// Check type of keys
if ( ! is_iterable($keys))
{
throw new Exception\InvalidArgumentException('Keys must be an array or a traversable object');
}
$this->validateKeys($keys);
return $this->driver->getMultiple((array)$keys);
}
/**
* Persists a set of key => value pairs in the cache, with an optional TTL.
*/
protected function _setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
{
$this->validateKeys($values, TRUE);
return ($ttl === NULL)
? $this->driver->setMultiple((array)$values)
: $this->driver->setMultiple((array)$values, $ttl);
}
/**
* Deletes multiple cache items in a single operation.
*/
protected function _deleteMultiple(iterable $keys): bool
{
$this->validateKeys($keys);
return $this->driver->deleteMultiple((array)$keys);
}
/**
* Determines whether an item is present in the cache.
* @throws Exception\InvalidArgumentException
*/
protected function _has(string $key): bool
{
$this->validateKey($key);
return $this->driver->exists($key);
}
} }

View File

@ -58,8 +58,6 @@ class TellerTest extends TestCase {
public function testGetDefaultLogger(): void public function testGetDefaultLogger(): void
{ {
$this->markTestSkipped();
$friend = new Friend($this->teller); $friend = new Friend($this->teller);
$driverFriend = new Friend($friend->driver); $driverFriend = new Friend($friend->driver);
@ -74,8 +72,6 @@ class TellerTest extends TestCase {
public function testSetLoggerInConstructor(): void public function testSetLoggerInConstructor(): void
{ {
$this->markTestSkipped();
$logger = new Logger('test'); $logger = new Logger('test');
$logger->pushHandler(new SyslogHandler('warning', LOG_USER, Logger::WARNING)); $logger->pushHandler(new SyslogHandler('warning', LOG_USER, Logger::WARNING));
@ -98,8 +94,6 @@ class TellerTest extends TestCase {
public function testGetSetLogger(): void public function testGetSetLogger(): void
{ {
$this->markTestSkipped();
$logger = new Logger('test'); $logger = new Logger('test');
$logger->pushHandler(new SyslogHandler('warning2',LOG_USER, Logger::WARNING)); $logger->pushHandler(new SyslogHandler('warning2',LOG_USER, Logger::WARNING));