1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-04 17:04:03 +00:00

Add tests

This commit is contained in:
Samuel Štancl 2019-08-13 19:06:58 +02:00
parent 57723a5adf
commit c7a1ec0c39
3 changed files with 64 additions and 0 deletions

View file

@ -0,0 +1,8 @@
<?php
namespace Stancl\Tenancy\Exceptions;
class CannotChangeUuidOrDomainException extends \Exception
{
protected $message = 'Uuid and domain cannot be changed.';
}

View file

@ -5,6 +5,7 @@ namespace Stancl\Tenancy;
use Stancl\Tenancy\Interfaces\StorageDriver;
use Stancl\Tenancy\Traits\BootstrapsTenancy;
use Illuminate\Contracts\Foundation\Application;
use Stancl\Tenancy\Exceptions\CannotChangeUuidOrDomainException;
class TenantManager
{
@ -274,6 +275,14 @@ class TenantManager
*/
public function put($key, $value = null, string $uuid = null)
{
if (in_array($key, ['uuid', 'domain'], true) ||
(is_array($key) && array_reduce(array_keys($key), function ($result, $k) {
return $result || in_array($k, ['uuid', 'domain']);
}, false))
) {
throw new CannotChangeUuidOrDomainException;
}
if (\is_null($uuid)) {
if (! isset($this->tenant['uuid'])) {
throw new \Exception('No UUID supplied (and no tenant is currently identified).');

View file

@ -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']);
}
}