From 42146a0871e4806b78f2a0f19b4cee7b7d67b2aa Mon Sep 17 00:00:00 2001 From: "steven.lewis" Date: Thu, 4 Nov 2021 09:06:51 +0000 Subject: [PATCH] test null default connection within ConnectionLocator and throw ConnectionNotFound from getDefault() on a null default --- src/ConnectionLocator.php | 5 +++++ tests/ConnectionLocatorTest.php | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/ConnectionLocator.php b/src/ConnectionLocator.php index 90a78369..66cb1fa6 100644 --- a/src/ConnectionLocator.php +++ b/src/ConnectionLocator.php @@ -90,9 +90,14 @@ public function setDefault(callable $callable): void * * @return ExtendedPdoInterface * + * @throws Exception\ConnectionNotFound */ public function getDefault(): ExtendedPdoInterface { + if (! $this->default) { + throw new Exception\ConnectionNotFound("default"); + } + if (! $this->default instanceof ExtendedPdo) { $this->default = call_user_func($this->default); } diff --git a/tests/ConnectionLocatorTest.php b/tests/ConnectionLocatorTest.php index 146729fd..4d01daee 100644 --- a/tests/ConnectionLocatorTest.php +++ b/tests/ConnectionLocatorTest.php @@ -49,6 +49,13 @@ protected function newLocator($read = [], $write = []) return new ConnectionLocator($this->default, $read, $write); } + public function testNullDefault() + { + $locator = new ConnectionLocator(null, $this->read, $this->write); + $this->expectException('Aura\Sql\Exception\ConnectionNotFound'); + $locator->getDefault(); + } + public function testGetDefault() { $locator = $this->newLocator();