From 0198a30413704efed8dd6c2b71f466abfbd7cb69 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Mon, 10 Oct 2022 15:11:37 +0200 Subject: [PATCH] Add and test `Tenant::current()` --- src/Database/Models/Tenant.php | 5 +++++ tests/TenantModelTest.php | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Database/Models/Tenant.php b/src/Database/Models/Tenant.php index 88c34146..bbaab7a3 100644 --- a/src/Database/Models/Tenant.php +++ b/src/Database/Models/Tenant.php @@ -45,6 +45,11 @@ class Tenant extends Model implements Contracts\Tenant return $this->getAttribute($this->getTenantKeyName()); } + public static function current(): Tenant|null + { + return tenant(); + } + public function newCollection(array $models = []): TenantCollection { return new TenantCollection($models); diff --git a/tests/TenantModelTest.php b/tests/TenantModelTest.php index b4fd38f6..40dfa996 100644 --- a/tests/TenantModelTest.php +++ b/tests/TenantModelTest.php @@ -141,6 +141,18 @@ test('a command can be run on a collection of tenants', function () { expect(Tenant::find('t2')->foo)->toBe('xyz'); }); +test('the current method returns the currently initialized tenant', function() { + tenancy()->initialize($tenant = Tenant::create()); + + expect(Tenant::current())->toBe($tenant); +}); + +test('the current method returns null if there is no currently initialized tenant', function() { + tenancy()->end(); + + expect(Tenant::current())->toBeNull(); +}); + class MyTenant extends Tenant { protected $table = 'tenants';