mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 20:54:03 +00:00
* fixed tests, remove method duplications, restore necessary inner classes * Update CommandsTest.php * temporary checks run on `shift-64622` on branch. * fixed `TestSeeder` class not resolved * fixed messed up names * removed `uses` from individual files and add it in `Pest` * extract tests to helpers * use pest dataset * Update AutomaticModeTest.php * newline * todo convention * resolve reviews * added `// todo@tests` * remove shift branch from CI workflow Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
parent
9d08334f48
commit
3f347ecaf6
27 changed files with 500 additions and 368 deletions
|
|
@ -3,35 +3,24 @@
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Events\TenancyEnded;
|
use Stancl\Tenancy\Events\TenancyEnded;
|
||||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
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('context is switched when tenancy is initialized', function () {
|
test('context is switched when tenancy is initialized', function () {
|
||||||
config(['tenancy.bootstrappers' => [
|
contextIsSwitchedWhenTenancyInitialized();
|
||||||
MyBootstrapper::class,
|
|
||||||
]]);
|
|
||||||
|
|
||||||
$tenant = Tenant::create([
|
|
||||||
'id' => 'acme',
|
|
||||||
]);
|
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
|
||||||
|
|
||||||
expect(app('tenancy_initialized_for_tenant'))->toBe('acme');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('context is reverted when tenancy is ended', function () {
|
test('context is reverted when tenancy is ended', function () {
|
||||||
$this->context_is_switched_when_tenancy_is_initialized();
|
contextIsSwitchedWhenTenancyInitialized();
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
|
|
@ -100,13 +89,31 @@ test('central helper doesnt change tenancy state when called in central context'
|
||||||
expect(tenant())->toBeNull();
|
expect(tenant())->toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Helpers
|
// todo@tests
|
||||||
function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant)
|
function contextIsSwitchedWhenTenancyInitialized()
|
||||||
{
|
{
|
||||||
app()->instance('tenancy_initialized_for_tenant', $tenant->getTenantKey());
|
config(['tenancy.bootstrappers' => [
|
||||||
|
MyBootstrapper::class,
|
||||||
|
]]);
|
||||||
|
|
||||||
|
$tenant = Tenant::create([
|
||||||
|
'id' => 'acme',
|
||||||
|
]);
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
expect(app('tenancy_initialized_for_tenant'))->toBe('acme');
|
||||||
}
|
}
|
||||||
|
|
||||||
function revert()
|
class MyBootstrapper implements TenancyBootstrapper
|
||||||
{
|
{
|
||||||
app()->instance('tenancy_ended', true);
|
public function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant)
|
||||||
|
{
|
||||||
|
app()->instance('tenancy_initialized_for_tenant', $tenant->getTenantKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function revert()
|
||||||
|
{
|
||||||
|
app()->instance('tenancy_ended', true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,8 +22,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
Event::listen(
|
Event::listen(
|
||||||
TenantCreated::class,
|
TenantCreated::class,
|
||||||
|
|
@ -184,7 +182,6 @@ test('filesystem data is separated', function () {
|
||||||
expect($new_storage_path)->toEqual($expected_storage_path);
|
expect($new_storage_path)->toEqual($expected_storage_path);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Helpers
|
|
||||||
function getDiskPrefix(string $disk): string
|
function getDiskPrefix(string $disk): string
|
||||||
{
|
{
|
||||||
/** @var FilesystemAdapter $disk */
|
/** @var FilesystemAdapter $disk */
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
CacheTenancyBootstrapper::class,
|
CacheTenancyBootstrapper::class,
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
DomainTenantResolver::$shouldCache = false;
|
DomainTenantResolver::$shouldCache = false;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,9 @@
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomainOrSubdomain;
|
use Stancl\Tenancy\Middleware\InitializeTenancyByDomainOrSubdomain;
|
||||||
|
use Stancl\Tenancy\Database\Models;
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
Route::group([
|
Route::group([
|
||||||
|
|
@ -60,3 +60,8 @@ test('tenant can be identified by domain', function () {
|
||||||
expect(tenancy()->initialized)->toBeTrue();
|
expect(tenancy()->initialized)->toBeTrue();
|
||||||
expect(tenant('id'))->toBe('acme');
|
expect(tenant('id'))->toBe('acme');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
class CombinedTenant extends Models\Tenant
|
||||||
|
{
|
||||||
|
use HasDomains;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
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;
|
||||||
|
|
@ -117,41 +115,21 @@ test('rollback command works', function () {
|
||||||
expect(Schema::hasTable('users'))->toBeFalse();
|
expect(Schema::hasTable('users'))->toBeFalse();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('seed command works', function () {
|
// Incomplete test
|
||||||
$this->markTestIncomplete();
|
test('seed command works');
|
||||||
});
|
|
||||||
|
|
||||||
test('database connection is switched to default', function () {
|
test('database connection is switched to default', function () {
|
||||||
$originalDBName = DB::connection()->getDatabaseName();
|
databaseConnectionSwitchedToDefault();
|
||||||
|
|
||||||
Artisan::call('tenants:migrate');
|
|
||||||
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
|
|
||||||
|
|
||||||
Artisan::call('tenants:seed', ['--class' => ExampleSeeder::class]);
|
|
||||||
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
|
|
||||||
|
|
||||||
Artisan::call('tenants:rollback');
|
|
||||||
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
|
|
||||||
|
|
||||||
$this->run_commands_works();
|
|
||||||
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('database connection is switched to default when tenancy has been initialized', function () {
|
test('database connection is switched to default when tenancy has been initialized', function () {
|
||||||
tenancy()->initialize(Tenant::create());
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
$this->database_connection_is_switched_to_default();
|
databaseConnectionSwitchedToDefault();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('run commands works', function () {
|
test('run command works', function () {
|
||||||
$id = Tenant::create()->getTenantKey();
|
runCommandWorks();
|
||||||
|
|
||||||
Artisan::call('tenants:migrate', ['--tenants' => [$id]]);
|
|
||||||
|
|
||||||
$this->artisan("tenants:run foo --tenants=$id --argument='a=foo' --option='b=bar' --option='c=xyz'")
|
|
||||||
->expectsOutput("User's name is Test command")
|
|
||||||
->expectsOutput('foo')
|
|
||||||
->expectsOutput('xyz');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('install command works', function () {
|
test('install command works', function () {
|
||||||
|
|
@ -200,3 +178,35 @@ test('run command with array of tenants works', function () {
|
||||||
->expectsOutput('Tenant: ' . $tenantId1)
|
->expectsOutput('Tenant: ' . $tenantId1)
|
||||||
->expectsOutput('Tenant: ' . $tenantId2);
|
->expectsOutput('Tenant: ' . $tenantId2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// todo@tests
|
||||||
|
function runCommandWorks(): void
|
||||||
|
{
|
||||||
|
$id = Tenant::create()->getTenantKey();
|
||||||
|
|
||||||
|
Artisan::call('tenants:migrate', ['--tenants' => [$id]]);
|
||||||
|
|
||||||
|
test()->artisan("tenants:run foo --tenants=$id --argument='a=foo' --option='b=bar' --option='c=xyz'")
|
||||||
|
->expectsOutput("User's name is Test command")
|
||||||
|
->expectsOutput('foo')
|
||||||
|
->expectsOutput('xyz');
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo@tests
|
||||||
|
function databaseConnectionSwitchedToDefault()
|
||||||
|
{
|
||||||
|
$originalDBName = DB::connection()->getDatabaseName();
|
||||||
|
|
||||||
|
Artisan::call('tenants:migrate');
|
||||||
|
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
|
||||||
|
|
||||||
|
Artisan::call('tenants:seed', ['--class' => ExampleSeeder::class]);
|
||||||
|
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
|
||||||
|
|
||||||
|
Artisan::call('tenants:rollback');
|
||||||
|
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
|
||||||
|
|
||||||
|
runCommandWorks();
|
||||||
|
|
||||||
|
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
use Stancl\JobPipeline\JobPipeline;
|
use Stancl\JobPipeline\JobPipeline;
|
||||||
|
|
@ -12,8 +11,8 @@ use Stancl\Tenancy\Jobs\MigrateDatabase;
|
||||||
use Stancl\Tenancy\Jobs\SeedDatabase;
|
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;
|
||||||
|
use Illuminate\Foundation\Auth\User as Authenticable;
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
use Stancl\Tenancy\Tests\Etc\TestSeeder;
|
||||||
|
|
||||||
test('database can be created after tenant creation', function () {
|
test('database can be created after tenant creation', function () {
|
||||||
config(['tenancy.database.template_tenant_connection' => 'mysql']);
|
config(['tenancy.database.template_tenant_connection' => 'mysql']);
|
||||||
|
|
@ -86,29 +85,24 @@ test('custom job can be added to the pipeline', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Helpers
|
class User extends Authenticable
|
||||||
/**
|
|
||||||
* Run the database seeds.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
function run()
|
|
||||||
{
|
{
|
||||||
DB::table('users')->insert([
|
protected $guarded = [];
|
||||||
'name' => 'Seeded User',
|
|
||||||
'email' => 'seeded@user',
|
|
||||||
'password' => bcrypt('password'),
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function __construct(Tenant $tenant)
|
class CreateSuperuser
|
||||||
{
|
{
|
||||||
test()->tenant = $tenant;
|
protected $tenant;
|
||||||
}
|
|
||||||
|
|
||||||
function handle()
|
public function __construct(Tenant $tenant)
|
||||||
{
|
{
|
||||||
test()->tenant->run(function () {
|
$this->tenant = $tenant;
|
||||||
User::create(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']);
|
}
|
||||||
});
|
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$this->tenant->run(function () {
|
||||||
|
User::create(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
config([
|
config([
|
||||||
'tenancy.database.managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
'tenancy.database.managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,14 @@
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
||||||
|
use Stancl\Tenancy\Database\Models;
|
||||||
use Stancl\Tenancy\Database\Models\Domain;
|
use Stancl\Tenancy\Database\Models\Domain;
|
||||||
use Stancl\Tenancy\Exceptions\DomainOccupiedByOtherTenantException;
|
use Stancl\Tenancy\Exceptions\DomainOccupiedByOtherTenantException;
|
||||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
Route::group([
|
Route::group([
|
||||||
'middleware' => InitializeTenancyByDomain::class,
|
'middleware' => InitializeTenancyByDomain::class,
|
||||||
|
|
@ -97,3 +97,8 @@ test('domains are always lowercase', function () {
|
||||||
|
|
||||||
expect(Domain::first()->domain)->toBe('capitals');
|
expect(Domain::first()->domain)->toBe('capitals');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
class DomainTenant extends Models\Tenant
|
||||||
|
{
|
||||||
|
use HasDomains;
|
||||||
|
}
|
||||||
|
|
|
||||||
23
tests/Etc/TestSeeder.php
Normal file
23
tests/Etc/TestSeeder.php
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Tests\Etc;
|
||||||
|
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class TestSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
DB::table('users')->insert([
|
||||||
|
'name' => 'Seeded User',
|
||||||
|
'email' => 'seeded@user',
|
||||||
|
'password' => bcrypt('password'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,10 +17,9 @@ use Stancl\Tenancy\Events\UpdatingDomain;
|
||||||
use Stancl\Tenancy\Jobs\CreateDatabase;
|
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||||
use Stancl\Tenancy\Jobs\MigrateDatabase;
|
use Stancl\Tenancy\Jobs\MigrateDatabase;
|
||||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
|
use Stancl\Tenancy\Listeners\QueueableListener;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
test('listeners can be synchronous', function () {
|
test('listeners can be synchronous', function () {
|
||||||
Queue::fake();
|
Queue::fake();
|
||||||
Event::listen(TenantCreated::class, FooListener::class);
|
Event::listen(TenantCreated::class, FooListener::class);
|
||||||
|
|
@ -180,8 +179,12 @@ test('database is not migrated if creation is disabled', function () {
|
||||||
expect($this->hasFailed())->toBeFalse();
|
expect($this->hasFailed())->toBeFalse();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Helpers
|
class FooListener extends QueueableListener
|
||||||
function handle()
|
|
||||||
{
|
{
|
||||||
app()->instance('foo', 'bar');
|
public static $shouldQueue = false;
|
||||||
|
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
app()->instance('foo', 'bar');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
CacheTenancyBootstrapper::class,
|
CacheTenancyBootstrapper::class,
|
||||||
|
|
@ -51,3 +49,4 @@ test('global cache manager stores data in global cache', function () {
|
||||||
tenancy()->initialize($tenant1);
|
tenancy()->initialize($tenant1);
|
||||||
expect(cache('def'))->toBe('ghi');
|
expect(cache('def'))->toBe('ghi');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,13 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Stancl\Tenancy\Database\Concerns\MaintenanceMode;
|
||||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use Stancl\Tenancy\Middleware\CheckTenantForMaintenanceMode;
|
use Stancl\Tenancy\Middleware\CheckTenantForMaintenanceMode;
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
test('tenant can be in maintenance mode', function () {
|
test('tenant can be in maintenance mode', function () {
|
||||||
Route::get('/foo', function () {
|
Route::get('/foo', function () {
|
||||||
return 'bar';
|
return 'bar';
|
||||||
|
|
@ -31,3 +30,8 @@ test('tenant can be in maintenance mode', function () {
|
||||||
$this->withoutExceptionHandling()
|
$this->withoutExceptionHandling()
|
||||||
->get('http://acme.localhost/foo');
|
->get('http://acme.localhost/foo');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
class MaintenanceTenant extends Tenant
|
||||||
|
{
|
||||||
|
use MaintenanceMode;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
PathTenantResolver::$tenantParameterName = 'tenant';
|
PathTenantResolver::$tenantParameterName = 'tenant';
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,42 +1,3 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
uses(\Stancl\Tenancy\Tests\TestCase::class)->in('Etc', 'Features');
|
uses(Stancl\Tenancy\Tests\TestCase::class)->in(__DIR__);
|
||||||
|
|
||||||
/*
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
| Test Case
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
|
|
|
||||||
| The closure you provide to your test functions is always bound to a specific PHPUnit test
|
|
||||||
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
|
|
||||||
| need to change it using the "uses()" function to bind a different classes or traits.
|
|
||||||
|
|
|
||||||
*/
|
|
||||||
|
|
||||||
/** @link https://pestphp.com/docs/underlying-test-case */
|
|
||||||
|
|
||||||
/*
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
| Expectations
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
|
|
|
||||||
| When you're writing tests, you often need to check that values meet certain conditions. The
|
|
||||||
| "expect()" function gives you access to a set of "expectations" methods that you can use
|
|
||||||
| to assert different things. Of course, you may extend the Expectation API at any time.
|
|
||||||
|
|
|
||||||
*/
|
|
||||||
|
|
||||||
/** @link https://pestphp.com/docs/expectations#custom-expectations */
|
|
||||||
|
|
||||||
/*
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
| Functions
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
|
|
|
||||||
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
|
|
||||||
| project that you don't want to repeat in every file. Here you can also expose helpers as
|
|
||||||
| global functions to help you to reduce the number of lines of code in your test files.
|
|
||||||
|
|
|
||||||
*/
|
|
||||||
|
|
||||||
/** @link https://pestphp.com/docs/helpers */
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,11 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Spatie\Valuestore\Valuestore;
|
use Spatie\Valuestore\Valuestore;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
@ -22,8 +27,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
config([
|
config([
|
||||||
'tenancy.bootstrappers' => [
|
'tenancy.bootstrappers' => [
|
||||||
|
|
@ -78,11 +81,6 @@ test('tenant id is not passed to central queues', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @testWith [true]
|
|
||||||
* [false]
|
|
||||||
*/
|
|
||||||
test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
|
test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
|
||||||
withTenantDatabases();
|
withTenantDatabases();
|
||||||
withFailedJobs();
|
withFailedJobs();
|
||||||
|
|
@ -114,13 +112,8 @@ test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
|
||||||
$tenant->run(function () use ($user) {
|
$tenant->run(function () use ($user) {
|
||||||
expect($user->fresh()->name)->toBe('Bar');
|
expect($user->fresh()->name)->toBe('Bar');
|
||||||
});
|
});
|
||||||
});
|
})->with([true, false]);;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @testWith [true]
|
|
||||||
* [false]
|
|
||||||
*/
|
|
||||||
test('tenancy is initialized when retrying jobs', function (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');
|
||||||
|
|
@ -163,7 +156,7 @@ test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenan
|
||||||
$tenant->run(function () use ($user) {
|
$tenant->run(function () use ($user) {
|
||||||
expect($user->fresh()->name)->toBe('Bar');
|
expect($user->fresh()->name)->toBe('Bar');
|
||||||
});
|
});
|
||||||
});
|
})->with([true, false]);
|
||||||
|
|
||||||
test('the tenant used by the job doesnt change when the current tenant changes', function () {
|
test('the tenant used by the job doesnt change when the current tenant changes', function () {
|
||||||
$tenant1 = Tenant::create([
|
$tenant1 = Tenant::create([
|
||||||
|
|
@ -186,7 +179,6 @@ test('the tenant used by the job doesnt change when the current tenant changes',
|
||||||
expect($this->valuestore->get('tenant_id'))->toBe('The current tenant id is: acme');
|
expect($this->valuestore->get('tenant_id'))->toBe('The current tenant id is: acme');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Helpers
|
|
||||||
function createValueStore(): void
|
function createValueStore(): void
|
||||||
{
|
{
|
||||||
$valueStorePath = __DIR__ . '/Etc/tmp/queuetest.json';
|
$valueStorePath = __DIR__ . '/Etc/tmp/queuetest.json';
|
||||||
|
|
@ -235,27 +227,39 @@ function withTenantDatabases()
|
||||||
})->toListener());
|
})->toListener());
|
||||||
}
|
}
|
||||||
|
|
||||||
function __construct(Valuestore $valuestore, User $user = null)
|
class TestJob implements ShouldQueue
|
||||||
{
|
{
|
||||||
test()->valuestore = $valuestore;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
test()->user = $user;
|
|
||||||
|
/** @var Valuestore */
|
||||||
|
protected $valuestore;
|
||||||
|
|
||||||
|
/** @var User|null */
|
||||||
|
protected $user;
|
||||||
|
|
||||||
|
public function __construct(Valuestore $valuestore, User $user = null)
|
||||||
|
{
|
||||||
|
$this->valuestore = $valuestore;
|
||||||
|
$this->user = $user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
if ($this->valuestore->get('shouldFail')) {
|
||||||
|
$this->valuestore->put('shouldFail', false);
|
||||||
|
|
||||||
|
throw new Exception('failing');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->user) {
|
||||||
|
assert($this->user->getConnectionName() === 'tenant');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->valuestore->put('tenant_id', 'The current tenant id is: ' . tenant('id'));
|
||||||
|
|
||||||
|
if ($userName = $this->valuestore->get('userName')) {
|
||||||
|
$this->user->update(['name' => $userName]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handle()
|
|
||||||
{
|
|
||||||
if (test()->valuestore->get('shouldFail')) {
|
|
||||||
test()->valuestore->put('shouldFail', false);
|
|
||||||
|
|
||||||
throw new Exception('failing');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (test()->user) {
|
|
||||||
assert(test()->user->getConnectionName() === 'tenant');
|
|
||||||
}
|
|
||||||
|
|
||||||
test()->valuestore->put('tenant_id', 'The current tenant id is: ' . tenant('id'));
|
|
||||||
|
|
||||||
if ($userName = test()->valuestore->get('userName')) {
|
|
||||||
test()->user->update(['name' => $userName]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
config([
|
config([
|
||||||
'tenancy.central_domains' => [
|
'tenancy.central_domains' => [
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,10 @@ use Illuminate\Support\Facades\Queue;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Stancl\JobPipeline\JobPipeline;
|
use Stancl\JobPipeline\JobPipeline;
|
||||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||||
|
use Stancl\Tenancy\Contracts\Syncable;
|
||||||
|
use Stancl\Tenancy\Contracts\SyncMaster;
|
||||||
|
use Stancl\Tenancy\Database\Concerns\CentralConnection;
|
||||||
|
use Stancl\Tenancy\Database\Concerns\ResourceSyncing;
|
||||||
use Stancl\Tenancy\Database\Models\TenantPivot;
|
use Stancl\Tenancy\Database\Models\TenantPivot;
|
||||||
use Stancl\Tenancy\DatabaseConfig;
|
use Stancl\Tenancy\DatabaseConfig;
|
||||||
use Stancl\Tenancy\Events\SyncedResourceChangedInForeignDatabase;
|
use Stancl\Tenancy\Events\SyncedResourceChangedInForeignDatabase;
|
||||||
|
|
@ -24,8 +28,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
DatabaseTenancyBootstrapper::class,
|
DatabaseTenancyBootstrapper::class,
|
||||||
|
|
@ -45,7 +47,7 @@ beforeEach(function () {
|
||||||
UpdateSyncedResource::$shouldQueue = false; // global state cleanup
|
UpdateSyncedResource::$shouldQueue = false; // global state cleanup
|
||||||
Event::listen(SyncedResourceSaved::class, UpdateSyncedResource::class);
|
Event::listen(SyncedResourceSaved::class, UpdateSyncedResource::class);
|
||||||
|
|
||||||
$this->artisan('migrate', [
|
test()->artisan('migrate', [
|
||||||
'--path' => [
|
'--path' => [
|
||||||
__DIR__ . '/Etc/synced_resource_migrations',
|
__DIR__ . '/Etc/synced_resource_migrations',
|
||||||
__DIR__ . '/Etc/synced_resource_migrations/users',
|
__DIR__ . '/Etc/synced_resource_migrations/users',
|
||||||
|
|
@ -81,7 +83,7 @@ test('only the synced columns are updated in the central db', function () {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$tenant = ResourceTenant::create();
|
$tenant = ResourceTenant::create();
|
||||||
migrateTenants();
|
migrateTenantsResource();
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
|
@ -125,40 +127,11 @@ test('only the synced columns are updated in the central db', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('creating the resource in tenant database creates it in central database and creates the mapping', function () {
|
test('creating the resource in tenant database creates it in central database and creates the mapping', function () {
|
||||||
// Assert no user in central DB
|
creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase();
|
||||||
expect(ResourceUser::all())->toHaveCount(0);
|
|
||||||
|
|
||||||
$tenant = ResourceTenant::create();
|
|
||||||
migrateTenants();
|
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
|
||||||
|
|
||||||
// Create the same user in tenant DB
|
|
||||||
ResourceUser::create([
|
|
||||||
'global_id' => 'acme',
|
|
||||||
'name' => 'John Doe',
|
|
||||||
'email' => 'john@localhost',
|
|
||||||
'password' => 'secret',
|
|
||||||
'role' => 'commenter', // unsynced
|
|
||||||
]);
|
|
||||||
|
|
||||||
tenancy()->end();
|
|
||||||
|
|
||||||
// Asset user was created
|
|
||||||
expect(CentralUser::first()->global_id)->toBe('acme');
|
|
||||||
expect(CentralUser::first()->role)->toBe('commenter');
|
|
||||||
|
|
||||||
// Assert mapping was created
|
|
||||||
expect(CentralUser::first()->tenants)->toHaveCount(1);
|
|
||||||
|
|
||||||
// Assert role change doesn't cascade
|
|
||||||
CentralUser::first()->update(['role' => 'central superadmin']);
|
|
||||||
tenancy()->initialize($tenant);
|
|
||||||
expect(ResourceUser::first()->role)->toBe('commenter');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('trying to update synced resources from central context using tenant models results in an exception', function () {
|
test('trying to update synced resources from central context using tenant models results in an exception', function () {
|
||||||
$this->creating_the_resource_in_tenant_database_creates_it_in_central_database_and_creates_the_mapping();
|
creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase();
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
expect(tenancy()->initialized)->toBeFalse();
|
expect(tenancy()->initialized)->toBeFalse();
|
||||||
|
|
@ -179,7 +152,7 @@ test('attaching a tenant to the central resource triggers a pull from the tenant
|
||||||
$tenant = ResourceTenant::create([
|
$tenant = ResourceTenant::create([
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
]);
|
]);
|
||||||
migrateTenants();
|
migrateTenantsResource();
|
||||||
|
|
||||||
$tenant->run(function () {
|
$tenant->run(function () {
|
||||||
expect(ResourceUser::all())->toHaveCount(0);
|
expect(ResourceUser::all())->toHaveCount(0);
|
||||||
|
|
@ -192,7 +165,7 @@ test('attaching a tenant to the central resource triggers a pull from the tenant
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('attaching users to tenants d o e s n o t d o a n y t h i n g', function () {
|
test('attaching users to tenants does not do anything', function () {
|
||||||
$centralUser = CentralUser::create([
|
$centralUser = CentralUser::create([
|
||||||
'global_id' => 'acme',
|
'global_id' => 'acme',
|
||||||
'name' => 'John Doe',
|
'name' => 'John Doe',
|
||||||
|
|
@ -204,7 +177,7 @@ test('attaching users to tenants d o e s n o t d o a n y t h i n g', function ()
|
||||||
$tenant = ResourceTenant::create([
|
$tenant = ResourceTenant::create([
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
]);
|
]);
|
||||||
migrateTenants();
|
migrateTenantsResource();
|
||||||
|
|
||||||
$tenant->run(function () {
|
$tenant->run(function () {
|
||||||
expect(ResourceUser::all())->toHaveCount(0);
|
expect(ResourceUser::all())->toHaveCount(0);
|
||||||
|
|
@ -239,7 +212,7 @@ test('resources are synced only to workspaces that have the resource', function
|
||||||
$t3 = ResourceTenant::create([
|
$t3 = ResourceTenant::create([
|
||||||
'id' => 't3',
|
'id' => 't3',
|
||||||
]);
|
]);
|
||||||
migrateTenants();
|
migrateTenantsResource();
|
||||||
|
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
$centralUser->tenants()->attach('t2');
|
$centralUser->tenants()->attach('t2');
|
||||||
|
|
@ -261,7 +234,7 @@ test('resources are synced only to workspaces that have the resource', function
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
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 () {
|
test('when a resource exists in other tenant dbs but is created in a tenant db the synced columns are updated in the other dbs', function () {
|
||||||
// create shared resource
|
// create shared resource
|
||||||
$centralUser = CentralUser::create([
|
$centralUser = CentralUser::create([
|
||||||
'global_id' => 'acme',
|
'global_id' => 'acme',
|
||||||
|
|
@ -277,7 +250,7 @@ test('when a resource exists in other tenant dbs but is c r e a t e d in a tenan
|
||||||
$t2 = ResourceTenant::create([
|
$t2 = ResourceTenant::create([
|
||||||
'id' => 't2',
|
'id' => 't2',
|
||||||
]);
|
]);
|
||||||
migrateTenants();
|
migrateTenantsResource();
|
||||||
|
|
||||||
// Copy (cascade) user to t1 DB
|
// Copy (cascade) user to t1 DB
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
@ -325,7 +298,7 @@ test('the synced columns are updated in other tenant dbs where the resource exis
|
||||||
$t3 = ResourceTenant::create([
|
$t3 = ResourceTenant::create([
|
||||||
'id' => 't3',
|
'id' => 't3',
|
||||||
]);
|
]);
|
||||||
migrateTenants();
|
migrateTenantsResource();
|
||||||
|
|
||||||
// Copy (cascade) user to t1 DB
|
// Copy (cascade) user to t1 DB
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
@ -380,7 +353,7 @@ test('when the resource doesnt exist in the tenant db non synced columns will ca
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
migrateTenants();
|
migrateTenantsResource();
|
||||||
|
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
||||||
|
|
@ -394,7 +367,7 @@ test('when the resource doesnt exist in the central db non synced columns will b
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
migrateTenants();
|
migrateTenantsResource();
|
||||||
|
|
||||||
$t1->run(function () {
|
$t1->run(function () {
|
||||||
ResourceUser::create([
|
ResourceUser::create([
|
||||||
|
|
@ -416,7 +389,7 @@ test('the listener can be queued', function () {
|
||||||
'id' => 't1',
|
'id' => 't1',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
migrateTenants();
|
migrateTenantsResource();
|
||||||
|
|
||||||
Queue::assertNothingPushed();
|
Queue::assertNothingPushed();
|
||||||
|
|
||||||
|
|
@ -455,7 +428,7 @@ test('an event is fired for all touched resources', function () {
|
||||||
$t3 = ResourceTenant::create([
|
$t3 = ResourceTenant::create([
|
||||||
'id' => 't3',
|
'id' => 't3',
|
||||||
]);
|
]);
|
||||||
migrateTenants();
|
migrateTenantsResource();
|
||||||
|
|
||||||
// Copy (cascade) user to t1 DB
|
// Copy (cascade) user to t1 DB
|
||||||
$centralUser->tenants()->attach('t1');
|
$centralUser->tenants()->attach('t1');
|
||||||
|
|
@ -529,8 +502,42 @@ test('an event is fired for all touched resources', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Helpers
|
// todo@tests
|
||||||
function migrateTenants()
|
function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase()
|
||||||
|
{
|
||||||
|
// Assert no user in central DB
|
||||||
|
expect(ResourceUser::all())->toHaveCount(0);
|
||||||
|
|
||||||
|
$tenant = ResourceTenant::create();
|
||||||
|
migrateTenantsResource();
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
// Create the same user in tenant DB
|
||||||
|
ResourceUser::create([
|
||||||
|
'global_id' => 'acme',
|
||||||
|
'name' => 'John Doe',
|
||||||
|
'email' => 'john@localhost',
|
||||||
|
'password' => 'secret',
|
||||||
|
'role' => 'commenter', // unsynced
|
||||||
|
]);
|
||||||
|
|
||||||
|
tenancy()->end();
|
||||||
|
|
||||||
|
// Asset user was created
|
||||||
|
expect(CentralUser::first()->global_id)->toBe('acme');
|
||||||
|
expect(CentralUser::first()->role)->toBe('commenter');
|
||||||
|
|
||||||
|
// Assert mapping was created
|
||||||
|
expect(CentralUser::first()->tenants)->toHaveCount(1);
|
||||||
|
|
||||||
|
// Assert role change doesn't cascade
|
||||||
|
CentralUser::first()->update(['role' => 'central superadmin']);
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
expect(ResourceUser::first()->role)->toBe('commenter');
|
||||||
|
}
|
||||||
|
|
||||||
|
function migrateTenantsResource()
|
||||||
{
|
{
|
||||||
test()->artisan('tenants:migrate', [
|
test()->artisan('tenants:migrate', [
|
||||||
'--path' => __DIR__ . '/Etc/synced_resource_migrations/users',
|
'--path' => __DIR__ . '/Etc/synced_resource_migrations/users',
|
||||||
|
|
@ -538,43 +545,88 @@ function migrateTenants()
|
||||||
])->assertExitCode(0);
|
])->assertExitCode(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
function users()
|
class ResourceTenant extends Tenant
|
||||||
{
|
{
|
||||||
return test()->belongsToMany(CentralUser::class, 'tenant_users', 'tenant_id', 'global_user_id', 'id', 'global_id')
|
public function users()
|
||||||
->using(TenantPivot::class);
|
{
|
||||||
|
return $this->belongsToMany(CentralUser::class, 'tenant_users', 'tenant_id', 'global_user_id', 'id', 'global_id')
|
||||||
|
->using(TenantPivot::class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function tenants(): BelongsToMany
|
class CentralUser extends Model implements SyncMaster
|
||||||
{
|
{
|
||||||
return test()->belongsToMany(ResourceTenant::class, 'tenant_users', 'global_user_id', 'tenant_id', 'global_id')
|
use ResourceSyncing, CentralConnection;
|
||||||
->using(TenantPivot::class);
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTenantModelName(): string
|
||||||
|
{
|
||||||
|
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',
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTenantModelName(): string
|
class ResourceUser extends Model implements Syncable
|
||||||
{
|
{
|
||||||
return ResourceUser::class;
|
use ResourceSyncing;
|
||||||
}
|
|
||||||
|
|
||||||
function getGlobalIdentifierKey()
|
protected $table = 'users';
|
||||||
{
|
protected $guarded = [];
|
||||||
return test()->getAttribute(test()->getGlobalIdentifierKeyName());
|
public $timestamps = false;
|
||||||
}
|
|
||||||
|
|
||||||
function getGlobalIdentifierKeyName(): string
|
public function getGlobalIdentifierKey()
|
||||||
{
|
{
|
||||||
return 'global_id';
|
return $this->getAttribute($this->getGlobalIdentifierKeyName());
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCentralModelName(): string
|
public function getGlobalIdentifierKeyName(): string
|
||||||
{
|
{
|
||||||
return CentralUser::class;
|
return 'global_id';
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSyncedAttributeNames(): array
|
public function getCentralModelName(): string
|
||||||
{
|
{
|
||||||
return [
|
return CentralUser::class;
|
||||||
'name',
|
}
|
||||||
'password',
|
|
||||||
'email',
|
public function getSyncedAttributeNames(): array
|
||||||
];
|
{
|
||||||
|
return [
|
||||||
|
'name',
|
||||||
|
'password',
|
||||||
|
'email',
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
Route::group([
|
Route::group([
|
||||||
'middleware' => [StartSession::class, InitializeTenancyBySubdomain::class, ScopeSessions::class],
|
'middleware' => [StartSession::class, InitializeTenancyBySubdomain::class, ScopeSessions::class],
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,15 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\QueryException;
|
use Illuminate\Database\QueryException;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
use Illuminate\Support\Facades\Validator;
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use Stancl\Tenancy\Database\Concerns\BelongsToPrimaryModel;
|
||||||
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
||||||
|
use Stancl\Tenancy\Database\Concerns\HasScopedValidationRules;
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
use Stancl\Tenancy\Tests\Etc\Tenant as TestTenant;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
BelongsToTenant::$tenantIdColumn = 'tenant_id';
|
BelongsToTenant::$tenantIdColumn = 'tenant_id';
|
||||||
|
|
@ -35,52 +37,11 @@ beforeEach(function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('primary models are scoped to the current tenant', function () {
|
test('primary models are scoped to the current tenant', function () {
|
||||||
// acme context
|
primaryModelsScopedToCurrentTenant();
|
||||||
tenancy()->initialize($acme = Tenant::create([
|
|
||||||
'id' => 'acme',
|
|
||||||
]));
|
|
||||||
|
|
||||||
$post = Post::create(['text' => 'Foo']);
|
|
||||||
|
|
||||||
expect($post->tenant_id)->toBe('acme');
|
|
||||||
expect($post->tenant->id)->toBe('acme');
|
|
||||||
|
|
||||||
$post = Post::first();
|
|
||||||
|
|
||||||
expect($post->tenant_id)->toBe('acme');
|
|
||||||
expect($post->tenant->id)->toBe('acme');
|
|
||||||
|
|
||||||
// ======================================
|
|
||||||
// foobar context
|
|
||||||
tenancy()->initialize($foobar = Tenant::create([
|
|
||||||
'id' => 'foobar',
|
|
||||||
]));
|
|
||||||
|
|
||||||
$post = Post::create(['text' => 'Bar']);
|
|
||||||
|
|
||||||
expect($post->tenant_id)->toBe('foobar');
|
|
||||||
expect($post->tenant->id)->toBe('foobar');
|
|
||||||
|
|
||||||
$post = Post::first();
|
|
||||||
|
|
||||||
expect($post->tenant_id)->toBe('foobar');
|
|
||||||
expect($post->tenant->id)->toBe('foobar');
|
|
||||||
|
|
||||||
// ======================================
|
|
||||||
// acme context again
|
|
||||||
|
|
||||||
tenancy()->initialize($acme);
|
|
||||||
|
|
||||||
$post = Post::first();
|
|
||||||
expect($post->tenant_id)->toBe('acme');
|
|
||||||
expect($post->tenant->id)->toBe('acme');
|
|
||||||
|
|
||||||
// Assert foobar models are inaccessible in acme context
|
|
||||||
expect(Post::count())->toBe(1);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('primary models are not scoped in the central context', function () {
|
test('primary models are not scoped in the central context', function () {
|
||||||
$this->primary_models_are_scoped_to_the_current_tenant();
|
primaryModelsScopedToCurrentTenant();
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
|
|
@ -88,32 +49,11 @@ test('primary models are not scoped in the central context', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('secondary models are scoped to the current tenant when accessed via primary model', function () {
|
test('secondary models are scoped to the current tenant when accessed via primary model', function () {
|
||||||
// acme context
|
secondaryModelsAreScopedToCurrentTenant();
|
||||||
tenancy()->initialize($acme = Tenant::create([
|
|
||||||
'id' => 'acme',
|
|
||||||
]));
|
|
||||||
|
|
||||||
$post = Post::create(['text' => 'Foo']);
|
|
||||||
$post->comments()->create(['text' => 'Comment text']);
|
|
||||||
|
|
||||||
// ================
|
|
||||||
// foobar context
|
|
||||||
tenancy()->initialize($foobar = Tenant::create([
|
|
||||||
'id' => 'foobar',
|
|
||||||
]));
|
|
||||||
|
|
||||||
$post = Post::create(['text' => 'Bar']);
|
|
||||||
$post->comments()->create(['text' => 'Comment text 2']);
|
|
||||||
|
|
||||||
// ================
|
|
||||||
// acme context again
|
|
||||||
tenancy()->initialize($acme);
|
|
||||||
expect(Post::count())->toBe(1);
|
|
||||||
expect(Post::first()->comments->count())->toBe(1);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('secondary models are n o t scoped to the current tenant when accessed directly', function () {
|
test('secondary models are not scoped to the current tenant when accessed directly', function () {
|
||||||
$this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model();
|
secondaryModelsAreScopedToCurrentTenant();
|
||||||
|
|
||||||
// We're in acme context
|
// We're in acme context
|
||||||
expect(tenant('id'))->toBe('acme');
|
expect(tenant('id'))->toBe('acme');
|
||||||
|
|
@ -121,7 +61,7 @@ test('secondary models are n o t scoped to the current tenant when accessed dire
|
||||||
expect(Comment::count())->toBe(2);
|
expect(Comment::count())->toBe(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
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 () {
|
test('secondary models a r e scoped to the current tenant when accessed directly and parent relationship traitis used', function () {
|
||||||
$acme = Tenant::create([
|
$acme = Tenant::create([
|
||||||
'id' => 'acme',
|
'id' => 'acme',
|
||||||
]);
|
]);
|
||||||
|
|
@ -153,8 +93,8 @@ test('secondary models a r e scoped to the current tenant when accessed directly
|
||||||
expect(ScopedComment::count())->toBe(2);
|
expect(ScopedComment::count())->toBe(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('secondary models are n o t scoped in the central context', function () {
|
test('secondary models are not scoped in the central context', function () {
|
||||||
$this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model();
|
secondaryModelsAreScopedToCurrentTenant();
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
|
|
@ -287,23 +227,128 @@ test('the model returned by the tenant helper has unique and exists validation r
|
||||||
expect($existsFails)->toBeFalse();
|
expect($existsFails)->toBeFalse();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Helpers
|
// todo@tests
|
||||||
function comments()
|
function primaryModelsScopedToCurrentTenant()
|
||||||
{
|
{
|
||||||
return test()->hasMany(Comment::class);
|
// acme context
|
||||||
|
tenancy()->initialize($acme = Tenant::create([
|
||||||
|
'id' => 'acme',
|
||||||
|
]));
|
||||||
|
|
||||||
|
$post = Post::create(['text' => 'Foo']);
|
||||||
|
|
||||||
|
expect($post->tenant_id)->toBe('acme');
|
||||||
|
expect($post->tenant->id)->toBe('acme');
|
||||||
|
|
||||||
|
$post = Post::first();
|
||||||
|
|
||||||
|
expect($post->tenant_id)->toBe('acme');
|
||||||
|
expect($post->tenant->id)->toBe('acme');
|
||||||
|
|
||||||
|
// ======================================
|
||||||
|
// foobar context
|
||||||
|
tenancy()->initialize($foobar = Tenant::create([
|
||||||
|
'id' => 'foobar',
|
||||||
|
]));
|
||||||
|
|
||||||
|
$post = Post::create(['text' => 'Bar']);
|
||||||
|
|
||||||
|
expect($post->tenant_id)->toBe('foobar');
|
||||||
|
expect($post->tenant->id)->toBe('foobar');
|
||||||
|
|
||||||
|
$post = Post::first();
|
||||||
|
|
||||||
|
expect($post->tenant_id)->toBe('foobar');
|
||||||
|
expect($post->tenant->id)->toBe('foobar');
|
||||||
|
|
||||||
|
// ======================================
|
||||||
|
// acme context again
|
||||||
|
|
||||||
|
tenancy()->initialize($acme);
|
||||||
|
|
||||||
|
$post = Post::first();
|
||||||
|
expect($post->tenant_id)->toBe('acme');
|
||||||
|
expect($post->tenant->id)->toBe('acme');
|
||||||
|
|
||||||
|
// Assert foobar models are inaccessible in acme context
|
||||||
|
expect(Post::count())->toBe(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function scoped_comments()
|
// todo@tests
|
||||||
|
function secondaryModelsAreScopedToCurrentTenant()
|
||||||
{
|
{
|
||||||
return test()->hasMany(Comment::class);
|
// acme context
|
||||||
|
tenancy()->initialize($acme = Tenant::create([
|
||||||
|
'id' => 'acme',
|
||||||
|
]));
|
||||||
|
|
||||||
|
$post = Post::create(['text' => 'Foo']);
|
||||||
|
$post->comments()->create(['text' => 'Comment text']);
|
||||||
|
|
||||||
|
// ================
|
||||||
|
// foobar context
|
||||||
|
tenancy()->initialize($foobar = Tenant::create([
|
||||||
|
'id' => 'foobar',
|
||||||
|
]));
|
||||||
|
|
||||||
|
$post = Post::create(['text' => 'Bar']);
|
||||||
|
$post->comments()->create(['text' => 'Comment text 2']);
|
||||||
|
|
||||||
|
// ================
|
||||||
|
// acme context again
|
||||||
|
tenancy()->initialize($acme);
|
||||||
|
expect(Post::count())->toBe(1);
|
||||||
|
expect(Post::first()->comments->count())->toBe(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function post()
|
class Tenant extends TestTenant
|
||||||
{
|
{
|
||||||
return test()->belongsTo(Post::class);
|
use HasScopedValidationRules;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRelationshipToPrimaryModel(): string
|
class Post extends Model
|
||||||
{
|
{
|
||||||
return 'post';
|
use BelongsToTenant;
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
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';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class GlobalResource extends Model
|
||||||
|
{
|
||||||
|
protected $guarded = [];
|
||||||
|
public $timestamps = false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
||||||
use Stancl\Tenancy\Exceptions\NotASubdomainException;
|
use Stancl\Tenancy\Exceptions\NotASubdomainException;
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
|
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
|
||||||
|
use Stancl\Tenancy\Database\Models;
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
// Global state cleanup after some tests
|
// Global state cleanup after some tests
|
||||||
|
|
@ -125,3 +125,8 @@ test('central domain is not a subdomain', function () {
|
||||||
->withoutExceptionHandling()
|
->withoutExceptionHandling()
|
||||||
->get('http://localhost/foo/abc/xyz');
|
->get('http://localhost/foo/abc/xyz');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
class SubdomainTenant extends Models\Tenant
|
||||||
|
{
|
||||||
|
use HasDomains;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
config(['tenancy.bootstrappers' => [
|
config(['tenancy.bootstrappers' => [
|
||||||
FilesystemTenancyBootstrapper::class,
|
FilesystemTenancyBootstrapper::class,
|
||||||
|
|
@ -96,11 +94,8 @@ test('asset helper tenancy can be disabled', function () {
|
||||||
expect(asset('foo'))->toBe($original);
|
expect(asset('foo'))->toBe($original);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Helpers
|
|
||||||
function getEnvironmentSetUp($app)
|
function getEnvironmentSetUp($app)
|
||||||
{
|
{
|
||||||
parent::getEnvironmentSetUp($app);
|
|
||||||
|
|
||||||
$app->booted(function () {
|
$app->booted(function () {
|
||||||
if (file_exists(base_path('routes/tenant.php'))) {
|
if (file_exists(base_path('routes/tenant.php'))) {
|
||||||
Route::middleware(['web'])
|
Route::middleware(['web'])
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
test('commands run globally are tenant aware and return valid exit code', function () {
|
test('commands run globally are tenant aware and return valid exit code', function () {
|
||||||
$tenant1 = Tenant::create();
|
$tenant1 = Tenant::create();
|
||||||
$tenant2 = Tenant::create();
|
$tenant2 = Tenant::create();
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
test('databases can be created and deleted', function ($driver, $databaseManager) {
|
test('databases can be created and deleted', function ($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;
|
||||||
|
|
@ -240,7 +238,6 @@ dataset('database_manager_provider', [
|
||||||
['sqlsrv', MicrosoftSQLDatabaseManager::class]
|
['sqlsrv', MicrosoftSQLDatabaseManager::class]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Helpers
|
|
||||||
function createUsersTable()
|
function createUsersTable()
|
||||||
{
|
{
|
||||||
Schema::create('users', function (Blueprint $table) {
|
Schema::create('users', function (Blueprint $table) {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
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;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
@ -18,8 +19,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
test('created event is dispatched', function () {
|
test('created event is dispatched', function () {
|
||||||
Event::fake([TenantCreated::class]);
|
Event::fake([TenantCreated::class]);
|
||||||
|
|
||||||
|
|
@ -142,28 +141,38 @@ test('a command can be run on a collection of tenants', function () {
|
||||||
expect(Tenant::find('t2')->foo)->toBe('xyz');
|
expect(Tenant::find('t2')->foo)->toBe('xyz');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Helpers
|
class MyTenant extends Tenant
|
||||||
function getTenantKeyName(): string
|
|
||||||
{
|
{
|
||||||
return 'id';
|
protected $table = 'tenants';
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTenantKey()
|
class AnotherTenant extends Model implements Contracts\Tenant
|
||||||
{
|
{
|
||||||
return test()->getAttribute('id');
|
protected $guarded = [];
|
||||||
}
|
protected $table = 'tenants';
|
||||||
|
|
||||||
function run(callable $callback)
|
public function getTenantKeyName(): string
|
||||||
{
|
{
|
||||||
$callback();
|
return 'id';
|
||||||
}
|
}
|
||||||
|
|
||||||
function getInternal(string $key)
|
public function getTenantKey()
|
||||||
{
|
{
|
||||||
return test()->$key;
|
return $this->getAttribute('id');
|
||||||
}
|
}
|
||||||
|
|
||||||
function setInternal(string $key, $value)
|
public function run(callable $callback)
|
||||||
{
|
{
|
||||||
test()->$key = $value;
|
$callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getInternal(string $key)
|
||||||
|
{
|
||||||
|
return $this->$key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setInternal(string $key, $value)
|
||||||
|
{
|
||||||
|
$this->$key = $value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,8 +22,7 @@ use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
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;
|
||||||
|
use Illuminate\Foundation\Auth\User as Authenticable;
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
$this->artisan('migrate', [
|
$this->artisan('migrate', [
|
||||||
|
|
@ -224,7 +223,6 @@ test('impersonation works with multiple models and guards', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Helpers
|
|
||||||
function migrateTenants()
|
function migrateTenants()
|
||||||
{
|
{
|
||||||
test()->artisan('tenants:migrate')->assertExitCode(0);
|
test()->artisan('tenants:migrate')->assertExitCode(0);
|
||||||
|
|
@ -253,3 +251,15 @@ function getRoutes($loginRoute = true, $authGuard = 'web'): Closure
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ImpersonationUser extends Authenticable
|
||||||
|
{
|
||||||
|
protected $guarded = [];
|
||||||
|
protected $table = 'users';
|
||||||
|
}
|
||||||
|
|
||||||
|
class AnotherImpersonationUser extends Authenticable
|
||||||
|
{
|
||||||
|
protected $guarded = [];
|
||||||
|
protected $table = 'users';
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,6 @@ 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;
|
||||||
|
|
||||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
InitializeTenancyByDomain::$onFail = null;
|
InitializeTenancyByDomain::$onFail = null;
|
||||||
});
|
});
|
||||||
|
|
@ -44,7 +42,30 @@ test('making one route universal doesnt make all routes universal', function ()
|
||||||
return tenant('id');
|
return tenant('id');
|
||||||
})->middleware(InitializeTenancyByDomain::class);
|
})->middleware(InitializeTenancyByDomain::class);
|
||||||
|
|
||||||
$this->a_route_can_work_in_both_central_and_tenant_context();
|
Route::middlewareGroup('universal', []);
|
||||||
|
config(['tenancy.features' => [UniversalRoutes::class]]);
|
||||||
|
|
||||||
|
Route::get('/foo', function () {
|
||||||
|
return tenancy()->initialized
|
||||||
|
? 'Tenancy is initialized.'
|
||||||
|
: 'Tenancy is not initialized.';
|
||||||
|
})->middleware(['universal', InitializeTenancyByDomain::class]);
|
||||||
|
|
||||||
|
$this->get('http://localhost/foo')
|
||||||
|
->assertSuccessful()
|
||||||
|
->assertSee('Tenancy is not initialized.');
|
||||||
|
|
||||||
|
$tenant = Tenant::create([
|
||||||
|
'id' => 'acme',
|
||||||
|
]);
|
||||||
|
$tenant->domains()->create([
|
||||||
|
'domain' => 'acme.localhost',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->get('http://acme.localhost/foo')
|
||||||
|
->assertSuccessful()
|
||||||
|
->assertSee('Tenancy is initialized.');
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
$this->get('http://localhost/bar')
|
$this->get('http://localhost/bar')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue