diff --git a/src/TenantManager.php b/src/TenantManager.php index 011cd52f..f9e4c76b 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -147,9 +147,15 @@ class TenantManager */ public function findByDomain(string $domain = null, $fields = []) { - $domain = $domain ?: $this->currentDomain(); + $domain = $domain ? : $this->currentDomain(); - return $this->find($this->getIdByDomain($domain), $fields); + $uuid = $this->getIdByDomain($domain); + + if (is_null($uuid)) { + throw new \Exception("Tenant with domain $domain could not be identified."); + } + + return $this->find($uuid, $fields); } public static function currentDomain(): ?string diff --git a/tests/TenantManagerTest.php b/tests/TenantManagerTest.php index e292dd13..0b3ec803 100644 --- a/tests/TenantManagerTest.php +++ b/tests/TenantManagerTest.php @@ -91,4 +91,11 @@ class TenantManagerTest extends TestCase $this->assertSame($domain, tenant()->create($domain)['domain']); } + + /** @test */ + public function find_by_domain_throws_an_exception_when_an_unused_domain_is_supplied() + { + $this->expectException(\Exception::class); + tenancy()->findByDomain('nonexistent.domain'); + } }