mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-06 00:34:05 +00:00
Convert test cases
This commit is contained in:
parent
405b91085e
commit
a8dcd0dfc9
27 changed files with 3059 additions and 3658 deletions
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Events\TenancyEnded;
|
use Stancl\Tenancy\Events\TenancyEnded;
|
||||||
|
|
@ -12,19 +10,14 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class AutomaticModeTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('context is switched when tenancy is initialized', function () {
|
||||||
public function context_is_switched_when_tenancy_is_initialized()
|
|
||||||
{
|
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
MyBootstrapper::class,
|
MyBootstrapper::class,
|
||||||
]]);
|
]]);
|
||||||
|
|
@ -36,21 +29,17 @@ class AutomaticModeTest extends TestCase
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
$this->assertSame('acme', app('tenancy_initialized_for_tenant'));
|
$this->assertSame('acme', app('tenancy_initialized_for_tenant'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('context is reverted when tenancy is ended', function () {
|
||||||
public function context_is_reverted_when_tenancy_is_ended()
|
|
||||||
{
|
|
||||||
$this->context_is_switched_when_tenancy_is_initialized();
|
$this->context_is_switched_when_tenancy_is_initialized();
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
$this->assertSame(true, app('tenancy_ended'));
|
$this->assertSame(true, app('tenancy_ended'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('context is switched when tenancy is reinitialized', function () {
|
||||||
public function context_is_switched_when_tenancy_is_reinitialized()
|
|
||||||
{
|
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
MyBootstrapper::class,
|
MyBootstrapper::class,
|
||||||
]]);
|
]]);
|
||||||
|
|
@ -70,11 +59,9 @@ class AutomaticModeTest extends TestCase
|
||||||
tenancy()->initialize($tenant2);
|
tenancy()->initialize($tenant2);
|
||||||
|
|
||||||
$this->assertSame('foobar', app('tenancy_initialized_for_tenant'));
|
$this->assertSame('foobar', app('tenancy_initialized_for_tenant'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('central helper runs callbacks in the central state', function () {
|
||||||
public function central_helper_runs_callbacks_in_the_central_state()
|
|
||||||
{
|
|
||||||
tenancy()->initialize($tenant = Tenant::create());
|
tenancy()->initialize($tenant = Tenant::create());
|
||||||
|
|
||||||
tenancy()->central(function () {
|
tenancy()->central(function () {
|
||||||
|
|
@ -82,21 +69,17 @@ class AutomaticModeTest extends TestCase
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->assertSame($tenant, tenant());
|
$this->assertSame($tenant, tenant());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('central helper returns the value from the callback', function () {
|
||||||
public function central_helper_returns_the_value_from_the_callback()
|
|
||||||
{
|
|
||||||
tenancy()->initialize(Tenant::create());
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
$this->assertSame('foo', tenancy()->central(function () {
|
$this->assertSame('foo', tenancy()->central(function () {
|
||||||
return 'foo';
|
return 'foo';
|
||||||
}));
|
}));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('central helper reverts back to tenant context', function () {
|
||||||
public function central_helper_reverts_back_to_tenant_context()
|
|
||||||
{
|
|
||||||
tenancy()->initialize($tenant = Tenant::create());
|
tenancy()->initialize($tenant = Tenant::create());
|
||||||
|
|
||||||
tenancy()->central(function () {
|
tenancy()->central(function () {
|
||||||
|
|
@ -104,11 +87,9 @@ class AutomaticModeTest extends TestCase
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->assertSame($tenant, tenant());
|
$this->assertSame($tenant, tenant());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('central helper doesnt change tenancy state when called in central context', function () {
|
||||||
public function central_helper_doesnt_change_tenancy_state_when_called_in_central_context()
|
|
||||||
{
|
|
||||||
$this->assertFalse(tenancy()->initialized);
|
$this->assertFalse(tenancy()->initialized);
|
||||||
$this->assertNull(tenant());
|
$this->assertNull(tenant());
|
||||||
|
|
||||||
|
|
@ -118,18 +99,15 @@ class AutomaticModeTest extends TestCase
|
||||||
|
|
||||||
$this->assertFalse(tenancy()->initialized);
|
$this->assertFalse(tenancy()->initialized);
|
||||||
$this->assertNull(tenant());
|
$this->assertNull(tenant());
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
class MyBootstrapper implements TenancyBootstrapper
|
// Helpers
|
||||||
|
function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant)
|
||||||
{
|
{
|
||||||
public function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant)
|
|
||||||
{
|
|
||||||
app()->instance('tenancy_initialized_for_tenant', $tenant->getTenantKey());
|
app()->instance('tenancy_initialized_for_tenant', $tenant->getTenantKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function revert()
|
function revert()
|
||||||
{
|
{
|
||||||
app()->instance('tenancy_ended', true);
|
app()->instance('tenancy_ended', true);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Filesystem\FilesystemAdapter;
|
use Illuminate\Filesystem\FilesystemAdapter;
|
||||||
use ReflectionObject;
|
use ReflectionObject;
|
||||||
use ReflectionProperty;
|
use ReflectionProperty;
|
||||||
|
|
@ -26,14 +24,9 @@ use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
||||||
|
|
||||||
class BootstrapperTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public $mockConsoleOutput = false;
|
|
||||||
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
Event::listen(
|
Event::listen(
|
||||||
TenantCreated::class,
|
TenantCreated::class,
|
||||||
JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
|
|
@ -43,11 +36,9 @@ class BootstrapperTest extends TestCase
|
||||||
|
|
||||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('database data is separated', function () {
|
||||||
public function database_data_is_separated()
|
|
||||||
{
|
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
DatabaseTenancyBootstrapper::class,
|
DatabaseTenancyBootstrapper::class,
|
||||||
]]);
|
]]);
|
||||||
|
|
@ -76,11 +67,9 @@ class BootstrapperTest extends TestCase
|
||||||
// Assert Bar user is not in this DB
|
// Assert Bar user is not in this DB
|
||||||
$this->assertCount(1, DB::table('users')->get());
|
$this->assertCount(1, DB::table('users')->get());
|
||||||
$this->assertSame('Foo', DB::table('users')->first()->name);
|
$this->assertSame('Foo', DB::table('users')->first()->name);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('cache data is separated', function () {
|
||||||
public function cache_data_is_separated()
|
|
||||||
{
|
|
||||||
config([
|
config([
|
||||||
'tenancy.bootstrappers' => [
|
'tenancy.bootstrappers' => [
|
||||||
CacheTenancyBootstrapper::class,
|
CacheTenancyBootstrapper::class,
|
||||||
|
|
@ -119,11 +108,9 @@ class BootstrapperTest extends TestCase
|
||||||
|
|
||||||
// Asset central is still the same
|
// Asset central is still the same
|
||||||
$this->assertSame('central', Cache::get('foo'));
|
$this->assertSame('central', Cache::get('foo'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('redis data is separated', function () {
|
||||||
public function redis_data_is_separated()
|
|
||||||
{
|
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
RedisTenancyBootstrapper::class,
|
RedisTenancyBootstrapper::class,
|
||||||
]]);
|
]]);
|
||||||
|
|
@ -150,11 +137,9 @@ class BootstrapperTest extends TestCase
|
||||||
tenancy()->initialize($tenant3);
|
tenancy()->initialize($tenant3);
|
||||||
$this->assertSame(null, Redis::get('foo'));
|
$this->assertSame(null, Redis::get('foo'));
|
||||||
$this->assertSame(null, Redis::get('abc'));
|
$this->assertSame(null, Redis::get('abc'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('filesystem data is separated', function () {
|
||||||
public function filesystem_data_is_separated()
|
|
||||||
{
|
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
FilesystemTenancyBootstrapper::class,
|
FilesystemTenancyBootstrapper::class,
|
||||||
]]);
|
]]);
|
||||||
|
|
@ -192,17 +177,18 @@ class BootstrapperTest extends TestCase
|
||||||
$expected_storage_path = $old_storage_path . '/tenant' . tenant('id'); // /tenant = suffix base
|
$expected_storage_path = $old_storage_path . '/tenant' . tenant('id'); // /tenant = suffix base
|
||||||
|
|
||||||
// Check that disk prefixes respect the root_override logic
|
// Check that disk prefixes respect the root_override logic
|
||||||
$this->assertSame($expected_storage_path . '/app/', $this->getDiskPrefix('local'));
|
$this->assertSame($expected_storage_path . '/app/', getDiskPrefix('local'));
|
||||||
$this->assertSame($expected_storage_path . '/app/public/', $this->getDiskPrefix('public'));
|
$this->assertSame($expected_storage_path . '/app/public/', getDiskPrefix('public'));
|
||||||
$this->assertSame('tenant' . tenant('id') . '/', $this->getDiskPrefix('s3'), '/');
|
$this->assertSame('tenant' . tenant('id') . '/', getDiskPrefix('s3'), '/');
|
||||||
|
|
||||||
// Check suffixing logic
|
// Check suffixing logic
|
||||||
$new_storage_path = storage_path();
|
$new_storage_path = storage_path();
|
||||||
$this->assertEquals($expected_storage_path, $new_storage_path);
|
$this->assertEquals($expected_storage_path, $new_storage_path);
|
||||||
}
|
});
|
||||||
|
|
||||||
protected function getDiskPrefix(string $disk): string
|
// Helpers
|
||||||
{
|
function getDiskPrefix(string $disk): string
|
||||||
|
{
|
||||||
/** @var FilesystemAdapter $disk */
|
/** @var FilesystemAdapter $disk */
|
||||||
$disk = Storage::disk($disk);
|
$disk = Storage::disk($disk);
|
||||||
$adapter = $disk->getAdapter();
|
$adapter = $disk->getAdapter();
|
||||||
|
|
@ -221,7 +207,4 @@ class BootstrapperTest extends TestCase
|
||||||
$prefix->setAccessible(true);
|
$prefix->setAccessible(true);
|
||||||
|
|
||||||
return $prefix->getValue($prefixer);
|
return $prefix->getValue($prefixer);
|
||||||
}
|
|
||||||
|
|
||||||
// for queues see QueueTest
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,74 +2,57 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class CacheManagerTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
CacheTenancyBootstrapper::class,
|
CacheTenancyBootstrapper::class,
|
||||||
]]);
|
]]);
|
||||||
|
|
||||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('default tag is automatically applied', function () {
|
||||||
public function default_tag_is_automatically_applied()
|
|
||||||
{
|
|
||||||
tenancy()->initialize(Tenant::create());
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
$this->assertArrayIsSubset([config('tenancy.cache.tag_base') . tenant('id')], cache()->tags('foo')->getTags()->getNames());
|
$this->assertArrayIsSubset([config('tenancy.cache.tag_base') . tenant('id')], cache()->tags('foo')->getTags()->getNames());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tags are merged when array is passed', function () {
|
||||||
public function tags_are_merged_when_array_is_passed()
|
|
||||||
{
|
|
||||||
tenancy()->initialize(Tenant::create());
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo', 'bar'];
|
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo', 'bar'];
|
||||||
$this->assertEquals($expected, cache()->tags(['foo', 'bar'])->getTags()->getNames());
|
$this->assertEquals($expected, cache()->tags(['foo', 'bar'])->getTags()->getNames());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tags are merged when string is passed', function () {
|
||||||
public function tags_are_merged_when_string_is_passed()
|
|
||||||
{
|
|
||||||
tenancy()->initialize(Tenant::create());
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo'];
|
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo'];
|
||||||
$this->assertEquals($expected, cache()->tags('foo')->getTags()->getNames());
|
$this->assertEquals($expected, cache()->tags('foo')->getTags()->getNames());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('exception is thrown when zero arguments are passed to tags method', function () {
|
||||||
public function exception_is_thrown_when_zero_arguments_are_passed_to_tags_method()
|
|
||||||
{
|
|
||||||
tenancy()->initialize(Tenant::create());
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
$this->expectException(\Exception::class);
|
$this->expectException(\Exception::class);
|
||||||
cache()->tags();
|
cache()->tags();
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('exception is thrown when more than one argument is passed to tags method', function () {
|
||||||
public function exception_is_thrown_when_more_than_one_argument_is_passed_to_tags_method()
|
|
||||||
{
|
|
||||||
tenancy()->initialize(Tenant::create());
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
$this->expectException(\Exception::class);
|
$this->expectException(\Exception::class);
|
||||||
cache()->tags(1, 2);
|
cache()->tags(1, 2);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tags separate cache well enough', function () {
|
||||||
public function tags_separate_cache_well_enough()
|
|
||||||
{
|
|
||||||
$tenant1 = Tenant::create();
|
$tenant1 = Tenant::create();
|
||||||
tenancy()->initialize($tenant1);
|
tenancy()->initialize($tenant1);
|
||||||
|
|
||||||
|
|
@ -83,11 +66,9 @@ class CacheManagerTest extends TestCase
|
||||||
|
|
||||||
cache()->put('foo', 'xyz', 1);
|
cache()->put('foo', 'xyz', 1);
|
||||||
$this->assertSame('xyz', cache()->get('foo'));
|
$this->assertSame('xyz', cache()->get('foo'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('invoking the cache helper works', function () {
|
||||||
public function invoking_the_cache_helper_works()
|
|
||||||
{
|
|
||||||
$tenant1 = Tenant::create();
|
$tenant1 = Tenant::create();
|
||||||
tenancy()->initialize($tenant1);
|
tenancy()->initialize($tenant1);
|
||||||
|
|
||||||
|
|
@ -101,11 +82,9 @@ class CacheManagerTest extends TestCase
|
||||||
|
|
||||||
cache(['foo' => 'xyz'], 1);
|
cache(['foo' => 'xyz'], 1);
|
||||||
$this->assertSame('xyz', cache('foo'));
|
$this->assertSame('xyz', cache('foo'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('cache is persisted', function () {
|
||||||
public function cache_is_persisted()
|
|
||||||
{
|
|
||||||
$tenant1 = Tenant::create();
|
$tenant1 = Tenant::create();
|
||||||
tenancy()->initialize($tenant1);
|
tenancy()->initialize($tenant1);
|
||||||
|
|
||||||
|
|
@ -116,11 +95,9 @@ class CacheManagerTest extends TestCase
|
||||||
|
|
||||||
tenancy()->initialize($tenant1);
|
tenancy()->initialize($tenant1);
|
||||||
$this->assertSame('bar', cache('foo'));
|
$this->assertSame('bar', cache('foo'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('cache is persisted when reidentification is used', function () {
|
||||||
public function cache_is_persisted_when_reidentification_is_used()
|
|
||||||
{
|
|
||||||
$tenant1 = Tenant::create();
|
$tenant1 = Tenant::create();
|
||||||
$tenant2 = Tenant::create();
|
$tenant2 = Tenant::create();
|
||||||
tenancy()->initialize($tenant1);
|
tenancy()->initialize($tenant1);
|
||||||
|
|
@ -133,5 +110,4 @@ class CacheManagerTest extends TestCase
|
||||||
|
|
||||||
tenancy()->initialize($tenant1);
|
tenancy()->initialize($tenant1);
|
||||||
$this->assertSame('bar', cache('foo'));
|
$this->assertSame('bar', cache('foo'));
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,24 +2,17 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
|
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class CachedTenantResolverTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public function tearDown(): void
|
afterEach(function () {
|
||||||
{
|
|
||||||
DomainTenantResolver::$shouldCache = false;
|
DomainTenantResolver::$shouldCache = false;
|
||||||
|
});
|
||||||
|
|
||||||
parent::tearDown();
|
test('tenants can be resolved using the cached resolver', function () {
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function tenants_can_be_resolved_using_the_cached_resolver()
|
|
||||||
{
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
$tenant->domains()->create([
|
$tenant->domains()->create([
|
||||||
'domain' => 'acme',
|
'domain' => 'acme',
|
||||||
|
|
@ -27,11 +20,9 @@ class CachedTenantResolverTest extends TestCase
|
||||||
|
|
||||||
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme')));
|
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme')));
|
||||||
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme')));
|
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme')));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('the underlying resolver is not touched when using the cached resolver', function () {
|
||||||
public function the_underlying_resolver_is_not_touched_when_using_the_cached_resolver()
|
|
||||||
{
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
$tenant->domains()->create([
|
$tenant->domains()->create([
|
||||||
'domain' => 'acme',
|
'domain' => 'acme',
|
||||||
|
|
@ -52,11 +43,9 @@ class CachedTenantResolverTest extends TestCase
|
||||||
DB::flushQueryLog();
|
DB::flushQueryLog();
|
||||||
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme')));
|
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme')));
|
||||||
$this->assertEmpty(DB::getQueryLog()); // empty
|
$this->assertEmpty(DB::getQueryLog()); // empty
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('cache is invalidated when the tenant is updated', function () {
|
||||||
public function cache_is_invalidated_when_the_tenant_is_updated()
|
|
||||||
{
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
$tenant->createDomain([
|
$tenant->createDomain([
|
||||||
'domain' => 'acme',
|
'domain' => 'acme',
|
||||||
|
|
@ -78,11 +67,9 @@ class CachedTenantResolverTest extends TestCase
|
||||||
DB::flushQueryLog();
|
DB::flushQueryLog();
|
||||||
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme')));
|
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme')));
|
||||||
$this->assertNotEmpty(DB::getQueryLog()); // not empty
|
$this->assertNotEmpty(DB::getQueryLog()); // not empty
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('cache is invalidated when a tenants domain is changed', function () {
|
||||||
public function cache_is_invalidated_when_a_tenants_domain_is_changed()
|
|
||||||
{
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
$tenant->createDomain([
|
$tenant->createDomain([
|
||||||
'domain' => 'acme',
|
'domain' => 'acme',
|
||||||
|
|
@ -108,5 +95,4 @@ class CachedTenantResolverTest extends TestCase
|
||||||
DB::flushQueryLog();
|
DB::flushQueryLog();
|
||||||
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('bar')));
|
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('bar')));
|
||||||
$this->assertNotEmpty(DB::getQueryLog()); // not empty
|
$this->assertNotEmpty(DB::getQueryLog()); // not empty
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,19 +2,14 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
||||||
use Stancl\Tenancy\Database\Models;
|
use Stancl\Tenancy\Database\Models;
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomainOrSubdomain;
|
use Stancl\Tenancy\Middleware\InitializeTenancyByDomainOrSubdomain;
|
||||||
|
|
||||||
class CombinedDomainAndSubdomainIdentificationTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
Route::group([
|
Route::group([
|
||||||
'middleware' => InitializeTenancyByDomainOrSubdomain::class,
|
'middleware' => InitializeTenancyByDomainOrSubdomain::class,
|
||||||
], function () {
|
], function () {
|
||||||
|
|
@ -24,11 +19,9 @@ class CombinedDomainAndSubdomainIdentificationTest extends TestCase
|
||||||
});
|
});
|
||||||
|
|
||||||
config(['tenancy.tenant_model' => CombinedTenant::class]);
|
config(['tenancy.tenant_model' => CombinedTenant::class]);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant can be identified by subdomain', function () {
|
||||||
public function tenant_can_be_identified_by_subdomain()
|
|
||||||
{
|
|
||||||
config(['tenancy.central_domains' => ['localhost']]);
|
config(['tenancy.central_domains' => ['localhost']]);
|
||||||
|
|
||||||
$tenant = CombinedTenant::create([
|
$tenant = CombinedTenant::create([
|
||||||
|
|
@ -47,11 +40,9 @@ class CombinedDomainAndSubdomainIdentificationTest extends TestCase
|
||||||
|
|
||||||
$this->assertTrue(tenancy()->initialized);
|
$this->assertTrue(tenancy()->initialized);
|
||||||
$this->assertSame('acme', tenant('id'));
|
$this->assertSame('acme', tenant('id'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant can be identified by domain', function () {
|
||||||
public function tenant_can_be_identified_by_domain()
|
|
||||||
{
|
|
||||||
config(['tenancy.central_domains' => []]);
|
config(['tenancy.central_domains' => []]);
|
||||||
|
|
||||||
$tenant = CombinedTenant::create([
|
$tenant = CombinedTenant::create([
|
||||||
|
|
@ -70,10 +61,4 @@ class CombinedDomainAndSubdomainIdentificationTest extends TestCase
|
||||||
|
|
||||||
$this->assertTrue(tenancy()->initialized);
|
$this->assertTrue(tenancy()->initialized);
|
||||||
$this->assertSame('acme', tenant('id'));
|
$this->assertSame('acme', tenant('id'));
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
class CombinedTenant extends Models\Tenant
|
|
||||||
{
|
|
||||||
use HasDomains;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Artisan;
|
use Illuminate\Support\Facades\Artisan;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
|
|
@ -19,12 +17,9 @@ use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class CommandsTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
return $event->tenant;
|
return $event->tenant;
|
||||||
})->toListener());
|
})->toListener());
|
||||||
|
|
@ -35,21 +30,16 @@ class CommandsTest extends TestCase
|
||||||
|
|
||||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
}
|
});
|
||||||
|
|
||||||
public function tearDown(): void
|
|
||||||
{
|
|
||||||
parent::tearDown();
|
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
// Cleanup tenancy config cache
|
// Cleanup tenancy config cache
|
||||||
if (file_exists(base_path('config/tenancy.php'))) {
|
if (file_exists(base_path('config/tenancy.php'))) {
|
||||||
unlink(base_path('config/tenancy.php'));
|
unlink(base_path('config/tenancy.php'));
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('migrate command doesnt change the db connection', function () {
|
||||||
public function migrate_command_doesnt_change_the_db_connection()
|
|
||||||
{
|
|
||||||
$this->assertFalse(Schema::hasTable('users'));
|
$this->assertFalse(Schema::hasTable('users'));
|
||||||
|
|
||||||
$old_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName();
|
$old_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName();
|
||||||
|
|
@ -59,11 +49,9 @@ class CommandsTest extends TestCase
|
||||||
$this->assertFalse(Schema::hasTable('users'));
|
$this->assertFalse(Schema::hasTable('users'));
|
||||||
$this->assertEquals($old_connection_name, $new_connection_name);
|
$this->assertEquals($old_connection_name, $new_connection_name);
|
||||||
$this->assertNotEquals('tenant', $new_connection_name);
|
$this->assertNotEquals('tenant', $new_connection_name);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('migrate command works without options', function () {
|
||||||
public function migrate_command_works_without_options()
|
|
||||||
{
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
$this->assertFalse(Schema::hasTable('users'));
|
$this->assertFalse(Schema::hasTable('users'));
|
||||||
|
|
@ -73,11 +61,9 @@ class CommandsTest extends TestCase
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
$this->assertTrue(Schema::hasTable('users'));
|
$this->assertTrue(Schema::hasTable('users'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('migrate command works with tenants option', function () {
|
||||||
public function migrate_command_works_with_tenants_option()
|
|
||||||
{
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
Artisan::call('tenants:migrate', [
|
Artisan::call('tenants:migrate', [
|
||||||
'--tenants' => [$tenant['id']],
|
'--tenants' => [$tenant['id']],
|
||||||
|
|
@ -89,11 +75,9 @@ class CommandsTest extends TestCase
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
$this->assertTrue(Schema::hasTable('users'));
|
$this->assertTrue(Schema::hasTable('users'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('migrate command loads schema state', function () {
|
||||||
public function migrate_command_loads_schema_state()
|
|
||||||
{
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
$this->assertFalse(Schema::hasTable('schema_users'));
|
$this->assertFalse(Schema::hasTable('schema_users'));
|
||||||
|
|
@ -109,11 +93,9 @@ class CommandsTest extends TestCase
|
||||||
// Check for both tables to see if missing migrations also get executed
|
// Check for both tables to see if missing migrations also get executed
|
||||||
$this->assertTrue(Schema::hasTable('schema_users'));
|
$this->assertTrue(Schema::hasTable('schema_users'));
|
||||||
$this->assertTrue(Schema::hasTable('users'));
|
$this->assertTrue(Schema::hasTable('users'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('dump command works', function () {
|
||||||
public function dump_command_works()
|
|
||||||
{
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
Artisan::call('tenants:migrate');
|
Artisan::call('tenants:migrate');
|
||||||
|
|
||||||
|
|
@ -121,11 +103,9 @@ class CommandsTest extends TestCase
|
||||||
|
|
||||||
Artisan::call('tenants:dump --path="tests/Etc/tenant-schema-test.dump"');
|
Artisan::call('tenants:dump --path="tests/Etc/tenant-schema-test.dump"');
|
||||||
$this->assertFileExists('tests/Etc/tenant-schema-test.dump');
|
$this->assertFileExists('tests/Etc/tenant-schema-test.dump');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('rollback command works', function () {
|
||||||
public function rollback_command_works()
|
|
||||||
{
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
Artisan::call('tenants:migrate');
|
Artisan::call('tenants:migrate');
|
||||||
$this->assertFalse(Schema::hasTable('users'));
|
$this->assertFalse(Schema::hasTable('users'));
|
||||||
|
|
@ -135,17 +115,13 @@ class CommandsTest extends TestCase
|
||||||
$this->assertTrue(Schema::hasTable('users'));
|
$this->assertTrue(Schema::hasTable('users'));
|
||||||
Artisan::call('tenants:rollback');
|
Artisan::call('tenants:rollback');
|
||||||
$this->assertFalse(Schema::hasTable('users'));
|
$this->assertFalse(Schema::hasTable('users'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('seed command works', function () {
|
||||||
public function seed_command_works()
|
|
||||||
{
|
|
||||||
$this->markTestIncomplete();
|
$this->markTestIncomplete();
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('database connection is switched to default', function () {
|
||||||
public function database_connection_is_switched_to_default()
|
|
||||||
{
|
|
||||||
$originalDBName = DB::connection()->getDatabaseName();
|
$originalDBName = DB::connection()->getDatabaseName();
|
||||||
|
|
||||||
Artisan::call('tenants:migrate');
|
Artisan::call('tenants:migrate');
|
||||||
|
|
@ -159,19 +135,15 @@ class CommandsTest extends TestCase
|
||||||
|
|
||||||
$this->run_commands_works();
|
$this->run_commands_works();
|
||||||
$this->assertSame($originalDBName, DB::connection()->getDatabaseName());
|
$this->assertSame($originalDBName, DB::connection()->getDatabaseName());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('database connection is switched to default when tenancy has been initialized', function () {
|
||||||
public function database_connection_is_switched_to_default_when_tenancy_has_been_initialized()
|
|
||||||
{
|
|
||||||
tenancy()->initialize(Tenant::create());
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
$this->database_connection_is_switched_to_default();
|
$this->database_connection_is_switched_to_default();
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('run commands works', function () {
|
||||||
public function run_commands_works()
|
|
||||||
{
|
|
||||||
$id = Tenant::create()->getTenantKey();
|
$id = Tenant::create()->getTenantKey();
|
||||||
|
|
||||||
Artisan::call('tenants:migrate', ['--tenants' => [$id]]);
|
Artisan::call('tenants:migrate', ['--tenants' => [$id]]);
|
||||||
|
|
@ -180,11 +152,9 @@ class CommandsTest extends TestCase
|
||||||
->expectsOutput("User's name is Test command")
|
->expectsOutput("User's name is Test command")
|
||||||
->expectsOutput('foo')
|
->expectsOutput('foo')
|
||||||
->expectsOutput('xyz');
|
->expectsOutput('xyz');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('install command works', function () {
|
||||||
public function install_command_works()
|
|
||||||
{
|
|
||||||
if (! is_dir($dir = app_path('Http'))) {
|
if (! is_dir($dir = app_path('Http'))) {
|
||||||
mkdir($dir, 0777, true);
|
mkdir($dir, 0777, true);
|
||||||
}
|
}
|
||||||
|
|
@ -199,11 +169,9 @@ class CommandsTest extends TestCase
|
||||||
$this->assertFileExists(database_path('migrations/2019_09_15_000010_create_tenants_table.php'));
|
$this->assertFileExists(database_path('migrations/2019_09_15_000010_create_tenants_table.php'));
|
||||||
$this->assertFileExists(database_path('migrations/2019_09_15_000020_create_domains_table.php'));
|
$this->assertFileExists(database_path('migrations/2019_09_15_000020_create_domains_table.php'));
|
||||||
$this->assertDirectoryExists(database_path('migrations/tenant'));
|
$this->assertDirectoryExists(database_path('migrations/tenant'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('migrate fresh command works', function () {
|
||||||
public function migrate_fresh_command_works()
|
|
||||||
{
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
$this->assertFalse(Schema::hasTable('users'));
|
$this->assertFalse(Schema::hasTable('users'));
|
||||||
|
|
@ -221,11 +189,9 @@ class CommandsTest extends TestCase
|
||||||
// test that db is wiped
|
// test that db is wiped
|
||||||
Artisan::call('tenants:migrate-fresh');
|
Artisan::call('tenants:migrate-fresh');
|
||||||
$this->assertFalse(DB::table('users')->exists());
|
$this->assertFalse(DB::table('users')->exists());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('run command with array of tenants works', function () {
|
||||||
public function run_command_with_array_of_tenants_works()
|
|
||||||
{
|
|
||||||
$tenantId1 = Tenant::create()->getTenantKey();
|
$tenantId1 = Tenant::create()->getTenantKey();
|
||||||
$tenantId2 = Tenant::create()->getTenantKey();
|
$tenantId2 = Tenant::create()->getTenantKey();
|
||||||
Artisan::call('tenants:migrate-fresh');
|
Artisan::call('tenants:migrate-fresh');
|
||||||
|
|
@ -233,5 +199,4 @@ class CommandsTest extends TestCase
|
||||||
$this->artisan("tenants:run foo --tenants=$tenantId1 --tenants=$tenantId2 --argument='a=foo' --option='b=bar' --option='c=xyz'")
|
$this->artisan("tenants:run foo --tenants=$tenantId1 --tenants=$tenantId2 --argument='a=foo' --option='b=bar' --option='c=xyz'")
|
||||||
->expectsOutput('Tenant: ' . $tenantId1)
|
->expectsOutput('Tenant: ' . $tenantId1)
|
||||||
->expectsOutput('Tenant: ' . $tenantId2);
|
->expectsOutput('Tenant: ' . $tenantId2);
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Database\Seeder;
|
use Illuminate\Database\Seeder;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticable;
|
use Illuminate\Foundation\Auth\User as Authenticable;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
@ -17,11 +15,9 @@ use Stancl\Tenancy\Jobs\SeedDatabase;
|
||||||
use Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager;
|
use Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class DatabasePreparationTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
/** @test */
|
test('database can be created after tenant creation', function () {
|
||||||
public function database_can_be_created_after_tenant_creation()
|
|
||||||
{
|
|
||||||
config(['tenancy.database.template_tenant_connection' => 'mysql']);
|
config(['tenancy.database.template_tenant_connection' => 'mysql']);
|
||||||
|
|
||||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
|
|
@ -34,11 +30,9 @@ class DatabasePreparationTest extends TestCase
|
||||||
$manager->setConnection('mysql');
|
$manager->setConnection('mysql');
|
||||||
|
|
||||||
$this->assertTrue($manager->databaseExists($tenant->database()->getName()));
|
$this->assertTrue($manager->databaseExists($tenant->database()->getName()));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('database can be migrated after tenant creation', function () {
|
||||||
public function database_can_be_migrated_after_tenant_creation()
|
|
||||||
{
|
|
||||||
Event::listen(TenantCreated::class, JobPipeline::make([
|
Event::listen(TenantCreated::class, JobPipeline::make([
|
||||||
CreateDatabase::class,
|
CreateDatabase::class,
|
||||||
MigrateDatabase::class,
|
MigrateDatabase::class,
|
||||||
|
|
@ -51,11 +45,9 @@ class DatabasePreparationTest extends TestCase
|
||||||
$tenant->run(function () {
|
$tenant->run(function () {
|
||||||
$this->assertTrue(Schema::hasTable('users'));
|
$this->assertTrue(Schema::hasTable('users'));
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('database can be seeded after tenant creation', function () {
|
||||||
public function database_can_be_seeded_after_tenant_creation()
|
|
||||||
{
|
|
||||||
config(['tenancy.seeder_parameters' => [
|
config(['tenancy.seeder_parameters' => [
|
||||||
'--class' => TestSeeder::class,
|
'--class' => TestSeeder::class,
|
||||||
]]);
|
]]);
|
||||||
|
|
@ -73,11 +65,9 @@ class DatabasePreparationTest extends TestCase
|
||||||
$tenant->run(function () {
|
$tenant->run(function () {
|
||||||
$this->assertSame('Seeded User', User::first()->name);
|
$this->assertSame('Seeded User', User::first()->name);
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('custom job can be added to the pipeline', function () {
|
||||||
public function custom_job_can_be_added_to_the_pipeline()
|
|
||||||
{
|
|
||||||
config(['tenancy.seeder_parameters' => [
|
config(['tenancy.seeder_parameters' => [
|
||||||
'--class' => TestSeeder::class,
|
'--class' => TestSeeder::class,
|
||||||
]]);
|
]]);
|
||||||
|
|
@ -96,44 +86,31 @@ class DatabasePreparationTest extends TestCase
|
||||||
$tenant->run(function () {
|
$tenant->run(function () {
|
||||||
$this->assertSame('Foo', User::all()[1]->name);
|
$this->assertSame('Foo', User::all()[1]->name);
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
class User extends Authenticable
|
// Helpers
|
||||||
{
|
/**
|
||||||
protected $guarded = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestSeeder extends Seeder
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Run the database seeds.
|
* Run the database seeds.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function run()
|
function run()
|
||||||
{
|
{
|
||||||
DB::table('users')->insert([
|
DB::table('users')->insert([
|
||||||
'name' => 'Seeded User',
|
'name' => 'Seeded User',
|
||||||
'email' => 'seeded@user',
|
'email' => 'seeded@user',
|
||||||
'password' => bcrypt('password'),
|
'password' => bcrypt('password'),
|
||||||
]);
|
]);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class CreateSuperuser
|
function __construct(Tenant $tenant)
|
||||||
{
|
{
|
||||||
protected $tenant;
|
test()->tenant = $tenant;
|
||||||
|
}
|
||||||
|
|
||||||
public function __construct(Tenant $tenant)
|
function handle()
|
||||||
{
|
{
|
||||||
$this->tenant = $tenant;
|
test()->tenant->run(function () {
|
||||||
}
|
|
||||||
|
|
||||||
public function handle()
|
|
||||||
{
|
|
||||||
$this->tenant->run(function () {
|
|
||||||
User::create(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']);
|
User::create(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
@ -20,12 +18,9 @@ use Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager;
|
||||||
use Stancl\Tenancy\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager;
|
use Stancl\Tenancy\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class DatabaseUsersTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
config([
|
config([
|
||||||
'tenancy.database.managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
'tenancy.database.managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
||||||
'tenancy.database.suffix' => '',
|
'tenancy.database.suffix' => '',
|
||||||
|
|
@ -35,11 +30,9 @@ class DatabaseUsersTest extends TestCase
|
||||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
return $event->tenant;
|
return $event->tenant;
|
||||||
})->toListener());
|
})->toListener());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('users are created when permission controlled mysql manager is used', function () {
|
||||||
public function users_are_created_when_permission_controlled_mysql_manager_is_used()
|
|
||||||
{
|
|
||||||
$tenant = new Tenant([
|
$tenant = new Tenant([
|
||||||
'id' => 'foo' . Str::random(10),
|
'id' => 'foo' . Str::random(10),
|
||||||
]);
|
]);
|
||||||
|
|
@ -52,11 +45,9 @@ class DatabaseUsersTest extends TestCase
|
||||||
$tenant->save();
|
$tenant->save();
|
||||||
|
|
||||||
$this->assertTrue($manager->userExists($tenant->database()->getUsername()));
|
$this->assertTrue($manager->userExists($tenant->database()->getUsername()));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('a tenants database cannot be created when the user already exists', function () {
|
||||||
public function a_tenants_database_cannot_be_created_when_the_user_already_exists()
|
|
||||||
{
|
|
||||||
$username = 'foo' . Str::random(8);
|
$username = 'foo' . Str::random(8);
|
||||||
$tenant = Tenant::create([
|
$tenant = Tenant::create([
|
||||||
'tenancy_db_username' => $username,
|
'tenancy_db_username' => $username,
|
||||||
|
|
@ -80,11 +71,9 @@ class DatabaseUsersTest extends TestCase
|
||||||
// database was not created because of DB transaction
|
// database was not created because of DB transaction
|
||||||
$this->assertFalse($manager2->databaseExists($tenant2->database()->getName()));
|
$this->assertFalse($manager2->databaseExists($tenant2->database()->getName()));
|
||||||
Event::assertNotDispatched(DatabaseCreated::class);
|
Event::assertNotDispatched(DatabaseCreated::class);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('correct grants are given to users', function () {
|
||||||
public function correct_grants_are_given_to_users()
|
|
||||||
{
|
|
||||||
PermissionControlledMySQLDatabaseManager::$grants = [
|
PermissionControlledMySQLDatabaseManager::$grants = [
|
||||||
'ALTER', 'ALTER ROUTINE', 'CREATE',
|
'ALTER', 'ALTER ROUTINE', 'CREATE',
|
||||||
];
|
];
|
||||||
|
|
@ -95,11 +84,9 @@ class DatabaseUsersTest extends TestCase
|
||||||
|
|
||||||
$query = DB::connection('mysql')->select("SHOW GRANTS FOR `{$tenant->database()->getUsername()}`@`%`")[1];
|
$query = DB::connection('mysql')->select("SHOW GRANTS FOR `{$tenant->database()->getUsername()}`@`%`")[1];
|
||||||
$this->assertStringStartsWith('GRANT CREATE, ALTER, ALTER ROUTINE ON', $query->{"Grants for {$user}@%"}); // @mysql because that's the hostname within the docker network
|
$this->assertStringStartsWith('GRANT CREATE, ALTER, ALTER ROUTINE ON', $query->{"Grants for {$user}@%"}); // @mysql because that's the hostname within the docker network
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('having existing databases without users and switching to permission controlled mysql manager doesnt break existing dbs', function () {
|
||||||
public function having_existing_databases_without_users_and_switching_to_permission_controlled_mysql_manager_doesnt_break_existing_dbs()
|
|
||||||
{
|
|
||||||
config([
|
config([
|
||||||
'tenancy.database.managers.mysql' => MySQLDatabaseManager::class,
|
'tenancy.database.managers.mysql' => MySQLDatabaseManager::class,
|
||||||
'tenancy.database.suffix' => '',
|
'tenancy.database.suffix' => '',
|
||||||
|
|
@ -126,5 +113,4 @@ class DatabaseUsersTest extends TestCase
|
||||||
|
|
||||||
$this->assertTrue($tenant->database()->manager() instanceof PermissionControlledMySQLDatabaseManager);
|
$this->assertTrue($tenant->database()->manager() instanceof PermissionControlledMySQLDatabaseManager);
|
||||||
$this->assertSame('root', config('database.connections.tenant.username'));
|
$this->assertSame('root', config('database.connections.tenant.username'));
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
||||||
use Stancl\Tenancy\Database\Models;
|
use Stancl\Tenancy\Database\Models;
|
||||||
|
|
@ -13,12 +11,9 @@ use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||||
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
|
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
|
||||||
|
|
||||||
class DomainTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
Route::group([
|
Route::group([
|
||||||
'middleware' => InitializeTenancyByDomain::class,
|
'middleware' => InitializeTenancyByDomain::class,
|
||||||
], function () {
|
], function () {
|
||||||
|
|
@ -28,11 +23,9 @@ class DomainTest extends TestCase
|
||||||
});
|
});
|
||||||
|
|
||||||
config(['tenancy.tenant_model' => DomainTenant::class]);
|
config(['tenancy.tenant_model' => DomainTenant::class]);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant can be identified using hostname', function () {
|
||||||
public function tenant_can_be_identified_using_hostname()
|
|
||||||
{
|
|
||||||
$tenant = DomainTenant::create();
|
$tenant = DomainTenant::create();
|
||||||
|
|
||||||
$id = $tenant->id;
|
$id = $tenant->id;
|
||||||
|
|
@ -45,11 +38,9 @@ class DomainTest extends TestCase
|
||||||
|
|
||||||
$this->assertSame($id, $resolvedTenant->id);
|
$this->assertSame($id, $resolvedTenant->id);
|
||||||
$this->assertSame(['foo.localhost'], $resolvedTenant->domains->pluck('domain')->toArray());
|
$this->assertSame(['foo.localhost'], $resolvedTenant->domains->pluck('domain')->toArray());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('a domain can belong to only one tenant', function () {
|
||||||
public function a_domain_can_belong_to_only_one_tenant()
|
|
||||||
{
|
|
||||||
$tenant = DomainTenant::create();
|
$tenant = DomainTenant::create();
|
||||||
|
|
||||||
$tenant->domains()->create([
|
$tenant->domains()->create([
|
||||||
|
|
@ -62,19 +53,15 @@ class DomainTest extends TestCase
|
||||||
$tenant2->domains()->create([
|
$tenant2->domains()->create([
|
||||||
'domain' => 'foo.localhost',
|
'domain' => 'foo.localhost',
|
||||||
]);
|
]);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('an exception is thrown if tenant cannot be identified', function () {
|
||||||
public function an_exception_is_thrown_if_tenant_cannot_be_identified()
|
|
||||||
{
|
|
||||||
$this->expectException(TenantCouldNotBeIdentifiedOnDomainException::class);
|
$this->expectException(TenantCouldNotBeIdentifiedOnDomainException::class);
|
||||||
|
|
||||||
app(DomainTenantResolver::class)->resolve('foo.localhost');
|
app(DomainTenantResolver::class)->resolve('foo.localhost');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant can be identified by domain', function () {
|
||||||
public function tenant_can_be_identified_by_domain()
|
|
||||||
{
|
|
||||||
$tenant = DomainTenant::create([
|
$tenant = DomainTenant::create([
|
||||||
'id' => 'acme',
|
'id' => 'acme',
|
||||||
]);
|
]);
|
||||||
|
|
@ -91,11 +78,9 @@ class DomainTest extends TestCase
|
||||||
|
|
||||||
$this->assertTrue(tenancy()->initialized);
|
$this->assertTrue(tenancy()->initialized);
|
||||||
$this->assertSame('acme', tenant('id'));
|
$this->assertSame('acme', tenant('id'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('onfail logic can be customized', function () {
|
||||||
public function onfail_logic_can_be_customized()
|
|
||||||
{
|
|
||||||
InitializeTenancyByDomain::$onFail = function () {
|
InitializeTenancyByDomain::$onFail = function () {
|
||||||
return 'foo';
|
return 'foo';
|
||||||
};
|
};
|
||||||
|
|
@ -103,11 +88,9 @@ class DomainTest extends TestCase
|
||||||
$this
|
$this
|
||||||
->get('http://foo.localhost/foo/abc/xyz')
|
->get('http://foo.localhost/foo/abc/xyz')
|
||||||
->assertSee('foo');
|
->assertSee('foo');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('domains are always lowercase', function () {
|
||||||
public function domains_are_always_lowercase()
|
|
||||||
{
|
|
||||||
$tenant = DomainTenant::create();
|
$tenant = DomainTenant::create();
|
||||||
|
|
||||||
$tenant->domains()->create([
|
$tenant->domains()->create([
|
||||||
|
|
@ -115,10 +98,4 @@ class DomainTest extends TestCase
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertSame('capitals', Domain::first()->domain);
|
$this->assertSame('capitals', Domain::first()->domain);
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
class DomainTenant extends Models\Tenant
|
|
||||||
{
|
|
||||||
use HasDomains;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Events\CallQueuedListener;
|
use Illuminate\Events\CallQueuedListener;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Illuminate\Support\Facades\Queue;
|
use Illuminate\Support\Facades\Queue;
|
||||||
|
|
@ -23,11 +21,9 @@ use Stancl\Tenancy\Listeners\QueueableListener;
|
||||||
use Stancl\Tenancy\Tenancy;
|
use Stancl\Tenancy\Tenancy;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class EventListenerTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
/** @test */
|
test('listeners can be synchronous', function () {
|
||||||
public function listeners_can_be_synchronous()
|
|
||||||
{
|
|
||||||
Queue::fake();
|
Queue::fake();
|
||||||
Event::listen(TenantCreated::class, FooListener::class);
|
Event::listen(TenantCreated::class, FooListener::class);
|
||||||
|
|
||||||
|
|
@ -36,11 +32,9 @@ class EventListenerTest extends TestCase
|
||||||
Queue::assertNothingPushed();
|
Queue::assertNothingPushed();
|
||||||
|
|
||||||
$this->assertSame('bar', app('foo'));
|
$this->assertSame('bar', app('foo'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('listeners can be queued by setting a static property', function () {
|
||||||
public function listeners_can_be_queued_by_setting_a_static_property()
|
|
||||||
{
|
|
||||||
Queue::fake();
|
Queue::fake();
|
||||||
|
|
||||||
Event::listen(TenantCreated::class, FooListener::class);
|
Event::listen(TenantCreated::class, FooListener::class);
|
||||||
|
|
@ -53,22 +47,18 @@ class EventListenerTest extends TestCase
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->assertFalse(app()->bound('foo'));
|
$this->assertFalse(app()->bound('foo'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('ing events can be used to cancel tenant model actions', function () {
|
||||||
public function ing_events_can_be_used_to_cancel_tenant_model_actions()
|
|
||||||
{
|
|
||||||
Event::listen(CreatingTenant::class, function () {
|
Event::listen(CreatingTenant::class, function () {
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->assertSame(false, Tenant::create()->exists);
|
$this->assertSame(false, Tenant::create()->exists);
|
||||||
$this->assertSame(0, Tenant::count());
|
$this->assertSame(0, Tenant::count());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('ing events can be used to cancel domain model actions', function () {
|
||||||
public function ing_events_can_be_used_to_cancel_domain_model_actions()
|
|
||||||
{
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
Event::listen(UpdatingDomain::class, function () {
|
Event::listen(UpdatingDomain::class, function () {
|
||||||
|
|
@ -84,11 +74,9 @@ class EventListenerTest extends TestCase
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertSame('acme', $domain->refresh()->domain);
|
$this->assertSame('acme', $domain->refresh()->domain);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('ing events can be used to cancel db creation', function () {
|
||||||
public function ing_events_can_be_used_to_cancel_db_creation()
|
|
||||||
{
|
|
||||||
Event::listen(CreatingDatabase::class, function (CreatingDatabase $event) {
|
Event::listen(CreatingDatabase::class, function (CreatingDatabase $event) {
|
||||||
$event->tenant->setInternal('create_database', false);
|
$event->tenant->setInternal('create_database', false);
|
||||||
});
|
});
|
||||||
|
|
@ -99,11 +87,9 @@ class EventListenerTest extends TestCase
|
||||||
$this->assertFalse($tenant->database()->manager()->databaseExists(
|
$this->assertFalse($tenant->database()->manager()->databaseExists(
|
||||||
$tenant->database()->getName()
|
$tenant->database()->getName()
|
||||||
));
|
));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('ing events can be used to cancel tenancy bootstrapping', function () {
|
||||||
public function ing_events_can_be_used_to_cancel_tenancy_bootstrapping()
|
|
||||||
{
|
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
DatabaseTenancyBootstrapper::class,
|
DatabaseTenancyBootstrapper::class,
|
||||||
RedisTenancyBootstrapper::class,
|
RedisTenancyBootstrapper::class,
|
||||||
|
|
@ -127,11 +113,9 @@ class EventListenerTest extends TestCase
|
||||||
tenancy()->initialize(Tenant::create());
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
$this->assertSame([DatabaseTenancyBootstrapper::class], array_map('get_class', tenancy()->getBootstrappers()));
|
$this->assertSame([DatabaseTenancyBootstrapper::class], array_map('get_class', tenancy()->getBootstrappers()));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('individual job pipelines can terminate while leaving others running', function () {
|
||||||
public function individual_job_pipelines_can_terminate_while_leaving_others_running()
|
|
||||||
{
|
|
||||||
$executed = [];
|
$executed = [];
|
||||||
|
|
||||||
Event::listen(
|
Event::listen(
|
||||||
|
|
@ -174,11 +158,9 @@ class EventListenerTest extends TestCase
|
||||||
'P2J1', // termminated after this
|
'P2J1', // termminated after this
|
||||||
// P2J2 was not reached
|
// P2J2 was not reached
|
||||||
], $executed);
|
], $executed);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('database is not migrated if creation is disabled', function () {
|
||||||
public function database_is_not_migrated_if_creation_is_disabled()
|
|
||||||
{
|
|
||||||
Event::listen(
|
Event::listen(
|
||||||
TenantCreated::class,
|
TenantCreated::class,
|
||||||
JobPipeline::make([
|
JobPipeline::make([
|
||||||
|
|
@ -198,15 +180,10 @@ class EventListenerTest extends TestCase
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertFalse($this->hasFailed());
|
$this->assertFalse($this->hasFailed());
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
class FooListener extends QueueableListener
|
// Helpers
|
||||||
|
function handle()
|
||||||
{
|
{
|
||||||
public static $shouldQueue = false;
|
|
||||||
|
|
||||||
public function handle()
|
|
||||||
{
|
|
||||||
app()->instance('foo', 'bar');
|
app()->instance('foo', 'bar');
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,18 +2,14 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests\Features;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use Stancl\Tenancy\Features\CrossDomainRedirect;
|
use Stancl\Tenancy\Features\CrossDomainRedirect;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
use Stancl\Tenancy\Tests\TestCase;
|
use Stancl\Tenancy\Tests\TestCase;
|
||||||
|
|
||||||
class RedirectTest extends TestCase
|
uses(TestCase::class);
|
||||||
{
|
|
||||||
/** @test */
|
test('tenant redirect macro replaces only the hostname', function () {
|
||||||
public function tenant_redirect_macro_replaces_only_the_hostname()
|
|
||||||
{
|
|
||||||
config([
|
config([
|
||||||
'tenancy.features' => [CrossDomainRedirect::class],
|
'tenancy.features' => [CrossDomainRedirect::class],
|
||||||
]);
|
]);
|
||||||
|
|
@ -31,16 +27,13 @@ class RedirectTest extends TestCase
|
||||||
|
|
||||||
$this->get('/redirect')
|
$this->get('/redirect')
|
||||||
->assertRedirect('http://abcd/foobar');
|
->assertRedirect('http://abcd/foobar');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant route helper generates correct url', function () {
|
||||||
public function tenant_route_helper_generates_correct_url()
|
|
||||||
{
|
|
||||||
Route::get('/abcdef/{a?}/{b?}', function () {
|
Route::get('/abcdef/{a?}/{b?}', function () {
|
||||||
return 'Foo';
|
return 'Foo';
|
||||||
})->name('foo');
|
})->name('foo');
|
||||||
|
|
||||||
$this->assertSame('http://foo.localhost/abcdef/as/df', tenant_route('foo.localhost', 'foo', ['a' => 'as', 'b' => 'df']));
|
$this->assertSame('http://foo.localhost/abcdef/as/df', tenant_route('foo.localhost', 'foo', ['a' => 'as', 'b' => 'df']));
|
||||||
$this->assertSame('http://foo.localhost/abcdef', tenant_route('foo.localhost', 'foo', []));
|
$this->assertSame('http://foo.localhost/abcdef', tenant_route('foo.localhost', 'foo', []));
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests\Features;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Stancl\Tenancy\Events\TenancyEnded;
|
use Stancl\Tenancy\Events\TenancyEnded;
|
||||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
|
|
@ -13,18 +11,13 @@ use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
use Stancl\Tenancy\Tests\TestCase;
|
use Stancl\Tenancy\Tests\TestCase;
|
||||||
|
|
||||||
class TenantConfigTest extends TestCase
|
uses(TestCase::class);
|
||||||
{
|
|
||||||
public function tearDown(): void
|
afterEach(function () {
|
||||||
{
|
|
||||||
TenantConfig::$storageToConfigMap = [];
|
TenantConfig::$storageToConfigMap = [];
|
||||||
|
});
|
||||||
|
|
||||||
parent::tearDown();
|
test('config is merged and removed', function () {
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function config_is_merged_and_removed()
|
|
||||||
{
|
|
||||||
$this->assertSame(null, config('services.paypal'));
|
$this->assertSame(null, config('services.paypal'));
|
||||||
config([
|
config([
|
||||||
'tenancy.features' => [TenantConfig::class],
|
'tenancy.features' => [TenantConfig::class],
|
||||||
|
|
@ -51,11 +44,9 @@ class TenantConfigTest extends TestCase
|
||||||
'public' => null,
|
'public' => null,
|
||||||
'private' => null,
|
'private' => null,
|
||||||
], config('services.paypal'));
|
], config('services.paypal'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('the value can be set to multiple config keys', function () {
|
||||||
public function the_value_can_be_set_to_multiple_config_keys()
|
|
||||||
{
|
|
||||||
$this->assertSame(null, config('services.paypal'));
|
$this->assertSame(null, config('services.paypal'));
|
||||||
config([
|
config([
|
||||||
'tenancy.features' => [TenantConfig::class],
|
'tenancy.features' => [TenantConfig::class],
|
||||||
|
|
@ -90,5 +81,4 @@ class TenantConfigTest extends TestCase
|
||||||
'public2' => null,
|
'public2' => null,
|
||||||
'private' => null,
|
'private' => null,
|
||||||
], config('services.paypal'));
|
], config('services.paypal'));
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Events\TenancyEnded;
|
use Stancl\Tenancy\Events\TenancyEnded;
|
||||||
|
|
@ -13,23 +11,18 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class GlobalCacheTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
CacheTenancyBootstrapper::class,
|
CacheTenancyBootstrapper::class,
|
||||||
]]);
|
]]);
|
||||||
|
|
||||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('global cache manager stores data in global cache', function () {
|
||||||
public function global_cache_manager_stores_data_in_global_cache()
|
|
||||||
{
|
|
||||||
$this->assertSame(null, cache('foo'));
|
$this->assertSame(null, cache('foo'));
|
||||||
GlobalCache::put(['foo' => 'bar'], 1);
|
GlobalCache::put(['foo' => 'bar'], 1);
|
||||||
$this->assertSame('bar', GlobalCache::get('foo'));
|
$this->assertSame('bar', GlobalCache::get('foo'));
|
||||||
|
|
@ -57,5 +50,4 @@ class GlobalCacheTest extends TestCase
|
||||||
|
|
||||||
tenancy()->initialize($tenant1);
|
tenancy()->initialize($tenant1);
|
||||||
$this->assertSame('ghi', cache('def'));
|
$this->assertSame('ghi', cache('def'));
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||||
use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException;
|
use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
@ -13,11 +11,9 @@ use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
|
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
|
||||||
|
|
||||||
class MaintenanceModeTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
/** @test */
|
test('tenant can be in maintenance mode', function () {
|
||||||
public function tenant_can_be_in_maintenance_mode()
|
|
||||||
{
|
|
||||||
Route::get('/foo', function () {
|
Route::get('/foo', function () {
|
||||||
return 'bar';
|
return 'bar';
|
||||||
})->middleware([InitializeTenancyByDomain::class, CheckTenantForMaintenanceMode::class]);
|
})->middleware([InitializeTenancyByDomain::class, CheckTenantForMaintenanceMode::class]);
|
||||||
|
|
@ -37,10 +33,4 @@ class MaintenanceModeTest extends TestCase
|
||||||
$this->expectException(HttpException::class);
|
$this->expectException(HttpException::class);
|
||||||
$this->withoutExceptionHandling()
|
$this->withoutExceptionHandling()
|
||||||
->get('http://acme.localhost/foo');
|
->get('http://acme.localhost/foo');
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
class MaintenanceTenant extends Tenant
|
|
||||||
{
|
|
||||||
use MaintenanceMode;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use Stancl\Tenancy\Exceptions\RouteIsMissingTenantParameterException;
|
use Stancl\Tenancy\Exceptions\RouteIsMissingTenantParameterException;
|
||||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByPathException;
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByPathException;
|
||||||
|
|
@ -11,12 +9,9 @@ use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
|
||||||
use Stancl\Tenancy\Resolvers\PathTenantResolver;
|
use Stancl\Tenancy\Resolvers\PathTenantResolver;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class PathIdentificationTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
PathTenantResolver::$tenantParameterName = 'tenant';
|
PathTenantResolver::$tenantParameterName = 'tenant';
|
||||||
|
|
||||||
Route::group([
|
Route::group([
|
||||||
|
|
@ -27,19 +22,14 @@ class PathIdentificationTest extends TestCase
|
||||||
return "$a + $b";
|
return "$a + $b";
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
public function tearDown(): void
|
|
||||||
{
|
|
||||||
parent::tearDown();
|
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
// Global state cleanup
|
// Global state cleanup
|
||||||
PathTenantResolver::$tenantParameterName = 'tenant';
|
PathTenantResolver::$tenantParameterName = 'tenant';
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant can be identified by path', function () {
|
||||||
public function tenant_can_be_identified_by_path()
|
|
||||||
{
|
|
||||||
Tenant::create([
|
Tenant::create([
|
||||||
'id' => 'acme',
|
'id' => 'acme',
|
||||||
]);
|
]);
|
||||||
|
|
@ -50,11 +40,9 @@ class PathIdentificationTest extends TestCase
|
||||||
|
|
||||||
$this->assertTrue(tenancy()->initialized);
|
$this->assertTrue(tenancy()->initialized);
|
||||||
$this->assertSame('acme', tenant('id'));
|
$this->assertSame('acme', tenant('id'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('route actions dont get the tenant id', function () {
|
||||||
public function route_actions_dont_get_the_tenant_id()
|
|
||||||
{
|
|
||||||
Tenant::create([
|
Tenant::create([
|
||||||
'id' => 'acme',
|
'id' => 'acme',
|
||||||
]);
|
]);
|
||||||
|
|
@ -67,11 +55,9 @@ class PathIdentificationTest extends TestCase
|
||||||
|
|
||||||
$this->assertTrue(tenancy()->initialized);
|
$this->assertTrue(tenancy()->initialized);
|
||||||
$this->assertSame('acme', tenant('id'));
|
$this->assertSame('acme', tenant('id'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('exception is thrown when tenant cannot be identified by path', function () {
|
||||||
public function exception_is_thrown_when_tenant_cannot_be_identified_by_path()
|
|
||||||
{
|
|
||||||
$this->expectException(TenantCouldNotBeIdentifiedByPathException::class);
|
$this->expectException(TenantCouldNotBeIdentifiedByPathException::class);
|
||||||
|
|
||||||
$this
|
$this
|
||||||
|
|
@ -79,11 +65,9 @@ class PathIdentificationTest extends TestCase
|
||||||
->get('/acme/foo/abc/xyz');
|
->get('/acme/foo/abc/xyz');
|
||||||
|
|
||||||
$this->assertFalse(tenancy()->initialized);
|
$this->assertFalse(tenancy()->initialized);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('onfail logic can be customized', function () {
|
||||||
public function onfail_logic_can_be_customized()
|
|
||||||
{
|
|
||||||
InitializeTenancyByPath::$onFail = function () {
|
InitializeTenancyByPath::$onFail = function () {
|
||||||
return 'foo';
|
return 'foo';
|
||||||
};
|
};
|
||||||
|
|
@ -91,11 +75,9 @@ class PathIdentificationTest extends TestCase
|
||||||
$this
|
$this
|
||||||
->get('/acme/foo/abc/xyz')
|
->get('/acme/foo/abc/xyz')
|
||||||
->assertContent('foo');
|
->assertContent('foo');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('an exception is thrown when the routes first parameter is not tenant', function () {
|
||||||
public function an_exception_is_thrown_when_the_routes_first_parameter_is_not_tenant()
|
|
||||||
{
|
|
||||||
Route::group([
|
Route::group([
|
||||||
// 'prefix' => '/{tenant}', -- intentionally commented
|
// 'prefix' => '/{tenant}', -- intentionally commented
|
||||||
'middleware' => InitializeTenancyByPath::class,
|
'middleware' => InitializeTenancyByPath::class,
|
||||||
|
|
@ -114,11 +96,9 @@ class PathIdentificationTest extends TestCase
|
||||||
$this
|
$this
|
||||||
->withoutExceptionHandling()
|
->withoutExceptionHandling()
|
||||||
->get('/bar/foo/bar');
|
->get('/bar/foo/bar');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant parameter name can be customized', function () {
|
||||||
public function tenant_parameter_name_can_be_customized()
|
|
||||||
{
|
|
||||||
PathTenantResolver::$tenantParameterName = 'team';
|
PathTenantResolver::$tenantParameterName = 'team';
|
||||||
|
|
||||||
Route::group([
|
Route::group([
|
||||||
|
|
@ -144,5 +124,4 @@ class PathIdentificationTest extends TestCase
|
||||||
$this
|
$this
|
||||||
->withoutExceptionHandling()
|
->withoutExceptionHandling()
|
||||||
->get('/acme/foo/abc/xyz');
|
->get('/acme/foo/abc/xyz');
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
@ -32,17 +30,9 @@ use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||||
|
|
||||||
class QueueTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public $mockConsoleOutput = false;
|
|
||||||
|
|
||||||
/** @var Valuestore */
|
|
||||||
protected $valuestore;
|
|
||||||
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
config([
|
config([
|
||||||
'tenancy.bootstrappers' => [
|
'tenancy.bootstrappers' => [
|
||||||
QueueTenancyBootstrapper::class,
|
QueueTenancyBootstrapper::class,
|
||||||
|
|
@ -54,65 +44,14 @@ class QueueTest extends TestCase
|
||||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
|
|
||||||
$this->createValueStore();
|
createValueStore();
|
||||||
}
|
});
|
||||||
|
|
||||||
public function tearDown(): void
|
afterEach(function () {
|
||||||
{
|
|
||||||
$this->valuestore->flush();
|
$this->valuestore->flush();
|
||||||
}
|
});
|
||||||
|
|
||||||
protected function createValueStore(): void
|
test('tenant id is passed to tenant queues', function () {
|
||||||
{
|
|
||||||
$valueStorePath = __DIR__ . '/Etc/tmp/queuetest.json';
|
|
||||||
|
|
||||||
if (! file_exists($valueStorePath)) {
|
|
||||||
// The directory sometimes goes missing as well when the file is deleted in git
|
|
||||||
if (! is_dir(__DIR__ . '/Etc/tmp')) {
|
|
||||||
mkdir(__DIR__ . '/Etc/tmp');
|
|
||||||
}
|
|
||||||
|
|
||||||
file_put_contents($valueStorePath, '');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->valuestore = Valuestore::make($valueStorePath)->flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function withFailedJobs()
|
|
||||||
{
|
|
||||||
Schema::connection('central')->create('failed_jobs', function (Blueprint $table) {
|
|
||||||
$table->increments('id');
|
|
||||||
$table->string('uuid')->unique();
|
|
||||||
$table->text('connection');
|
|
||||||
$table->text('queue');
|
|
||||||
$table->longText('payload');
|
|
||||||
$table->longText('exception');
|
|
||||||
$table->timestamp('failed_at')->useCurrent();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function withUsers()
|
|
||||||
{
|
|
||||||
Schema::create('users', function (Blueprint $table) {
|
|
||||||
$table->increments('id');
|
|
||||||
$table->string('name');
|
|
||||||
$table->string('email')->unique();
|
|
||||||
$table->string('password');
|
|
||||||
$table->rememberToken();
|
|
||||||
$table->timestamps();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function withTenantDatabases()
|
|
||||||
{
|
|
||||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
|
||||||
return $event->tenant;
|
|
||||||
})->toListener());
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function tenant_id_is_passed_to_tenant_queues()
|
|
||||||
{
|
|
||||||
config(['queue.default' => 'sync']);
|
config(['queue.default' => 'sync']);
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
@ -126,11 +65,9 @@ class QueueTest extends TestCase
|
||||||
Event::assertDispatched(JobProcessing::class, function ($event) {
|
Event::assertDispatched(JobProcessing::class, function ($event) {
|
||||||
return $event->job->payload()['tenant_id'] === tenant('id');
|
return $event->job->payload()['tenant_id'] === tenant('id');
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant id is not passed to central queues', function () {
|
||||||
public function tenant_id_is_not_passed_to_central_queues()
|
|
||||||
{
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
@ -147,24 +84,22 @@ class QueueTest extends TestCase
|
||||||
Event::assertDispatched(JobProcessing::class, function ($event) {
|
Event::assertDispatched(JobProcessing::class, function ($event) {
|
||||||
return ! isset($event->job->payload()['tenant_id']);
|
return ! isset($event->job->payload()['tenant_id']);
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
|
||||||
*
|
*
|
||||||
* @testWith [true]
|
* @testWith [true]
|
||||||
* [false]
|
* [false]
|
||||||
*/
|
*/
|
||||||
public function tenancy_is_initialized_inside_queues(bool $shouldEndTenancy)
|
test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
|
||||||
{
|
withTenantDatabases();
|
||||||
$this->withTenantDatabases();
|
withFailedJobs();
|
||||||
$this->withFailedJobs();
|
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
$this->withUsers();
|
withUsers();
|
||||||
|
|
||||||
$user = User::create(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']);
|
$user = User::create(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']);
|
||||||
|
|
||||||
|
|
@ -187,28 +122,26 @@ class QueueTest extends TestCase
|
||||||
$tenant->run(function () use ($user) {
|
$tenant->run(function () use ($user) {
|
||||||
$this->assertSame('Bar', $user->fresh()->name);
|
$this->assertSame('Bar', $user->fresh()->name);
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
|
||||||
*
|
*
|
||||||
* @testWith [true]
|
* @testWith [true]
|
||||||
* [false]
|
* [false]
|
||||||
*/
|
*/
|
||||||
public function tenancy_is_initialized_when_retrying_jobs(bool $shouldEndTenancy)
|
test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenancy) {
|
||||||
{
|
|
||||||
if (! Str::startsWith(app()->version(), '8')) {
|
if (! Str::startsWith(app()->version(), '8')) {
|
||||||
$this->markTestSkipped('queue:retry tenancy is only supported in Laravel 8');
|
$this->markTestSkipped('queue:retry tenancy is only supported in Laravel 8');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->withFailedJobs();
|
withFailedJobs();
|
||||||
$this->withTenantDatabases();
|
withTenantDatabases();
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
$this->withUsers();
|
withUsers();
|
||||||
|
|
||||||
$user = User::create(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']);
|
$user = User::create(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']);
|
||||||
|
|
||||||
|
|
@ -238,11 +171,9 @@ class QueueTest extends TestCase
|
||||||
$tenant->run(function () use ($user) {
|
$tenant->run(function () use ($user) {
|
||||||
$this->assertSame('Bar', $user->fresh()->name);
|
$this->assertSame('Bar', $user->fresh()->name);
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('the tenant used by the job doesnt change when the current tenant changes', function () {
|
||||||
public function the_tenant_used_by_the_job_doesnt_change_when_the_current_tenant_changes()
|
|
||||||
{
|
|
||||||
$tenant1 = Tenant::create([
|
$tenant1 = Tenant::create([
|
||||||
'id' => 'acme',
|
'id' => 'acme',
|
||||||
]);
|
]);
|
||||||
|
|
@ -261,41 +192,78 @@ class QueueTest extends TestCase
|
||||||
$this->artisan('queue:work --once');
|
$this->artisan('queue:work --once');
|
||||||
|
|
||||||
$this->assertSame('The current tenant id is: acme', $this->valuestore->get('tenant_id'));
|
$this->assertSame('The current tenant id is: acme', $this->valuestore->get('tenant_id'));
|
||||||
|
});
|
||||||
|
|
||||||
|
// Helpers
|
||||||
|
function createValueStore(): void
|
||||||
|
{
|
||||||
|
$valueStorePath = __DIR__ . '/Etc/tmp/queuetest.json';
|
||||||
|
|
||||||
|
if (! file_exists($valueStorePath)) {
|
||||||
|
// The directory sometimes goes missing as well when the file is deleted in git
|
||||||
|
if (! is_dir(__DIR__ . '/Etc/tmp')) {
|
||||||
|
mkdir(__DIR__ . '/Etc/tmp');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file_put_contents($valueStorePath, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
test()->valuestore = Valuestore::make($valueStorePath)->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestJob implements ShouldQueue
|
function withFailedJobs()
|
||||||
{
|
{
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
Schema::connection('central')->create('failed_jobs', function (Blueprint $table) {
|
||||||
|
$table->increments('id');
|
||||||
|
$table->string('uuid')->unique();
|
||||||
|
$table->text('connection');
|
||||||
|
$table->text('queue');
|
||||||
|
$table->longText('payload');
|
||||||
|
$table->longText('exception');
|
||||||
|
$table->timestamp('failed_at')->useCurrent();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/** @var Valuestore */
|
function withUsers()
|
||||||
protected $valuestore;
|
{
|
||||||
|
Schema::create('users', function (Blueprint $table) {
|
||||||
|
$table->increments('id');
|
||||||
|
$table->string('name');
|
||||||
|
$table->string('email')->unique();
|
||||||
|
$table->string('password');
|
||||||
|
$table->rememberToken();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/** @var User|null */
|
function withTenantDatabases()
|
||||||
protected $user;
|
{
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
}
|
||||||
|
|
||||||
public function __construct(Valuestore $valuestore, User $user = null)
|
function __construct(Valuestore $valuestore, User $user = null)
|
||||||
{
|
{
|
||||||
$this->valuestore = $valuestore;
|
test()->valuestore = $valuestore;
|
||||||
$this->user = $user;
|
test()->user = $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle()
|
function handle()
|
||||||
{
|
{
|
||||||
if ($this->valuestore->get('shouldFail')) {
|
if (test()->valuestore->get('shouldFail')) {
|
||||||
$this->valuestore->put('shouldFail', false);
|
test()->valuestore->put('shouldFail', false);
|
||||||
|
|
||||||
throw new Exception('failing');
|
throw new Exception('failing');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->user) {
|
if (test()->user) {
|
||||||
assert($this->user->getConnectionName() === 'tenant');
|
assert(test()->user->getConnectionName() === 'tenant');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->valuestore->put('tenant_id', 'The current tenant id is: ' . tenant('id'));
|
test()->valuestore->put('tenant_id', 'The current tenant id is: ' . tenant('id'));
|
||||||
|
|
||||||
if ($userName = $this->valuestore->get('userName')) {
|
if ($userName = test()->valuestore->get('userName')) {
|
||||||
$this->user->update(['name' => $userName]);
|
test()->user->update(['name' => $userName]);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,18 +2,13 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class RequestDataIdentificationTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
config([
|
config([
|
||||||
'tenancy.central_domains' => [
|
'tenancy.central_domains' => [
|
||||||
'localhost',
|
'localhost',
|
||||||
|
|
@ -23,19 +18,14 @@ class RequestDataIdentificationTest extends TestCase
|
||||||
Route::middleware(InitializeTenancyByRequestData::class)->get('/test', function () {
|
Route::middleware(InitializeTenancyByRequestData::class)->get('/test', function () {
|
||||||
return 'Tenant id: ' . tenant('id');
|
return 'Tenant id: ' . tenant('id');
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
public function tearDown(): void
|
afterEach(function () {
|
||||||
{
|
|
||||||
InitializeTenancyByRequestData::$header = 'X-Tenant';
|
InitializeTenancyByRequestData::$header = 'X-Tenant';
|
||||||
InitializeTenancyByRequestData::$queryParameter = 'tenant';
|
InitializeTenancyByRequestData::$queryParameter = 'tenant';
|
||||||
|
});
|
||||||
|
|
||||||
parent::tearDown();
|
test('header identification works', function () {
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function header_identification_works()
|
|
||||||
{
|
|
||||||
InitializeTenancyByRequestData::$header = 'X-Tenant';
|
InitializeTenancyByRequestData::$header = 'X-Tenant';
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
$tenant2 = Tenant::create();
|
$tenant2 = Tenant::create();
|
||||||
|
|
@ -46,11 +36,9 @@ class RequestDataIdentificationTest extends TestCase
|
||||||
'X-Tenant' => $tenant->id,
|
'X-Tenant' => $tenant->id,
|
||||||
])
|
])
|
||||||
->assertSee($tenant->id);
|
->assertSee($tenant->id);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('query parameter identification works', function () {
|
||||||
public function query_parameter_identification_works()
|
|
||||||
{
|
|
||||||
InitializeTenancyByRequestData::$header = null;
|
InitializeTenancyByRequestData::$header = null;
|
||||||
InitializeTenancyByRequestData::$queryParameter = 'tenant';
|
InitializeTenancyByRequestData::$queryParameter = 'tenant';
|
||||||
|
|
||||||
|
|
@ -61,5 +49,4 @@ class RequestDataIdentificationTest extends TestCase
|
||||||
->withoutExceptionHandling()
|
->withoutExceptionHandling()
|
||||||
->get('test?tenant=' . $tenant->id)
|
->get('test?tenant=' . $tenant->id)
|
||||||
->assertSee($tenant->id);
|
->assertSee($tenant->id);
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
use Illuminate\Events\CallQueuedListener;
|
use Illuminate\Events\CallQueuedListener;
|
||||||
|
|
@ -30,12 +28,9 @@ use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Listeners\UpdateSyncedResource;
|
use Stancl\Tenancy\Listeners\UpdateSyncedResource;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class ResourceSyncingTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
DatabaseTenancyBootstrapper::class,
|
DatabaseTenancyBootstrapper::class,
|
||||||
]]);
|
]]);
|
||||||
|
|
@ -61,19 +56,9 @@ class ResourceSyncingTest extends TestCase
|
||||||
],
|
],
|
||||||
'--realpath' => true,
|
'--realpath' => true,
|
||||||
])->assertExitCode(0);
|
])->assertExitCode(0);
|
||||||
}
|
});
|
||||||
|
|
||||||
protected function migrateTenants()
|
test('an event is triggered when a synced resource is changed', function () {
|
||||||
{
|
|
||||||
$this->artisan('tenants:migrate', [
|
|
||||||
'--path' => __DIR__ . '/Etc/synced_resource_migrations/users',
|
|
||||||
'--realpath' => true,
|
|
||||||
])->assertExitCode(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function an_event_is_triggered_when_a_synced_resource_is_changed()
|
|
||||||
{
|
|
||||||
Event::fake([SyncedResourceSaved::class]);
|
Event::fake([SyncedResourceSaved::class]);
|
||||||
|
|
||||||
$user = ResourceUser::create([
|
$user = ResourceUser::create([
|
||||||
|
|
@ -87,11 +72,9 @@ class ResourceSyncingTest extends TestCase
|
||||||
Event::assertDispatched(SyncedResourceSaved::class, function (SyncedResourceSaved $event) use ($user) {
|
Event::assertDispatched(SyncedResourceSaved::class, function (SyncedResourceSaved $event) use ($user) {
|
||||||
return $event->model === $user;
|
return $event->model === $user;
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('only the synced columns are updated in the central db', function () {
|
||||||
public function only_the_synced_columns_are_updated_in_the_central_db()
|
|
||||||
{
|
|
||||||
// Create user in central DB
|
// Create user in central DB
|
||||||
$user = CentralUser::create([
|
$user = CentralUser::create([
|
||||||
'global_id' => 'acme',
|
'global_id' => 'acme',
|
||||||
|
|
@ -102,7 +85,7 @@ class ResourceSyncingTest extends TestCase
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$tenant = ResourceTenant::create();
|
$tenant = ResourceTenant::create();
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
|
@ -143,16 +126,14 @@ class ResourceSyncingTest extends TestCase
|
||||||
'password' => 'secret', // no changes
|
'password' => 'secret', // no changes
|
||||||
'role' => 'superadmin', // unsynced
|
'role' => 'superadmin', // unsynced
|
||||||
], ResourceUser::first()->getAttributes());
|
], ResourceUser::first()->getAttributes());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('creating the resource in tenant database creates it in central database and creates the mapping', function () {
|
||||||
public function creating_the_resource_in_tenant_database_creates_it_in_central_database_and_creates_the_mapping()
|
|
||||||
{
|
|
||||||
// Assert no user in central DB
|
// Assert no user in central DB
|
||||||
$this->assertCount(0, ResourceUser::all());
|
$this->assertCount(0, ResourceUser::all());
|
||||||
|
|
||||||
$tenant = ResourceTenant::create();
|
$tenant = ResourceTenant::create();
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
|
@ -178,11 +159,9 @@ class ResourceSyncingTest extends TestCase
|
||||||
CentralUser::first()->update(['role' => 'central superadmin']);
|
CentralUser::first()->update(['role' => 'central superadmin']);
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
$this->assertSame('commenter', ResourceUser::first()->role);
|
$this->assertSame('commenter', ResourceUser::first()->role);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('trying to update synced resources from central context using tenant models results in an exception', function () {
|
||||||
public function trying_to_update_synced_resources_from_central_context_using_tenant_models_results_in_an_exception()
|
|
||||||
{
|
|
||||||
$this->creating_the_resource_in_tenant_database_creates_it_in_central_database_and_creates_the_mapping();
|
$this->creating_the_resource_in_tenant_database_creates_it_in_central_database_and_creates_the_mapping();
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
@ -190,11 +169,9 @@ class ResourceSyncingTest extends TestCase
|
||||||
|
|
||||||
$this->expectException(ModelNotSyncMasterException::class);
|
$this->expectException(ModelNotSyncMasterException::class);
|
||||||
ResourceUser::first()->update(['role' => 'foobar']);
|
ResourceUser::first()->update(['role' => 'foobar']);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('attaching a tenant to the central resource triggers a pull from the tenant db', function () {
|
||||||
public function attaching_a_tenant_to_the_central_resource_triggers_a_pull_from_the_tenant_db()
|
|
||||||
{
|
|
||||||
$centralUser = CentralUser::create([
|
$centralUser = CentralUser::create([
|
||||||
'global_id' => 'acme',
|
'global_id' => 'acme',
|
||||||
'name' => 'John Doe',
|
'name' => 'John Doe',
|
||||||
|
|
@ -206,7 +183,7 @@ class ResourceSyncingTest extends TestCase
|
||||||
$tenant = ResourceTenant::create([
|
$tenant = ResourceTenant::create([
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
]);
|
]);
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
|
|
||||||
$tenant->run(function () {
|
$tenant->run(function () {
|
||||||
$this->assertCount(0, ResourceUser::all());
|
$this->assertCount(0, ResourceUser::all());
|
||||||
|
|
@ -217,11 +194,9 @@ class ResourceSyncingTest extends TestCase
|
||||||
$tenant->run(function () {
|
$tenant->run(function () {
|
||||||
$this->assertCount(1, ResourceUser::all());
|
$this->assertCount(1, ResourceUser::all());
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('attaching users to tenants d o e s n o t d o a n y t h i n g', function () {
|
||||||
public function attaching_users_to_tenants_DOES_NOT_DO_ANYTHING()
|
|
||||||
{
|
|
||||||
$centralUser = CentralUser::create([
|
$centralUser = CentralUser::create([
|
||||||
'global_id' => 'acme',
|
'global_id' => 'acme',
|
||||||
'name' => 'John Doe',
|
'name' => 'John Doe',
|
||||||
|
|
@ -233,7 +208,7 @@ class ResourceSyncingTest extends TestCase
|
||||||
$tenant = ResourceTenant::create([
|
$tenant = ResourceTenant::create([
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
]);
|
]);
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
|
|
||||||
$tenant->run(function () {
|
$tenant->run(function () {
|
||||||
$this->assertCount(0, ResourceUser::all());
|
$this->assertCount(0, ResourceUser::all());
|
||||||
|
|
@ -246,11 +221,9 @@ class ResourceSyncingTest extends TestCase
|
||||||
// Still zero
|
// Still zero
|
||||||
$this->assertCount(0, ResourceUser::all());
|
$this->assertCount(0, ResourceUser::all());
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('resources are synced only to workspaces that have the resource', function () {
|
||||||
public function resources_are_synced_only_to_workspaces_that_have_the_resource()
|
|
||||||
{
|
|
||||||
$centralUser = CentralUser::create([
|
$centralUser = CentralUser::create([
|
||||||
'global_id' => 'acme',
|
'global_id' => 'acme',
|
||||||
'name' => 'John Doe',
|
'name' => 'John Doe',
|
||||||
|
|
@ -270,7 +243,7 @@ class ResourceSyncingTest extends TestCase
|
||||||
$t3 = ResourceTenant::create([
|
$t3 = ResourceTenant::create([
|
||||||
'id' => 't3',
|
'id' => 't3',
|
||||||
]);
|
]);
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
|
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
$centralUser->tenants()->attach('t2');
|
$centralUser->tenants()->attach('t2');
|
||||||
|
|
@ -290,11 +263,9 @@ class ResourceSyncingTest extends TestCase
|
||||||
// assert user does NOT exist
|
// assert user does NOT exist
|
||||||
$this->assertCount(0, ResourceUser::all());
|
$this->assertCount(0, ResourceUser::all());
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('when a resource exists in other tenant dbs but is c r e a t e d in a tenant db the synced columns are updated in the other dbs', function () {
|
||||||
public function when_a_resource_exists_in_other_tenant_dbs_but_is_CREATED_in_a_tenant_db_the_synced_columns_are_updated_in_the_other_dbs()
|
|
||||||
{
|
|
||||||
// create shared resource
|
// create shared resource
|
||||||
$centralUser = CentralUser::create([
|
$centralUser = CentralUser::create([
|
||||||
'global_id' => 'acme',
|
'global_id' => 'acme',
|
||||||
|
|
@ -310,7 +281,7 @@ class ResourceSyncingTest extends TestCase
|
||||||
$t2 = ResourceTenant::create([
|
$t2 = ResourceTenant::create([
|
||||||
'id' => 't2',
|
'id' => 't2',
|
||||||
]);
|
]);
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
|
|
||||||
// Copy (cascade) user to t1 DB
|
// Copy (cascade) user to t1 DB
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
@ -337,11 +308,9 @@ class ResourceSyncingTest extends TestCase
|
||||||
$this->assertSame('john@foo', $user->email); // email changed
|
$this->assertSame('john@foo', $user->email); // email changed
|
||||||
$this->assertSame('commenter', $user->role); // role didn't change, i.e. is the same as from the original copy from central
|
$this->assertSame('commenter', $user->role); // role didn't change, i.e. is the same as from the original copy from central
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('the synced columns are updated in other tenant dbs where the resource exists', function () {
|
||||||
public function the_synced_columns_are_updated_in_other_tenant_dbs_where_the_resource_exists()
|
|
||||||
{
|
|
||||||
// create shared resource
|
// create shared resource
|
||||||
$centralUser = CentralUser::create([
|
$centralUser = CentralUser::create([
|
||||||
'global_id' => 'acme',
|
'global_id' => 'acme',
|
||||||
|
|
@ -360,7 +329,7 @@ class ResourceSyncingTest extends TestCase
|
||||||
$t3 = ResourceTenant::create([
|
$t3 = ResourceTenant::create([
|
||||||
'id' => 't3',
|
'id' => 't3',
|
||||||
]);
|
]);
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
|
|
||||||
// Copy (cascade) user to t1 DB
|
// Copy (cascade) user to t1 DB
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
@ -390,11 +359,9 @@ class ResourceSyncingTest extends TestCase
|
||||||
$centralUser = CentralUser::first();
|
$centralUser = CentralUser::first();
|
||||||
$this->assertSame('John 3', $centralUser->name); // synced
|
$this->assertSame('John 3', $centralUser->name); // synced
|
||||||
$this->assertSame('commenter', $centralUser->role); // unsynced
|
$this->assertSame('commenter', $centralUser->role); // unsynced
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('global id is generated using id generator when its not supplied', function () {
|
||||||
public function global_id_is_generated_using_id_generator_when_its_not_supplied()
|
|
||||||
{
|
|
||||||
$user = CentralUser::create([
|
$user = CentralUser::create([
|
||||||
'name' => 'John Doe',
|
'name' => 'John Doe',
|
||||||
'email' => 'john@doe',
|
'email' => 'john@doe',
|
||||||
|
|
@ -403,11 +370,9 @@ class ResourceSyncingTest extends TestCase
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertNotNull($user->global_id);
|
$this->assertNotNull($user->global_id);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('when the resource doesnt exist in the tenant db non synced columns will cascade too', function () {
|
||||||
public function when_the_resource_doesnt_exist_in_the_tenant_db_non_synced_columns_will_cascade_too()
|
|
||||||
{
|
|
||||||
$centralUser = CentralUser::create([
|
$centralUser = CentralUser::create([
|
||||||
'name' => 'John Doe',
|
'name' => 'John Doe',
|
||||||
'email' => 'john@doe',
|
'email' => 'john@doe',
|
||||||
|
|
@ -419,23 +384,21 @@ class ResourceSyncingTest extends TestCase
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
|
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
||||||
$t1->run(function () {
|
$t1->run(function () {
|
||||||
$this->assertSame('employee', ResourceUser::first()->role);
|
$this->assertSame('employee', ResourceUser::first()->role);
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('when the resource doesnt exist in the central db non synced columns will bubble up too', function () {
|
||||||
public function when_the_resource_doesnt_exist_in_the_central_db_non_synced_columns_will_bubble_up_too()
|
|
||||||
{
|
|
||||||
$t1 = ResourceTenant::create([
|
$t1 = ResourceTenant::create([
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
|
|
||||||
$t1->run(function () {
|
$t1->run(function () {
|
||||||
ResourceUser::create([
|
ResourceUser::create([
|
||||||
|
|
@ -447,11 +410,9 @@ class ResourceSyncingTest extends TestCase
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->assertSame('employee', CentralUser::first()->role);
|
$this->assertSame('employee', CentralUser::first()->role);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('the listener can be queued', function () {
|
||||||
public function the_listener_can_be_queued()
|
|
||||||
{
|
|
||||||
Queue::fake();
|
Queue::fake();
|
||||||
UpdateSyncedResource::$shouldQueue = true;
|
UpdateSyncedResource::$shouldQueue = true;
|
||||||
|
|
||||||
|
|
@ -459,7 +420,7 @@ class ResourceSyncingTest extends TestCase
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
|
|
||||||
Queue::assertNothingPushed();
|
Queue::assertNothingPushed();
|
||||||
|
|
||||||
|
|
@ -475,11 +436,9 @@ class ResourceSyncingTest extends TestCase
|
||||||
Queue::assertPushed(CallQueuedListener::class, function (CallQueuedListener $job) {
|
Queue::assertPushed(CallQueuedListener::class, function (CallQueuedListener $job) {
|
||||||
return $job->class === UpdateSyncedResource::class;
|
return $job->class === UpdateSyncedResource::class;
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('an event is fired for all touched resources', function () {
|
||||||
public function an_event_is_fired_for_all_touched_resources()
|
|
||||||
{
|
|
||||||
Event::fake([SyncedResourceChangedInForeignDatabase::class]);
|
Event::fake([SyncedResourceChangedInForeignDatabase::class]);
|
||||||
|
|
||||||
// create shared resource
|
// create shared resource
|
||||||
|
|
@ -500,7 +459,7 @@ class ResourceSyncingTest extends TestCase
|
||||||
$t3 = ResourceTenant::create([
|
$t3 = ResourceTenant::create([
|
||||||
'id' => 't3',
|
'id' => 't3',
|
||||||
]);
|
]);
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
|
|
||||||
// Copy (cascade) user to t1 DB
|
// Copy (cascade) user to t1 DB
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
@ -572,91 +531,54 @@ class ResourceSyncingTest extends TestCase
|
||||||
Event::assertNotDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) {
|
Event::assertNotDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) {
|
||||||
return $event->tenant === null;
|
return $event->tenant === null;
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
|
// Helpers
|
||||||
|
function migrateTenants()
|
||||||
|
{
|
||||||
|
test()->artisan('tenants:migrate', [
|
||||||
|
'--path' => __DIR__ . '/Etc/synced_resource_migrations/users',
|
||||||
|
'--realpath' => true,
|
||||||
|
])->assertExitCode(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
class ResourceTenant extends Tenant
|
function users()
|
||||||
{
|
{
|
||||||
public function users()
|
return test()->belongsToMany(CentralUser::class, 'tenant_users', 'tenant_id', 'global_user_id', 'id', 'global_id')
|
||||||
{
|
|
||||||
return $this->belongsToMany(CentralUser::class, 'tenant_users', 'tenant_id', 'global_user_id', 'id', 'global_id')
|
|
||||||
->using(TenantPivot::class);
|
->using(TenantPivot::class);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class CentralUser extends Model implements SyncMaster
|
function tenants(): BelongsToMany
|
||||||
{
|
{
|
||||||
use ResourceSyncing, CentralConnection;
|
return test()->belongsToMany(ResourceTenant::class, 'tenant_users', 'global_user_id', 'tenant_id', 'global_id')
|
||||||
|
|
||||||
protected $guarded = [];
|
|
||||||
public $timestamps = false;
|
|
||||||
public $table = 'users';
|
|
||||||
|
|
||||||
public function tenants(): BelongsToMany
|
|
||||||
{
|
|
||||||
return $this->belongsToMany(ResourceTenant::class, 'tenant_users', 'global_user_id', 'tenant_id', 'global_id')
|
|
||||||
->using(TenantPivot::class);
|
->using(TenantPivot::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTenantModelName(): string
|
function getTenantModelName(): string
|
||||||
{
|
{
|
||||||
return ResourceUser::class;
|
return ResourceUser::class;
|
||||||
}
|
|
||||||
|
|
||||||
public function getGlobalIdentifierKey()
|
|
||||||
{
|
|
||||||
return $this->getAttribute($this->getGlobalIdentifierKeyName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getGlobalIdentifierKeyName(): string
|
|
||||||
{
|
|
||||||
return 'global_id';
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getCentralModelName(): string
|
|
||||||
{
|
|
||||||
return static::class;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getSyncedAttributeNames(): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'name',
|
|
||||||
'password',
|
|
||||||
'email',
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class ResourceUser extends Model implements Syncable
|
function getGlobalIdentifierKey()
|
||||||
{
|
{
|
||||||
use ResourceSyncing;
|
return test()->getAttribute(test()->getGlobalIdentifierKeyName());
|
||||||
|
}
|
||||||
|
|
||||||
protected $table = 'users';
|
function getGlobalIdentifierKeyName(): string
|
||||||
protected $guarded = [];
|
{
|
||||||
public $timestamps = false;
|
|
||||||
|
|
||||||
public function getGlobalIdentifierKey()
|
|
||||||
{
|
|
||||||
return $this->getAttribute($this->getGlobalIdentifierKeyName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getGlobalIdentifierKeyName(): string
|
|
||||||
{
|
|
||||||
return 'global_id';
|
return 'global_id';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCentralModelName(): string
|
function getCentralModelName(): string
|
||||||
{
|
{
|
||||||
return CentralUser::class;
|
return CentralUser::class;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSyncedAttributeNames(): array
|
function getSyncedAttributeNames(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'name',
|
'name',
|
||||||
'password',
|
'password',
|
||||||
'email',
|
'email',
|
||||||
];
|
];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Session\Middleware\StartSession;
|
use Illuminate\Session\Middleware\StartSession;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
@ -13,12 +11,9 @@ use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
|
||||||
use Stancl\Tenancy\Middleware\ScopeSessions;
|
use Stancl\Tenancy\Middleware\ScopeSessions;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class ScopeSessionsTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
Route::group([
|
Route::group([
|
||||||
'middleware' => [StartSession::class, InitializeTenancyBySubdomain::class, ScopeSessions::class],
|
'middleware' => [StartSession::class, InitializeTenancyBySubdomain::class, ScopeSessions::class],
|
||||||
], function () {
|
], function () {
|
||||||
|
|
@ -35,22 +30,18 @@ class ScopeSessionsTest extends TestCase
|
||||||
'domain' => $tenant->id,
|
'domain' => $tenant->id,
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant id is auto added to session if its missing', function () {
|
||||||
public function tenant_id_is_auto_added_to_session_if_its_missing()
|
|
||||||
{
|
|
||||||
$tenant = Tenant::create([
|
$tenant = Tenant::create([
|
||||||
'id' => 'acme',
|
'id' => 'acme',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->get('http://acme.localhost/foo')
|
$this->get('http://acme.localhost/foo')
|
||||||
->assertSessionHas(ScopeSessions::$tenantIdKey, 'acme');
|
->assertSessionHas(ScopeSessions::$tenantIdKey, 'acme');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('changing tenant id in session will abort the request', function () {
|
||||||
public function changing_tenant_id_in_session_will_abort_the_request()
|
|
||||||
{
|
|
||||||
$tenant = Tenant::create([
|
$tenant = Tenant::create([
|
||||||
'id' => 'acme',
|
'id' => 'acme',
|
||||||
]);
|
]);
|
||||||
|
|
@ -62,11 +53,9 @@ class ScopeSessionsTest extends TestCase
|
||||||
|
|
||||||
$this->get('http://acme.localhost/foo')
|
$this->get('http://acme.localhost/foo')
|
||||||
->assertStatus(403);
|
->assertStatus(403);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('an exception is thrown when the middleware is executed before tenancy is initialized', function () {
|
||||||
public function an_exception_is_thrown_when_the_middleware_is_executed_before_tenancy_is_initialized()
|
|
||||||
{
|
|
||||||
Route::get('/bar', function () {
|
Route::get('/bar', function () {
|
||||||
return true;
|
return true;
|
||||||
})->middleware([StartSession::class, ScopeSessions::class]);
|
})->middleware([StartSession::class, ScopeSessions::class]);
|
||||||
|
|
@ -77,5 +66,4 @@ class ScopeSessionsTest extends TestCase
|
||||||
|
|
||||||
$this->expectException(TenancyNotInitializedException::class);
|
$this->expectException(TenancyNotInitializedException::class);
|
||||||
$this->withoutExceptionHandling()->get('http://acme.localhost/bar');
|
$this->withoutExceptionHandling()->get('http://acme.localhost/bar');
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\QueryException;
|
use Illuminate\Database\QueryException;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
|
@ -12,14 +10,11 @@ use Illuminate\Support\Facades\Validator;
|
||||||
use Stancl\Tenancy\Database\Concerns\BelongsToPrimaryModel;
|
use Stancl\Tenancy\Database\Concerns\BelongsToPrimaryModel;
|
||||||
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
||||||
use Stancl\Tenancy\Database\Concerns\HasScopedValidationRules;
|
use Stancl\Tenancy\Database\Concerns\HasScopedValidationRules;
|
||||||
|
|
||||||
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant as TestTenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant as TestTenant;
|
||||||
|
|
||||||
class SingleDatabaseTenancyTest extends TestCase
|
beforeEach(function () {
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
BelongsToTenant::$tenantIdColumn = 'tenant_id';
|
BelongsToTenant::$tenantIdColumn = 'tenant_id';
|
||||||
|
|
||||||
Schema::create('posts', function (Blueprint $table) {
|
Schema::create('posts', function (Blueprint $table) {
|
||||||
|
|
@ -41,11 +36,9 @@ class SingleDatabaseTenancyTest extends TestCase
|
||||||
});
|
});
|
||||||
|
|
||||||
config(['tenancy.tenant_model' => Tenant::class]);
|
config(['tenancy.tenant_model' => Tenant::class]);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('primary models are scoped to the current tenant', function () {
|
||||||
public function primary_models_are_scoped_to_the_current_tenant()
|
|
||||||
{
|
|
||||||
// acme context
|
// acme context
|
||||||
tenancy()->initialize($acme = Tenant::create([
|
tenancy()->initialize($acme = Tenant::create([
|
||||||
'id' => 'acme',
|
'id' => 'acme',
|
||||||
|
|
@ -88,21 +81,17 @@ class SingleDatabaseTenancyTest extends TestCase
|
||||||
|
|
||||||
// Assert foobar models are inaccessible in acme context
|
// Assert foobar models are inaccessible in acme context
|
||||||
$this->assertSame(1, Post::count());
|
$this->assertSame(1, Post::count());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('primary models are not scoped in the central context', function () {
|
||||||
public function primary_models_are_not_scoped_in_the_central_context()
|
|
||||||
{
|
|
||||||
$this->primary_models_are_scoped_to_the_current_tenant();
|
$this->primary_models_are_scoped_to_the_current_tenant();
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
$this->assertSame(2, Post::count());
|
$this->assertSame(2, Post::count());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('secondary models are scoped to the current tenant when accessed via primary model', function () {
|
||||||
public function secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model()
|
|
||||||
{
|
|
||||||
// acme context
|
// acme context
|
||||||
tenancy()->initialize($acme = Tenant::create([
|
tenancy()->initialize($acme = Tenant::create([
|
||||||
'id' => 'acme',
|
'id' => 'acme',
|
||||||
|
|
@ -125,22 +114,18 @@ class SingleDatabaseTenancyTest extends TestCase
|
||||||
tenancy()->initialize($acme);
|
tenancy()->initialize($acme);
|
||||||
$this->assertSame(1, Post::count());
|
$this->assertSame(1, Post::count());
|
||||||
$this->assertSame(1, Post::first()->comments->count());
|
$this->assertSame(1, Post::first()->comments->count());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('secondary models are n o t scoped to the current tenant when accessed directly', function () {
|
||||||
public function secondary_models_are_NOT_scoped_to_the_current_tenant_when_accessed_directly()
|
|
||||||
{
|
|
||||||
$this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model();
|
$this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model();
|
||||||
|
|
||||||
// We're in acme context
|
// We're in acme context
|
||||||
$this->assertSame('acme', tenant('id'));
|
$this->assertSame('acme', tenant('id'));
|
||||||
|
|
||||||
$this->assertSame(2, Comment::count());
|
$this->assertSame(2, Comment::count());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('secondary models a r e scoped to the current tenant when accessed directly a n d p a r e n t r e l a t i o n s h i p t r a i t i s u s e d', function () {
|
||||||
public function secondary_models_ARE_scoped_to_the_current_tenant_when_accessed_directly_AND_PARENT_RELATIONSHIP_TRAIT_IS_USED()
|
|
||||||
{
|
|
||||||
$acme = Tenant::create([
|
$acme = Tenant::create([
|
||||||
'id' => 'acme',
|
'id' => 'acme',
|
||||||
]);
|
]);
|
||||||
|
|
@ -170,21 +155,17 @@ class SingleDatabaseTenancyTest extends TestCase
|
||||||
|
|
||||||
// Global context
|
// Global context
|
||||||
$this->assertSame(2, ScopedComment::count());
|
$this->assertSame(2, ScopedComment::count());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('secondary models are n o t scoped in the central context', function () {
|
||||||
public function secondary_models_are_NOT_scoped_in_the_central_context()
|
|
||||||
{
|
|
||||||
$this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model();
|
$this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model();
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
$this->assertSame(2, Comment::count());
|
$this->assertSame(2, Comment::count());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('global models are not scoped at all', function () {
|
||||||
public function global_models_are_not_scoped_at_all()
|
|
||||||
{
|
|
||||||
Schema::create('global_resources', function (Blueprint $table) {
|
Schema::create('global_resources', function (Blueprint $table) {
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
$table->string('text');
|
$table->string('text');
|
||||||
|
|
@ -205,11 +186,9 @@ class SingleDatabaseTenancyTest extends TestCase
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->assertSame(4, GlobalResource::count());
|
$this->assertSame(4, GlobalResource::count());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant id and relationship is auto added when creating primary resources in tenant context', function () {
|
||||||
public function tenant_id_and_relationship_is_auto_added_when_creating_primary_resources_in_tenant_context()
|
|
||||||
{
|
|
||||||
tenancy()->initialize($acme = Tenant::create([
|
tenancy()->initialize($acme = Tenant::create([
|
||||||
'id' => 'acme',
|
'id' => 'acme',
|
||||||
]));
|
]));
|
||||||
|
|
@ -220,19 +199,15 @@ class SingleDatabaseTenancyTest extends TestCase
|
||||||
$this->assertTrue($post->relationLoaded('tenant'));
|
$this->assertTrue($post->relationLoaded('tenant'));
|
||||||
$this->assertSame($acme, $post->tenant);
|
$this->assertSame($acme, $post->tenant);
|
||||||
$this->assertSame(tenant(), $post->tenant);
|
$this->assertSame(tenant(), $post->tenant);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant id is not auto added when creating primary resources in central context', function () {
|
||||||
public function tenant_id_is_not_auto_added_when_creating_primary_resources_in_central_context()
|
|
||||||
{
|
|
||||||
$this->expectException(QueryException::class);
|
$this->expectException(QueryException::class);
|
||||||
|
|
||||||
Post::create(['text' => 'Foo']);
|
Post::create(['text' => 'Foo']);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant id column name can be customized', function () {
|
||||||
public function tenant_id_column_name_can_be_customized()
|
|
||||||
{
|
|
||||||
BelongsToTenant::$tenantIdColumn = 'team_id';
|
BelongsToTenant::$tenantIdColumn = 'team_id';
|
||||||
|
|
||||||
Schema::drop('comments');
|
Schema::drop('comments');
|
||||||
|
|
@ -278,11 +253,9 @@ class SingleDatabaseTenancyTest extends TestCase
|
||||||
|
|
||||||
// Assert foobar models are inaccessible in acme context
|
// Assert foobar models are inaccessible in acme context
|
||||||
$this->assertSame(1, Post::count());
|
$this->assertSame(1, Post::count());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('the model returned by the tenant helper has unique and exists validation rules', function () {
|
||||||
public function the_model_returned_by_the_tenant_helper_has_unique_and_exists_validation_rules()
|
|
||||||
{
|
|
||||||
Schema::table('posts', function (Blueprint $table) {
|
Schema::table('posts', function (Blueprint $table) {
|
||||||
$table->string('slug')->nullable();
|
$table->string('slug')->nullable();
|
||||||
$table->unique(['tenant_id', 'slug']);
|
$table->unique(['tenant_id', 'slug']);
|
||||||
|
|
@ -316,57 +289,25 @@ class SingleDatabaseTenancyTest extends TestCase
|
||||||
// Assert that tenant()->unique() and tenant()->exists() are scoped
|
// Assert that tenant()->unique() and tenant()->exists() are scoped
|
||||||
$this->assertTrue($uniqueFails);
|
$this->assertTrue($uniqueFails);
|
||||||
$this->assertFalse($existsFails);
|
$this->assertFalse($existsFails);
|
||||||
}
|
});
|
||||||
|
|
||||||
|
// Helpers
|
||||||
|
function comments()
|
||||||
|
{
|
||||||
|
return test()->hasMany(Comment::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
class Tenant extends TestTenant
|
function scoped_comments()
|
||||||
{
|
{
|
||||||
use HasScopedValidationRules;
|
return test()->hasMany(Comment::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
class Post extends Model
|
function post()
|
||||||
{
|
{
|
||||||
use BelongsToTenant;
|
return test()->belongsTo(Post::class);
|
||||||
|
|
||||||
protected $guarded = [];
|
|
||||||
public $timestamps = false;
|
|
||||||
|
|
||||||
public function comments()
|
|
||||||
{
|
|
||||||
return $this->hasMany(Comment::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function scoped_comments()
|
|
||||||
{
|
|
||||||
return $this->hasMany(Comment::class);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Comment extends Model
|
function getRelationshipToPrimaryModel(): string
|
||||||
{
|
{
|
||||||
protected $guarded = [];
|
|
||||||
public $timestamps = false;
|
|
||||||
|
|
||||||
public function post()
|
|
||||||
{
|
|
||||||
return $this->belongsTo(Post::class);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ScopedComment extends Comment
|
|
||||||
{
|
|
||||||
use BelongsToPrimaryModel;
|
|
||||||
|
|
||||||
protected $table = 'comments';
|
|
||||||
|
|
||||||
public function getRelationshipToPrimaryModel(): string
|
|
||||||
{
|
|
||||||
return 'post';
|
return 'post';
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class GlobalResource extends Model
|
|
||||||
{
|
|
||||||
protected $guarded = [];
|
|
||||||
public $timestamps = false;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,20 +2,15 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
||||||
use Stancl\Tenancy\Database\Models;
|
use Stancl\Tenancy\Database\Models;
|
||||||
use Stancl\Tenancy\Exceptions\NotASubdomainException;
|
use Stancl\Tenancy\Exceptions\NotASubdomainException;
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
|
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
|
||||||
|
|
||||||
class SubdomainTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
// Global state cleanup after some tests
|
// Global state cleanup after some tests
|
||||||
InitializeTenancyBySubdomain::$onFail = null;
|
InitializeTenancyBySubdomain::$onFail = null;
|
||||||
|
|
||||||
|
|
@ -28,11 +23,9 @@ class SubdomainTest extends TestCase
|
||||||
});
|
});
|
||||||
|
|
||||||
config(['tenancy.tenant_model' => SubdomainTenant::class]);
|
config(['tenancy.tenant_model' => SubdomainTenant::class]);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant can be identified by subdomain', function () {
|
||||||
public function tenant_can_be_identified_by_subdomain()
|
|
||||||
{
|
|
||||||
$tenant = SubdomainTenant::create([
|
$tenant = SubdomainTenant::create([
|
||||||
'id' => 'acme',
|
'id' => 'acme',
|
||||||
]);
|
]);
|
||||||
|
|
@ -49,11 +42,9 @@ class SubdomainTest extends TestCase
|
||||||
|
|
||||||
$this->assertTrue(tenancy()->initialized);
|
$this->assertTrue(tenancy()->initialized);
|
||||||
$this->assertSame('acme', tenant('id'));
|
$this->assertSame('acme', tenant('id'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('onfail logic can be customized', function () {
|
||||||
public function onfail_logic_can_be_customized()
|
|
||||||
{
|
|
||||||
InitializeTenancyBySubdomain::$onFail = function () {
|
InitializeTenancyBySubdomain::$onFail = function () {
|
||||||
return 'foo';
|
return 'foo';
|
||||||
};
|
};
|
||||||
|
|
@ -61,31 +52,25 @@ class SubdomainTest extends TestCase
|
||||||
$this
|
$this
|
||||||
->get('http://foo.localhost/foo/abc/xyz')
|
->get('http://foo.localhost/foo/abc/xyz')
|
||||||
->assertSee('foo');
|
->assertSee('foo');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('localhost is not a valid subdomain', function () {
|
||||||
public function localhost_is_not_a_valid_subdomain()
|
|
||||||
{
|
|
||||||
$this->expectException(NotASubdomainException::class);
|
$this->expectException(NotASubdomainException::class);
|
||||||
|
|
||||||
$this
|
$this
|
||||||
->withoutExceptionHandling()
|
->withoutExceptionHandling()
|
||||||
->get('http://localhost/foo/abc/xyz');
|
->get('http://localhost/foo/abc/xyz');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('ip address is not a valid subdomain', function () {
|
||||||
public function ip_address_is_not_a_valid_subdomain()
|
|
||||||
{
|
|
||||||
$this->expectException(NotASubdomainException::class);
|
$this->expectException(NotASubdomainException::class);
|
||||||
|
|
||||||
$this
|
$this
|
||||||
->withoutExceptionHandling()
|
->withoutExceptionHandling()
|
||||||
->get('http://127.0.0.1/foo/abc/xyz');
|
->get('http://127.0.0.1/foo/abc/xyz');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('oninvalidsubdomain logic can be customized', function () {
|
||||||
public function oninvalidsubdomain_logic_can_be_customized()
|
|
||||||
{
|
|
||||||
// in this case, we need to return a response instance
|
// in this case, we need to return a response instance
|
||||||
// since a string would be treated as the subdomain
|
// since a string would be treated as the subdomain
|
||||||
InitializeTenancyBySubdomain::$onFail = function ($e) {
|
InitializeTenancyBySubdomain::$onFail = function ($e) {
|
||||||
|
|
@ -100,11 +85,9 @@ class SubdomainTest extends TestCase
|
||||||
->withoutExceptionHandling()
|
->withoutExceptionHandling()
|
||||||
->get('http://127.0.0.1/foo/abc/xyz')
|
->get('http://127.0.0.1/foo/abc/xyz')
|
||||||
->assertSee('foo custom invalid subdomain handler');
|
->assertSee('foo custom invalid subdomain handler');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('we cant use a subdomain that doesnt belong to our central domains', function () {
|
||||||
public function we_cant_use_a_subdomain_that_doesnt_belong_to_our_central_domains()
|
|
||||||
{
|
|
||||||
config(['tenancy.central_domains' => [
|
config(['tenancy.central_domains' => [
|
||||||
'127.0.0.1',
|
'127.0.0.1',
|
||||||
// not 'localhost'
|
// not 'localhost'
|
||||||
|
|
@ -123,11 +106,9 @@ class SubdomainTest extends TestCase
|
||||||
$this
|
$this
|
||||||
->withoutExceptionHandling()
|
->withoutExceptionHandling()
|
||||||
->get('http://foo.localhost/foo/abc/xyz');
|
->get('http://foo.localhost/foo/abc/xyz');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('central domain is not a subdomain', function () {
|
||||||
public function central_domain_is_not_a_subdomain()
|
|
||||||
{
|
|
||||||
config(['tenancy.central_domains' => [
|
config(['tenancy.central_domains' => [
|
||||||
'localhost',
|
'localhost',
|
||||||
]]);
|
]]);
|
||||||
|
|
@ -145,10 +126,4 @@ class SubdomainTest extends TestCase
|
||||||
$this
|
$this
|
||||||
->withoutExceptionHandling()
|
->withoutExceptionHandling()
|
||||||
->get('http://localhost/foo/abc/xyz');
|
->get('http://localhost/foo/abc/xyz');
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
class SubdomainTenant extends Models\Tenant
|
|
||||||
{
|
|
||||||
use HasDomains;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
@ -15,43 +13,22 @@ use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class TenantAssetTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public function getEnvironmentSetUp($app)
|
|
||||||
{
|
|
||||||
parent::getEnvironmentSetUp($app);
|
|
||||||
|
|
||||||
$app->booted(function () {
|
|
||||||
if (file_exists(base_path('routes/tenant.php'))) {
|
|
||||||
Route::middleware(['web'])
|
|
||||||
->namespace($this->app['config']['tenancy.tenant_route_namespace'] ?? 'App\Http\Controllers')
|
|
||||||
->group(base_path('routes/tenant.php'));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
FilesystemTenancyBootstrapper::class,
|
FilesystemTenancyBootstrapper::class,
|
||||||
]]);
|
]]);
|
||||||
|
|
||||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
}
|
});
|
||||||
|
|
||||||
public function tearDown(): void
|
|
||||||
{
|
|
||||||
parent::tearDown();
|
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
// Cleanup
|
// Cleanup
|
||||||
TenantAssetsController::$tenancyMiddleware = InitializeTenancyByDomain::class;
|
TenantAssetsController::$tenancyMiddleware = InitializeTenancyByDomain::class;
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('asset can be accessed using the url returned by the tenant asset helper', function () {
|
||||||
public function asset_can_be_accessed_using_the_url_returned_by_the_tenant_asset_helper()
|
|
||||||
{
|
|
||||||
TenantAssetsController::$tenancyMiddleware = InitializeTenancyByRequestData::class;
|
TenantAssetsController::$tenancyMiddleware = InitializeTenancyByRequestData::class;
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
@ -75,33 +52,27 @@ class TenantAssetTest extends TestCase
|
||||||
fclose($f);
|
fclose($f);
|
||||||
|
|
||||||
$this->assertSame('bar', $content);
|
$this->assertSame('bar', $content);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('asset helper returns a link to tenant asset controller when asset url is null', function () {
|
||||||
public function asset_helper_returns_a_link_to_TenantAssetController_when_asset_url_is_null()
|
|
||||||
{
|
|
||||||
config(['app.asset_url' => null]);
|
config(['app.asset_url' => null]);
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
$this->assertSame(route('stancl.tenancy.asset', ['path' => 'foo']), asset('foo'));
|
$this->assertSame(route('stancl.tenancy.asset', ['path' => 'foo']), asset('foo'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('asset helper returns a link to an external url when asset url is not null', function () {
|
||||||
public function asset_helper_returns_a_link_to_an_external_url_when_asset_url_is_not_null()
|
|
||||||
{
|
|
||||||
config(['app.asset_url' => 'https://an-s3-bucket']);
|
config(['app.asset_url' => 'https://an-s3-bucket']);
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
$this->assertSame("https://an-s3-bucket/tenant{$tenant->id}/foo", asset('foo'));
|
$this->assertSame("https://an-s3-bucket/tenant{$tenant->id}/foo", asset('foo'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('global asset helper returns the same url regardless of tenancy initialization', function () {
|
||||||
public function global_asset_helper_returns_the_same_url_regardless_of_tenancy_initialization()
|
|
||||||
{
|
|
||||||
$original = global_asset('foobar');
|
$original = global_asset('foobar');
|
||||||
$this->assertSame(asset('foobar'), global_asset('foobar'));
|
$this->assertSame(asset('foobar'), global_asset('foobar'));
|
||||||
|
|
||||||
|
|
@ -109,11 +80,9 @@ class TenantAssetTest extends TestCase
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
$this->assertSame($original, global_asset('foobar'));
|
$this->assertSame($original, global_asset('foobar'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('asset helper tenancy can be disabled', function () {
|
||||||
public function asset_helper_tenancy_can_be_disabled()
|
|
||||||
{
|
|
||||||
$original = asset('foo');
|
$original = asset('foo');
|
||||||
|
|
||||||
config([
|
config([
|
||||||
|
|
@ -125,5 +94,18 @@ class TenantAssetTest extends TestCase
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
$this->assertSame($original, asset('foo'));
|
$this->assertSame($original, asset('foo'));
|
||||||
|
});
|
||||||
|
|
||||||
|
// Helpers
|
||||||
|
function getEnvironmentSetUp($app)
|
||||||
|
{
|
||||||
|
parent::getEnvironmentSetUp($app);
|
||||||
|
|
||||||
|
$app->booted(function () {
|
||||||
|
if (file_exists(base_path('routes/tenant.php'))) {
|
||||||
|
Route::middleware(['web'])
|
||||||
|
->namespace(test()->app['config']['tenancy.tenant_route_namespace'] ?? 'App\Http\Controllers')
|
||||||
|
->group(base_path('routes/tenant.php'));
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,13 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Artisan;
|
use Illuminate\Support\Facades\Artisan;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class TenantAwareCommandTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
/** @test */
|
test('commands run globally are tenant aware and return valid exit code', function () {
|
||||||
public function commands_run_globally_are_tenant_aware_and_return_valid_exit_code()
|
|
||||||
{
|
|
||||||
$tenant1 = Tenant::create();
|
$tenant1 = Tenant::create();
|
||||||
$tenant2 = Tenant::create();
|
$tenant2 = Tenant::create();
|
||||||
Artisan::call('tenants:migrate', [
|
Artisan::call('tenants:migrate', [
|
||||||
|
|
@ -29,5 +25,4 @@ class TenantAwareCommandTest extends TestCase
|
||||||
tenancy()->initialize($tenant2);
|
tenancy()->initialize($tenant2);
|
||||||
$this->assertNotEmpty(DB::table('users')->get());
|
$this->assertNotEmpty(DB::table('users')->get());
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
@ -27,14 +25,9 @@ use Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager;
|
||||||
use Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager;
|
use Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class TenantDatabaseManagerTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
/**
|
test('databases can be created and deleted', function ($driver, $databaseManager) {
|
||||||
* @test
|
|
||||||
* @dataProvider database_manager_provider
|
|
||||||
*/
|
|
||||||
public function databases_can_be_created_and_deleted($driver, $databaseManager)
|
|
||||||
{
|
|
||||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
return $event->tenant;
|
return $event->tenant;
|
||||||
})->toListener());
|
})->toListener());
|
||||||
|
|
@ -58,11 +51,9 @@ class TenantDatabaseManagerTest extends TestCase
|
||||||
$this->assertTrue($manager->databaseExists($name));
|
$this->assertTrue($manager->databaseExists($name));
|
||||||
$manager->deleteDatabase($tenant);
|
$manager->deleteDatabase($tenant);
|
||||||
$this->assertFalse($manager->databaseExists($name));
|
$this->assertFalse($manager->databaseExists($name));
|
||||||
}
|
})->with('database_manager_provider');
|
||||||
|
|
||||||
/** @test */
|
test('dbs can be created when another driver is used for the central db', function () {
|
||||||
public function dbs_can_be_created_when_another_driver_is_used_for_the_central_db()
|
|
||||||
{
|
|
||||||
$this->assertSame('central', config('database.default'));
|
$this->assertSame('central', config('database.default'));
|
||||||
|
|
||||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
|
|
@ -94,23 +85,9 @@ class TenantDatabaseManagerTest extends TestCase
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertTrue($postgresManager->databaseExists($database));
|
$this->assertTrue($postgresManager->databaseExists($database));
|
||||||
}
|
});
|
||||||
|
|
||||||
public function database_manager_provider()
|
test('the tenant connection is fully removed', function () {
|
||||||
{
|
|
||||||
return [
|
|
||||||
['mysql', MySQLDatabaseManager::class],
|
|
||||||
['mysql', PermissionControlledMySQLDatabaseManager::class],
|
|
||||||
['sqlite', SQLiteDatabaseManager::class],
|
|
||||||
['pgsql', PostgreSQLDatabaseManager::class],
|
|
||||||
['pgsql', PostgreSQLSchemaManager::class],
|
|
||||||
['sqlsrv', MicrosoftSQLDatabaseManager::class]
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function the_tenant_connection_is_fully_removed()
|
|
||||||
{
|
|
||||||
config([
|
config([
|
||||||
'tenancy.boostrappers' => [
|
'tenancy.boostrappers' => [
|
||||||
DatabaseTenancyBootstrapper::class,
|
DatabaseTenancyBootstrapper::class,
|
||||||
|
|
@ -131,7 +108,7 @@ class TenantDatabaseManagerTest extends TestCase
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
$this->createUsersTable();
|
createUsersTable();
|
||||||
|
|
||||||
$this->assertSame(['central', 'tenant'], array_keys(app('db')->getConnections()));
|
$this->assertSame(['central', 'tenant'], array_keys(app('db')->getConnections()));
|
||||||
$this->assertArrayHasKey('tenant', config('database.connections'));
|
$this->assertArrayHasKey('tenant', config('database.connections'));
|
||||||
|
|
@ -140,23 +117,9 @@ class TenantDatabaseManagerTest extends TestCase
|
||||||
|
|
||||||
$this->assertSame(['central'], array_keys(app('db')->getConnections()));
|
$this->assertSame(['central'], array_keys(app('db')->getConnections()));
|
||||||
$this->assertNull(config('database.connections.tenant'));
|
$this->assertNull(config('database.connections.tenant'));
|
||||||
}
|
});
|
||||||
|
|
||||||
protected function createUsersTable()
|
test('db name is prefixed with db path when sqlite is used', function () {
|
||||||
{
|
|
||||||
Schema::create('users', function (Blueprint $table) {
|
|
||||||
$table->increments('id');
|
|
||||||
$table->string('name');
|
|
||||||
$table->string('email')->unique();
|
|
||||||
$table->string('password');
|
|
||||||
$table->rememberToken();
|
|
||||||
$table->timestamps();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function db_name_is_prefixed_with_db_path_when_sqlite_is_used()
|
|
||||||
{
|
|
||||||
if (file_exists(database_path('foodb'))) {
|
if (file_exists(database_path('foodb'))) {
|
||||||
unlink(database_path('foodb')); // cleanup
|
unlink(database_path('foodb')); // cleanup
|
||||||
}
|
}
|
||||||
|
|
@ -171,11 +134,9 @@ class TenantDatabaseManagerTest extends TestCase
|
||||||
app(DatabaseManager::class)->createTenantConnection($tenant);
|
app(DatabaseManager::class)->createTenantConnection($tenant);
|
||||||
|
|
||||||
$this->assertSame(config('database.connections.tenant.database'), database_path('foodb'));
|
$this->assertSame(config('database.connections.tenant.database'), database_path('foodb'));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('schema manager uses schema to separate tenant dbs', function () {
|
||||||
public function schema_manager_uses_schema_to_separate_tenant_dbs()
|
|
||||||
{
|
|
||||||
config([
|
config([
|
||||||
'tenancy.database.managers.pgsql' => \Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager::class,
|
'tenancy.database.managers.pgsql' => \Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager::class,
|
||||||
'tenancy.boostrappers' => [
|
'tenancy.boostrappers' => [
|
||||||
|
|
@ -202,11 +163,9 @@ class TenantDatabaseManagerTest extends TestCase
|
||||||
|
|
||||||
$this->assertSame($tenant->database()->getName(), $schemaConfig);
|
$this->assertSame($tenant->database()->getName(), $schemaConfig);
|
||||||
$this->assertSame($originalDatabaseName, config(['database.connections.pgsql.database']));
|
$this->assertSame($originalDatabaseName, config(['database.connections.pgsql.database']));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('a tenants database cannot be created when the database already exists', function () {
|
||||||
public function a_tenants_database_cannot_be_created_when_the_database_already_exists()
|
|
||||||
{
|
|
||||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
return $event->tenant;
|
return $event->tenant;
|
||||||
})->toListener());
|
})->toListener());
|
||||||
|
|
@ -223,11 +182,9 @@ class TenantDatabaseManagerTest extends TestCase
|
||||||
$tenant2 = Tenant::create([
|
$tenant2 = Tenant::create([
|
||||||
'tenancy_db_name' => $name,
|
'tenancy_db_name' => $name,
|
||||||
]);
|
]);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant database can be created on a foreign server', function () {
|
||||||
public function tenant_database_can_be_created_on_a_foreign_server()
|
|
||||||
{
|
|
||||||
config([
|
config([
|
||||||
'tenancy.database.managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
'tenancy.database.managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
||||||
'database.connections.mysql2' => [
|
'database.connections.mysql2' => [
|
||||||
|
|
@ -268,11 +225,31 @@ class TenantDatabaseManagerTest extends TestCase
|
||||||
|
|
||||||
$manager->setConnection('mysql2');
|
$manager->setConnection('mysql2');
|
||||||
$this->assertTrue($manager->databaseExists($name));
|
$this->assertTrue($manager->databaseExists($name));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('path used by sqlite manager can be customized', function () {
|
||||||
public function path_used_by_sqlite_manager_can_be_customized()
|
|
||||||
{
|
|
||||||
$this->markTestIncomplete();
|
$this->markTestIncomplete();
|
||||||
}
|
});
|
||||||
|
|
||||||
|
// Datasets
|
||||||
|
dataset('database_manager_provider', [
|
||||||
|
['mysql', MySQLDatabaseManager::class],
|
||||||
|
['mysql', PermissionControlledMySQLDatabaseManager::class],
|
||||||
|
['sqlite', SQLiteDatabaseManager::class],
|
||||||
|
['pgsql', PostgreSQLDatabaseManager::class],
|
||||||
|
['pgsql', PostgreSQLSchemaManager::class],
|
||||||
|
['sqlsrv', MicrosoftSQLDatabaseManager::class]
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Helpers
|
||||||
|
function createUsersTable()
|
||||||
|
{
|
||||||
|
Schema::create('users', function (Blueprint $table) {
|
||||||
|
$table->increments('id');
|
||||||
|
$table->string('name');
|
||||||
|
$table->string('email')->unique();
|
||||||
|
$table->string('password');
|
||||||
|
$table->rememberToken();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
|
|
@ -21,11 +19,9 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
use Stancl\Tenancy\UUIDGenerator;
|
use Stancl\Tenancy\UUIDGenerator;
|
||||||
|
|
||||||
class TenantModelTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
/** @test */
|
test('created event is dispatched', function () {
|
||||||
public function created_event_is_dispatched()
|
|
||||||
{
|
|
||||||
Event::fake([TenantCreated::class]);
|
Event::fake([TenantCreated::class]);
|
||||||
|
|
||||||
Event::assertNotDispatched(TenantCreated::class);
|
Event::assertNotDispatched(TenantCreated::class);
|
||||||
|
|
@ -33,11 +29,9 @@ class TenantModelTest extends TestCase
|
||||||
Tenant::create();
|
Tenant::create();
|
||||||
|
|
||||||
Event::assertDispatched(TenantCreated::class);
|
Event::assertDispatched(TenantCreated::class);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('current tenant can be resolved from service container using typehint', function () {
|
||||||
public function current_tenant_can_be_resolved_from_service_container_using_typehint()
|
|
||||||
{
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
@ -47,11 +41,9 @@ class TenantModelTest extends TestCase
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
$this->assertSame(null, app(Contracts\Tenant::class));
|
$this->assertSame(null, app(Contracts\Tenant::class));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('id is generated when no id is supplied', function () {
|
||||||
public function id_is_generated_when_no_id_is_supplied()
|
|
||||||
{
|
|
||||||
config(['tenancy.id_generator' => UUIDGenerator::class]);
|
config(['tenancy.id_generator' => UUIDGenerator::class]);
|
||||||
|
|
||||||
$this->mock(UUIDGenerator::class, function ($mock) {
|
$this->mock(UUIDGenerator::class, function ($mock) {
|
||||||
|
|
@ -61,11 +53,9 @@ class TenantModelTest extends TestCase
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
$this->assertNotNull($tenant->id);
|
$this->assertNotNull($tenant->id);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('autoincrement ids are supported', function () {
|
||||||
public function autoincrement_ids_are_supported()
|
|
||||||
{
|
|
||||||
Schema::drop('domains');
|
Schema::drop('domains');
|
||||||
Schema::table('tenants', function (Blueprint $table) {
|
Schema::table('tenants', function (Blueprint $table) {
|
||||||
$table->bigIncrements('id')->change();
|
$table->bigIncrements('id')->change();
|
||||||
|
|
@ -78,21 +68,17 @@ class TenantModelTest extends TestCase
|
||||||
|
|
||||||
$this->assertSame(1, $tenant1->id);
|
$this->assertSame(1, $tenant1->id);
|
||||||
$this->assertSame(2, $tenant2->id);
|
$this->assertSame(2, $tenant2->id);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('custom tenant model can be used', function () {
|
||||||
public function custom_tenant_model_can_be_used()
|
|
||||||
{
|
|
||||||
$tenant = MyTenant::create();
|
$tenant = MyTenant::create();
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
$this->assertTrue(tenant() instanceof MyTenant);
|
$this->assertTrue(tenant() instanceof MyTenant);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('custom tenant model that doesnt extend vendor tenant model can be used', function () {
|
||||||
public function custom_tenant_model_that_doesnt_extend_vendor_Tenant_model_can_be_used()
|
|
||||||
{
|
|
||||||
$tenant = AnotherTenant::create([
|
$tenant = AnotherTenant::create([
|
||||||
'id' => 'acme',
|
'id' => 'acme',
|
||||||
]);
|
]);
|
||||||
|
|
@ -100,11 +86,9 @@ class TenantModelTest extends TestCase
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
$this->assertTrue(tenant() instanceof AnotherTenant);
|
$this->assertTrue(tenant() instanceof AnotherTenant);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant can be created even when we are in another tenants context', function () {
|
||||||
public function tenant_can_be_created_even_when_we_are_in_another_tenants_context()
|
|
||||||
{
|
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
DatabaseTenancyBootstrapper::class,
|
DatabaseTenancyBootstrapper::class,
|
||||||
]]);
|
]]);
|
||||||
|
|
@ -129,21 +113,17 @@ class TenantModelTest extends TestCase
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
$this->assertSame(2, Tenant::count());
|
$this->assertSame(2, Tenant::count());
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('the model uses tenant collection', function () {
|
||||||
public function the_model_uses_TenantCollection()
|
|
||||||
{
|
|
||||||
Tenant::create();
|
Tenant::create();
|
||||||
Tenant::create();
|
Tenant::create();
|
||||||
|
|
||||||
$this->assertSame(2, Tenant::count());
|
$this->assertSame(2, Tenant::count());
|
||||||
$this->assertTrue(Tenant::all() instanceof TenantCollection);
|
$this->assertTrue(Tenant::all() instanceof TenantCollection);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('a command can be run on a collection of tenants', function () {
|
||||||
public function a_command_can_be_run_on_a_collection_of_tenants()
|
|
||||||
{
|
|
||||||
Tenant::create([
|
Tenant::create([
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
'foo' => 'bar',
|
'foo' => 'bar',
|
||||||
|
|
@ -161,41 +141,30 @@ class TenantModelTest extends TestCase
|
||||||
|
|
||||||
$this->assertSame('xyz', Tenant::find('t1')->foo);
|
$this->assertSame('xyz', Tenant::find('t1')->foo);
|
||||||
$this->assertSame('xyz', Tenant::find('t2')->foo);
|
$this->assertSame('xyz', Tenant::find('t2')->foo);
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
class MyTenant extends Tenant
|
// Helpers
|
||||||
|
function getTenantKeyName(): string
|
||||||
{
|
{
|
||||||
protected $table = 'tenants';
|
|
||||||
}
|
|
||||||
|
|
||||||
class AnotherTenant extends Model implements Contracts\Tenant
|
|
||||||
{
|
|
||||||
protected $guarded = [];
|
|
||||||
protected $table = 'tenants';
|
|
||||||
|
|
||||||
public function getTenantKeyName(): string
|
|
||||||
{
|
|
||||||
return 'id';
|
return 'id';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTenantKey()
|
function getTenantKey()
|
||||||
{
|
{
|
||||||
return $this->getAttribute('id');
|
return test()->getAttribute('id');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run(callable $callback)
|
function run(callable $callback)
|
||||||
{
|
{
|
||||||
$callback();
|
$callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getInternal(string $key)
|
function getInternal(string $key)
|
||||||
{
|
{
|
||||||
return $this->$key;
|
return test()->$key;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setInternal(string $key, $value)
|
function setInternal(string $key, $value)
|
||||||
{
|
{
|
||||||
$this->$key = $value;
|
test()->$key = $value;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Carbon\CarbonInterval;
|
use Carbon\CarbonInterval;
|
||||||
use Closure;
|
use Closure;
|
||||||
|
|
@ -27,17 +25,9 @@ use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
|
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class TenantUserImpersonationTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
protected function migrateTenants()
|
|
||||||
{
|
|
||||||
$this->artisan('tenants:migrate')->assertExitCode(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
$this->artisan('migrate', [
|
$this->artisan('migrate', [
|
||||||
'--path' => __DIR__ . '/../assets/impersonation-migrations',
|
'--path' => __DIR__ . '/../assets/impersonation-migrations',
|
||||||
'--realpath' => true,
|
'--realpath' => true,
|
||||||
|
|
@ -63,42 +53,16 @@ class TenantUserImpersonationTest extends TestCase
|
||||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
|
|
||||||
config(['auth.providers.users.model' => ImpersonationUser::class]);
|
config(['auth.providers.users.model' => ImpersonationUser::class]);
|
||||||
}
|
});
|
||||||
|
|
||||||
public function makeLoginRoute()
|
test('tenant user can be impersonated on a tenant domain', function () {
|
||||||
{
|
Route::middleware(InitializeTenancyByDomain::class)->group(getRoutes());
|
||||||
Route::get('/login', function () {
|
|
||||||
return 'Please log in';
|
|
||||||
})->name('login');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getRoutes($loginRoute = true, $authGuard = 'web'): Closure
|
|
||||||
{
|
|
||||||
return function () use ($loginRoute, $authGuard) {
|
|
||||||
if ($loginRoute) {
|
|
||||||
$this->makeLoginRoute();
|
|
||||||
}
|
|
||||||
|
|
||||||
Route::get('/dashboard', function () use ($authGuard) {
|
|
||||||
return 'You are logged in as ' . auth()->guard($authGuard)->user()->name;
|
|
||||||
})->middleware('auth:' . $authGuard);
|
|
||||||
|
|
||||||
Route::get('/impersonate/{token}', function ($token) {
|
|
||||||
return UserImpersonation::makeResponse($token);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function tenant_user_can_be_impersonated_on_a_tenant_domain()
|
|
||||||
{
|
|
||||||
Route::middleware(InitializeTenancyByDomain::class)->group($this->getRoutes());
|
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
$tenant->domains()->create([
|
$tenant->domains()->create([
|
||||||
'domain' => 'foo.localhost',
|
'domain' => 'foo.localhost',
|
||||||
]);
|
]);
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
$user = $tenant->run(function () {
|
$user = $tenant->run(function () {
|
||||||
return ImpersonationUser::create([
|
return ImpersonationUser::create([
|
||||||
'name' => 'Joe',
|
'name' => 'Joe',
|
||||||
|
|
@ -120,20 +84,18 @@ class TenantUserImpersonationTest extends TestCase
|
||||||
$this->get('http://foo.localhost/dashboard')
|
$this->get('http://foo.localhost/dashboard')
|
||||||
->assertSuccessful()
|
->assertSuccessful()
|
||||||
->assertSee('You are logged in as Joe');
|
->assertSee('You are logged in as Joe');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tenant user can be impersonated on a tenant path', function () {
|
||||||
public function tenant_user_can_be_impersonated_on_a_tenant_path()
|
makeLoginRoute();
|
||||||
{
|
|
||||||
$this->makeLoginRoute();
|
|
||||||
|
|
||||||
Route::middleware(InitializeTenancyByPath::class)->prefix('/{tenant}')->group($this->getRoutes(false));
|
Route::middleware(InitializeTenancyByPath::class)->prefix('/{tenant}')->group(getRoutes(false));
|
||||||
|
|
||||||
$tenant = Tenant::create([
|
$tenant = Tenant::create([
|
||||||
'id' => 'acme',
|
'id' => 'acme',
|
||||||
'tenancy_db_name' => 'db' . Str::random(16),
|
'tenancy_db_name' => 'db' . Str::random(16),
|
||||||
]);
|
]);
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
$user = $tenant->run(function () {
|
$user = $tenant->run(function () {
|
||||||
return ImpersonationUser::create([
|
return ImpersonationUser::create([
|
||||||
'name' => 'Joe',
|
'name' => 'Joe',
|
||||||
|
|
@ -155,18 +117,16 @@ class TenantUserImpersonationTest extends TestCase
|
||||||
$this->get('/acme/dashboard')
|
$this->get('/acme/dashboard')
|
||||||
->assertSuccessful()
|
->assertSuccessful()
|
||||||
->assertSee('You are logged in as Joe');
|
->assertSee('You are logged in as Joe');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tokens have a limited ttl', function () {
|
||||||
public function tokens_have_a_limited_ttl()
|
Route::middleware(InitializeTenancyByDomain::class)->group(getRoutes());
|
||||||
{
|
|
||||||
Route::middleware(InitializeTenancyByDomain::class)->group($this->getRoutes());
|
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
$tenant->domains()->create([
|
$tenant->domains()->create([
|
||||||
'domain' => 'foo.localhost',
|
'domain' => 'foo.localhost',
|
||||||
]);
|
]);
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
$user = $tenant->run(function () {
|
$user = $tenant->run(function () {
|
||||||
return ImpersonationUser::create([
|
return ImpersonationUser::create([
|
||||||
'name' => 'Joe',
|
'name' => 'Joe',
|
||||||
|
|
@ -184,18 +144,16 @@ class TenantUserImpersonationTest extends TestCase
|
||||||
$this->followingRedirects()
|
$this->followingRedirects()
|
||||||
->get('http://foo.localhost/impersonate/' . $token->token)
|
->get('http://foo.localhost/impersonate/' . $token->token)
|
||||||
->assertStatus(403);
|
->assertStatus(403);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('tokens are deleted after use', function () {
|
||||||
public function tokens_are_deleted_after_use()
|
Route::middleware(InitializeTenancyByDomain::class)->group(getRoutes());
|
||||||
{
|
|
||||||
Route::middleware(InitializeTenancyByDomain::class)->group($this->getRoutes());
|
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
$tenant->domains()->create([
|
$tenant->domains()->create([
|
||||||
'domain' => 'foo.localhost',
|
'domain' => 'foo.localhost',
|
||||||
]);
|
]);
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
$user = $tenant->run(function () {
|
$user = $tenant->run(function () {
|
||||||
return ImpersonationUser::create([
|
return ImpersonationUser::create([
|
||||||
'name' => 'Joe',
|
'name' => 'Joe',
|
||||||
|
|
@ -215,11 +173,9 @@ class TenantUserImpersonationTest extends TestCase
|
||||||
->assertSee('You are logged in as Joe');
|
->assertSee('You are logged in as Joe');
|
||||||
|
|
||||||
$this->assertNull(ImpersonationToken::find($token->token));
|
$this->assertNull(ImpersonationToken::find($token->token));
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('impersonation works with multiple models and guards', function () {
|
||||||
public function impersonation_works_with_multiple_models_and_guards()
|
|
||||||
{
|
|
||||||
config([
|
config([
|
||||||
'auth.guards.another' => [
|
'auth.guards.another' => [
|
||||||
'driver' => 'session',
|
'driver' => 'session',
|
||||||
|
|
@ -235,13 +191,13 @@ class TenantUserImpersonationTest extends TestCase
|
||||||
return new SessionGuard($name, Auth::createUserProvider($config['provider']), session());
|
return new SessionGuard($name, Auth::createUserProvider($config['provider']), session());
|
||||||
});
|
});
|
||||||
|
|
||||||
Route::middleware(InitializeTenancyByDomain::class)->group($this->getRoutes(true, 'another'));
|
Route::middleware(InitializeTenancyByDomain::class)->group(getRoutes(true, 'another'));
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
$tenant->domains()->create([
|
$tenant->domains()->create([
|
||||||
'domain' => 'foo.localhost',
|
'domain' => 'foo.localhost',
|
||||||
]);
|
]);
|
||||||
$this->migrateTenants();
|
migrateTenants();
|
||||||
$user = $tenant->run(function () {
|
$user = $tenant->run(function () {
|
||||||
return AnotherImpersonationUser::create([
|
return AnotherImpersonationUser::create([
|
||||||
'name' => 'Joe',
|
'name' => 'Joe',
|
||||||
|
|
@ -268,17 +224,34 @@ class TenantUserImpersonationTest extends TestCase
|
||||||
$this->assertSame('Joe', auth()->guard('another')->user()->name);
|
$this->assertSame('Joe', auth()->guard('another')->user()->name);
|
||||||
$this->assertSame(null, auth()->guard('web')->user());
|
$this->assertSame(null, auth()->guard('web')->user());
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Helpers
|
||||||
|
function migrateTenants()
|
||||||
|
{
|
||||||
|
test()->artisan('tenants:migrate')->assertExitCode(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeLoginRoute()
|
||||||
|
{
|
||||||
|
Route::get('/login', function () {
|
||||||
|
return 'Please log in';
|
||||||
|
})->name('login');
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRoutes($loginRoute = true, $authGuard = 'web'): Closure
|
||||||
|
{
|
||||||
|
return function () use ($loginRoute, $authGuard) {
|
||||||
|
if ($loginRoute) {
|
||||||
|
test()->makeLoginRoute();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class ImpersonationUser extends Authenticable
|
Route::get('/dashboard', function () use ($authGuard) {
|
||||||
{
|
return 'You are logged in as ' . auth()->guard($authGuard)->user()->name;
|
||||||
protected $guarded = [];
|
})->middleware('auth:' . $authGuard);
|
||||||
protected $table = 'users';
|
|
||||||
}
|
|
||||||
|
|
||||||
class AnotherImpersonationUser extends Authenticable
|
Route::get('/impersonate/{token}', function ($token) {
|
||||||
{
|
return UserImpersonation::makeResponse($token);
|
||||||
protected $guarded = [];
|
});
|
||||||
protected $table = 'users';
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,25 +2,18 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use Stancl\Tenancy\Features\UniversalRoutes;
|
use Stancl\Tenancy\Features\UniversalRoutes;
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class UniversalRouteTest extends TestCase
|
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||||
{
|
|
||||||
public function tearDown(): void
|
afterEach(function () {
|
||||||
{
|
|
||||||
InitializeTenancyByDomain::$onFail = null;
|
InitializeTenancyByDomain::$onFail = null;
|
||||||
|
});
|
||||||
|
|
||||||
parent::tearDown();
|
test('a route can work in both central and tenant context', function () {
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function a_route_can_work_in_both_central_and_tenant_context()
|
|
||||||
{
|
|
||||||
Route::middlewareGroup('universal', []);
|
Route::middlewareGroup('universal', []);
|
||||||
config(['tenancy.features' => [UniversalRoutes::class]]);
|
config(['tenancy.features' => [UniversalRoutes::class]]);
|
||||||
|
|
||||||
|
|
@ -44,11 +37,9 @@ class UniversalRouteTest extends TestCase
|
||||||
$this->get('http://acme.localhost/foo')
|
$this->get('http://acme.localhost/foo')
|
||||||
->assertSuccessful()
|
->assertSuccessful()
|
||||||
->assertSee('Tenancy is initialized.');
|
->assertSee('Tenancy is initialized.');
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
test('making one route universal doesnt make all routes universal', function () {
|
||||||
public function making_one_route_universal_doesnt_make_all_routes_universal()
|
|
||||||
{
|
|
||||||
Route::get('/bar', function () {
|
Route::get('/bar', function () {
|
||||||
return tenant('id');
|
return tenant('id');
|
||||||
})->middleware(InitializeTenancyByDomain::class);
|
})->middleware(InitializeTenancyByDomain::class);
|
||||||
|
|
@ -62,5 +53,4 @@ class UniversalRouteTest extends TestCase
|
||||||
$this->get('http://acme.localhost/bar')
|
$this->get('http://acme.localhost/bar')
|
||||||
->assertSuccessful()
|
->assertSuccessful()
|
||||||
->assertSee('acme');
|
->assertSee('acme');
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue