From d2ec7f171d28a34fafe7ed64c6a2d817fc0b3a51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 13 Feb 2019 16:26:00 +0100 Subject: [PATCH] Fix #25 better error handling --- src/TenantManager.php | 10 ++++++++-- tests/TenantManagerTest.php | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) 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'); + } }