From 5e99fca070455696a2f89550bea41c3e9b941731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 14 May 2020 02:29:14 +0200 Subject: [PATCH] Custom tenant collections --- src/Contracts/Tenant.php | 13 +++++- src/Database/Collections/TenantCollection.php | 23 ++++++++++ src/Database/Models/Tenant.php | 8 ++++ tests/TenantModelTest.php | 43 +++++++++++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/Database/Collections/TenantCollection.php diff --git a/src/Contracts/Tenant.php b/src/Contracts/Tenant.php index 831e399b..377fb2e6 100644 --- a/src/Contracts/Tenant.php +++ b/src/Contracts/Tenant.php @@ -7,7 +7,18 @@ namespace Stancl\Tenancy\Contracts; */ interface Tenant { + /** Get the name of the key used for identifying the tenant. */ public function getTenantKeyName(): string; + + /** Get the value of the key used for identifying the tenant. */ public function getTenantKey(): string; + + /** Get the value of an internal key. */ + public function getInternal(string $key); + + /** Set the value of an internal key. */ + public function setInternal(string $key, $value); + + /** Run a callback in this tenant's environment. */ public function run(callable $callback); -} \ No newline at end of file +} diff --git a/src/Database/Collections/TenantCollection.php b/src/Database/Collections/TenantCollection.php new file mode 100644 index 00000000..298f0d25 --- /dev/null +++ b/src/Database/Collections/TenantCollection.php @@ -0,0 +1,23 @@ +runForMultiple($this->items, $callable); + + return $this; + } +} \ No newline at end of file diff --git a/src/Database/Models/Tenant.php b/src/Database/Models/Tenant.php index fe2ec642..1ccebc23 100644 --- a/src/Database/Models/Tenant.php +++ b/src/Database/Models/Tenant.php @@ -6,6 +6,7 @@ use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; use Stancl\Tenancy\Events; use Stancl\Tenancy\Contracts; +use Stancl\Tenancy\Database\Collections\TenantCollection; use Stancl\Tenancy\Database\Concerns; /** @@ -13,6 +14,8 @@ use Stancl\Tenancy\Database\Concerns; * @property Carbon $created_at * @property Carbon $updated_at * @property array $data + * + * @method TenantCollection all() */ class Tenant extends Model implements Contracts\Tenant { @@ -36,6 +39,11 @@ class Tenant extends Model implements Contracts\Tenant return $this->getAttribute($this->getTenantKeyName()); } + public function newCollection(array $models = []): TenantCollection + { + return new TenantCollection($models); + } + public function getCasts() { return array_merge($this->dataColumnCasts(), [ diff --git a/tests/TenantModelTest.php b/tests/TenantModelTest.php index 85674890..04c7c025 100644 --- a/tests/TenantModelTest.php +++ b/tests/TenantModelTest.php @@ -19,6 +19,7 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\JobPipeline; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Illuminate\Support\Str; +use Stancl\Tenancy\Database\Collections\TenantCollection; class TenantModelTest extends TestCase { @@ -172,6 +173,38 @@ class TenantModelTest extends TestCase { // todo. tests for registerPriorityListener } + + /** @test */ + public function the_model_uses_TenantCollection() + { + Tenant::create(); + Tenant::create(); + + $this->assertSame(2, Tenant::count()); + $this->assertTrue(Tenant::all() instanceof TenantCollection); + } + + /** @test */ + public function a_command_can_be_run_on_a_collection_of_tenants() + { + Tenant::create([ + 'id' => 't1', + 'foo' => 'bar', + ]); + Tenant::create([ + 'id' => 't2', + 'foo' => 'bar', + ]); + + Tenant::all()->runForEach(function ($tenant) { + $tenant->update([ + 'foo' => 'xyz', + ]); + }); + + $this->assertSame('xyz', Tenant::find('t1')->foo); + $this->assertSame('xyz', Tenant::find('t2')->foo); + } } class MyTenant extends Tenant @@ -198,4 +231,14 @@ class AnotherTenant extends Model implements Contracts\Tenant { $callback(); } + + public function getInternal(string $key) + { + return $this->$key; + } + + public function setInternal(string $key, $value) + { + $this->$key = $value; + } }