From c7a1ec0c393613d7c0b13ad0e0cde3c2101cf17a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 13 Aug 2019 19:06:58 +0200 Subject: [PATCH] Add tests --- .../CannotChangeUuidOrDomainException.php | 8 ++++ src/TenantManager.php | 9 ++++ tests/TenantManagerTest.php | 47 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 src/Exceptions/CannotChangeUuidOrDomainException.php diff --git a/src/Exceptions/CannotChangeUuidOrDomainException.php b/src/Exceptions/CannotChangeUuidOrDomainException.php new file mode 100644 index 00000000..52817767 --- /dev/null +++ b/src/Exceptions/CannotChangeUuidOrDomainException.php @@ -0,0 +1,8 @@ +tenant['uuid'])) { throw new \Exception('No UUID supplied (and no tenant is currently identified).'); diff --git a/tests/TenantManagerTest.php b/tests/TenantManagerTest.php index 050c729a..a9a775c5 100644 --- a/tests/TenantManagerTest.php +++ b/tests/TenantManagerTest.php @@ -4,6 +4,7 @@ namespace Stancl\Tenancy\Tests; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; +use Stancl\Tenancy\Exceptions\CannotChangeUuidOrDomainException; class TenantManagerTest extends TestCase { @@ -194,4 +195,50 @@ class TenantManagerTest extends TestCase $tenant2 = tenant()->create('bar.localhost'); $this->assertEqualsCanonicalizing([$tenant1, $tenant2], tenant()->all()->toArray()); } + + /** @test */ + public function properites_can_be_passed_in_the_create_method() + { + $data = ['plan' => 'free', 'subscribed_until' => '2020-01-01']; + $tenant = tenant()->create('foo.localhost', $data); + + $tenant_data = $tenant; + unset($tenant_data['uuid']); + unset($tenant_data['domain']); + + $this->assertSame($data, $tenant_data); + } + + /** @test */ + public function database_name_can_be_passed_in_the_create_method() + { + $database = 'abc'; + config(['tenancy.database_name_key' => '_stancl_tenancy_database_name']); + + $tenant = tenant()->create('foo.localhost', [ + '_stancl_tenancy_database_name' => $database + ]); + + $this->assertSame($database, tenant()->getDatabaseName($tenant)); + } + + /** @test */ + public function uuid_and_domain_cannot_be_changed() + { + $tenant = tenant()->create('foo.localhost'); + + $this->expectException(CannotChangeUuidOrDomainException::class); + tenant()->put('uuid', 'foo', $tenant['uuid']); + + $this->expectException(CannotChangeUuidOrDomainException::class); + tenant()->put(['uuid' => 'foo'], null, $tenant['uuid']); + + tenancy()->init('foo.localhost'); + + $this->expectException(CannotChangeUuidOrDomainException::class); + tenant()->put('uuid', 'foo'); + + $this->expectException(CannotChangeUuidOrDomainException::class); + tenant()->put(['uuid' => 'foo']); + } }