From 93fc961b34728ae6b1629683e79eba931342ebae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 20 Sep 2019 19:44:05 +0200 Subject: [PATCH] Tenant::with --- src/Tenant.php | 16 ++++++++++++++-- tests/TenantClassTest.php | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Tenant.php b/src/Tenant.php index 713b9fe9..e4aa3888 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -6,6 +6,7 @@ namespace Stancl\Tenancy; use ArrayAccess; use Illuminate\Foundation\Application; +use Illuminate\Support\Str; use Stancl\Tenancy\Contracts\StorageDriver; use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator; use Stancl\Tenancy\Exceptions\TenantStorageException; @@ -267,6 +268,13 @@ class Tenant implements ArrayAccess return $this->put($key, $value); } + public function with(string $key, $value): self + { + $this->data[$key] = $value; + + return $this; + } + public function __get($key) { return $this->get($key); @@ -280,8 +288,12 @@ class Tenant implements ArrayAccess $this->data[$key] = $value; } - public function __call($name, $arguments) + public function __call($method, $parameters) { - // todo withId() + if (Str::startsWith($method, 'with')) { + return $this->with(Str::snake(substr($method, 4)), $parameters[0]); + } + + // todo throw some exception? } } diff --git a/tests/TenantClassTest.php b/tests/TenantClassTest.php index 322530c9..4a11dc32 100644 --- a/tests/TenantClassTest.php +++ b/tests/TenantClassTest.php @@ -72,4 +72,18 @@ class TenantClassTest extends TestCase $this->assertEqualsCanonicalizing(['completely.localhost', 'different.localhost', 'domains.localhost'], $tenant->domains); $this->assertEqualsCanonicalizing(['completely.localhost', 'different.localhost', 'domains.localhost'], tenancy()->find($id)->domains); } + + /** @test */ + public function with_methods_work() + { + $id = 'foo' . $this->randomString(); + $tenant = Tenant::new()->withDomains(['foo.localhost'])->with('id', $id); + $this->assertSame($id, $tenant->id); + + $id2 = 'bar' . $this->randomString(); + $tenant2 = Tenant::new()->withDomains(['bar.localhost'])->withId($id2)->withFooBar('xyz'); + $this->assertSame($id2, $tenant2->data['id']); + $this->assertSame('xyz', $tenant2->foo_bar); + $this->assertArrayHasKey('foo_bar', $tenant2->data); + } }