Restore full test coverage
All checks were successful
Gitea - aviat/banker/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2021-12-01 13:38:54 -05:00
parent 274ad3e2e4
commit f3b958d17f
3 changed files with 75 additions and 40 deletions

View File

@ -1,30 +0,0 @@
<?php declare(strict_types=1);
/**
* Banker
*
* A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16)
*
* PHP version 8+
*
* @package Banker
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2016 - 2021 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 5.0.0
* @link https://git.timshomepage.net/timw4mail/banker
*/
namespace Aviat\Banker;
use DateInterval;
use Psr\Log\{LoggerInterface, LoggerAwareInterface};
/**
* Actual implementations for Simple Cache interface, so that TypeErrors can be caught and replaced with the
* PSR-specified exceptions
*/
abstract class AbstractTeller {
use _Driver;
use LoggerTrait;
}

View File

@ -260,19 +260,28 @@ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
/**
* 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))
$keys = (array)$keys;
$this->validateKeys($keys);
$foundValues = $this->driver->getMultiple($keys);
$foundKeys = array_keys($foundValues);
// If all the values are found, just return them
if ($keys === $foundKeys)
{
throw new Exception\InvalidArgumentException('Keys must be an array or a traversable object');
return $foundValues;
}
$this->validateKeys($keys);
// Otherwise, return a default value for missing keys
$result = $foundValues;
foreach (array_diff($keys, $foundKeys) as $key)
{
$result[$key] = $default;
}
return $this->driver->getMultiple((array)$keys);
return $result;
}
/**

View File

@ -123,6 +123,34 @@ class TellerTest extends TestCase {
}
}
public function testGetMultipleWithDefaultValues(): void
{
$setValues = [
'foo' => 24,
'bar' => '87',
'baz' => [1, 2, 3],
];
$expectedValues = [
'foo' => 24,
'bar' => '87',
'baz' => [1, 2, 3],
'a' => NULL,
'b' => NULL,
'c' => NULL,
'd' => NULL,
'e' => NULL,
'f' => NULL,
];
$searchKeys = array_keys($expectedValues);
$this->assertTrue($this->teller->setMultiple($setValues));
$received = $this->teller->getMultiple($searchKeys);
$this->assertEquals($expectedValues, $received);
}
public function testGetSetMultiple(): void
{
$this->assertTrue($this->teller->setMultiple($this->testValues));
@ -185,20 +213,48 @@ class TellerTest extends TestCase {
public function testBadKeyType(): void
{
$this->expectException(InvalidArgumentException::class);
// $this->expectExceptionMessage('Cache key must be a string.');
$this->teller->get(546567);
}
public function testBadKeyTypeSet(): void
{
$this->expectException(InvalidArgumentException::class);
$this->teller->set(546567, 'foo');
}
public function testBadKeyTypeDelete(): void
{
$this->expectException(InvalidArgumentException::class);
$this->teller->delete(546567);
}
public function testBadKeyTypeHas(): void
{
$this->expectException(InvalidArgumentException::class);
$this->teller->has(546567);
}
public function testBadKeysType (): void
{
$this->expectException(InvalidArgumentException::class);
// $this->expectExceptionMessage('Keys must be an array or a traversable object');
$keys = (object)[];
$this->teller->getMultiple($keys);
}
public function testBadKeysTypeSet (): void
{
$this->expectException(InvalidArgumentException::class);
$keys = (object)[];
$this->teller->setMultiple($keys);
}
public function testBadKeysTypeDelete (): void
{
$this->expectException(InvalidArgumentException::class);
$keys = (object)[];
$this->teller->deleteMultiple($keys);
}
/**
* @dataProvider keyValidationTests
* @param string $key