From f3b958d17f82203a3f761ac591e5503b3681e722 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 1 Dec 2021 13:38:54 -0500 Subject: [PATCH] Restore full test coverage --- src/AbstractTeller.php | 30 -------------------- src/Teller.php | 21 ++++++++++---- tests/TellerTest.php | 64 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 40 deletions(-) delete mode 100644 src/AbstractTeller.php diff --git a/src/AbstractTeller.php b/src/AbstractTeller.php deleted file mode 100644 index 9d27c75..0000000 --- a/src/AbstractTeller.php +++ /dev/null @@ -1,30 +0,0 @@ - - * @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; - - -} \ No newline at end of file diff --git a/src/Teller.php b/src/Teller.php index 6207d60..2068c3e 100644 --- a/src/Teller.php +++ b/src/Teller.php @@ -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; } /** diff --git a/tests/TellerTest.php b/tests/TellerTest.php index 77cc404..3aec04d 100644 --- a/tests/TellerTest.php +++ b/tests/TellerTest.php @@ -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