mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 07:54:03 +00:00
Add auto-seeder tests
This commit is contained in:
parent
7e2eed88da
commit
ee73ac5dce
3 changed files with 45 additions and 5 deletions
|
|
@ -22,7 +22,7 @@ class QueuedTenantDatabaseSeeder implements ShouldQueue
|
||||||
/** @var array */
|
/** @var array */
|
||||||
protected $seederClassParameter = [];
|
protected $seederClassParameter = [];
|
||||||
|
|
||||||
public function __construct(Tenant $tenant, $seederClassName = '')
|
public function __construct(Tenant $tenant, string $seederClassName)
|
||||||
{
|
{
|
||||||
$this->tenantId = $tenant->id;
|
$this->tenantId = $tenant->id;
|
||||||
$this->seederClassParameter = ! empty($seederClassName) ? ['--class' => $seederClassName] : [];
|
$this->seederClassParameter = ! empty($seederClassName) ? ['--class' => $seederClassName] : [];
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ use Illuminate\Support\Collection;
|
||||||
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
|
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
|
||||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
||||||
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseMigrator;
|
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseMigrator;
|
||||||
|
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseSeeder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal Class is subject to breaking changes in minor and patch versions.
|
* @internal Class is subject to breaking changes in minor and patch versions.
|
||||||
|
|
@ -67,7 +68,7 @@ class TenantManager
|
||||||
$afterCreating = [];
|
$afterCreating = [];
|
||||||
|
|
||||||
if ($this->shouldMigrateAfterCreation()) {
|
if ($this->shouldMigrateAfterCreation()) {
|
||||||
$afterCreating += $this->databaseCreationQueued() ? [
|
$afterCreating = array_merge($afterCreating, $this->databaseCreationQueued() ? [
|
||||||
new QueuedTenantDatabaseMigrator($tenant),
|
new QueuedTenantDatabaseMigrator($tenant),
|
||||||
] : [
|
] : [
|
||||||
function () use ($tenant) {
|
function () use ($tenant) {
|
||||||
|
|
@ -75,13 +76,13 @@ class TenantManager
|
||||||
'--tenants' => [$tenant['id']],
|
'--tenants' => [$tenant['id']],
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
];
|
]);
|
||||||
|
|
||||||
if ($this->shouldSeedAfterMigration()) {
|
if ($this->shouldSeedAfterMigration()) {
|
||||||
$seederClassName = $this->getSeederRootClass();
|
$seederClassName = $this->getSeederRootClass();
|
||||||
$seederClassParameter = ! empty($seederClassName) ? ['--class' => $seederClassName] : [];
|
$seederClassParameter = ! empty($seederClassName) ? ['--class' => $seederClassName] : [];
|
||||||
|
|
||||||
$afterCreating += $this->databaseCreationQueued() ? [
|
$afterCreating = array_merge($afterCreating, $this->databaseCreationQueued() ? [
|
||||||
new QueuedTenantDatabaseSeeder($tenant, $seederClassName),
|
new QueuedTenantDatabaseSeeder($tenant, $seederClassName),
|
||||||
] : [
|
] : [
|
||||||
function () use ($tenant, $seederClassParameter) {
|
function () use ($tenant, $seederClassParameter) {
|
||||||
|
|
@ -89,7 +90,7 @@ class TenantManager
|
||||||
'--tenants' => [$tenant['id']],
|
'--tenants' => [$tenant['id']],
|
||||||
] + $seederClassParameter);
|
] + $seederClassParameter);
|
||||||
},
|
},
|
||||||
];
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ use Stancl\Tenancy\Exceptions\TenantDoesNotExistException;
|
||||||
use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException;
|
use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException;
|
||||||
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseCreator;
|
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseCreator;
|
||||||
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseMigrator;
|
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseMigrator;
|
||||||
|
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseSeeder;
|
||||||
use Stancl\Tenancy\Tenant;
|
use Stancl\Tenancy\Tenant;
|
||||||
use Stancl\Tenancy\TenantManager;
|
use Stancl\Tenancy\TenantManager;
|
||||||
|
|
||||||
|
|
@ -235,6 +236,25 @@ class TenantManagerTest extends TestCase
|
||||||
$tenant2 = Tenant::create(['bar.localhost']);
|
$tenant2 = Tenant::create(['bar.localhost']);
|
||||||
tenancy()->initialize($tenant2);
|
tenancy()->initialize($tenant2);
|
||||||
$this->assertTrue(\Schema::hasTable('users'));
|
$this->assertTrue(\Schema::hasTable('users'));
|
||||||
|
$this->assertCount(0, \DB::select('select * from users'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function automatic_seeding_works()
|
||||||
|
{
|
||||||
|
$tenant = Tenant::create(['foo.localhost']);
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
$this->assertFalse(\Schema::hasTable('users'));
|
||||||
|
|
||||||
|
config([
|
||||||
|
'tenancy.migrate_after_creation' => true,
|
||||||
|
'tenancy.seed_after_migration' => true,
|
||||||
|
'tenancy.seeder_class' => \Stancl\Tenancy\Tests\Etc\ExampleSeeder::class,
|
||||||
|
]);
|
||||||
|
$tenant2 = Tenant::create(['bar.localhost']);
|
||||||
|
tenancy()->initialize($tenant2);
|
||||||
|
$this->assertTrue(\Schema::hasTable('users'));
|
||||||
|
$this->assertCount(1, \DB::select('select * from users'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|
@ -272,6 +292,25 @@ class TenantManagerTest extends TestCase
|
||||||
// $this->assertTrue(\Schema::hasTable('users'));
|
// $this->assertTrue(\Schema::hasTable('users'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function auto_seeding_is_queued_when_enabled()
|
||||||
|
{
|
||||||
|
Queue::fake();
|
||||||
|
|
||||||
|
config([
|
||||||
|
'tenancy.queue_database_creation' => true,
|
||||||
|
'tenancy.migrate_after_creation' => true,
|
||||||
|
'tenancy.seed_after_migration' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$tenant = Tenant::new()->save();
|
||||||
|
|
||||||
|
Queue::assertPushedWithChain(QueuedTenantDatabaseCreator::class, [
|
||||||
|
QueuedTenantDatabaseMigrator::class,
|
||||||
|
QueuedTenantDatabaseSeeder::class,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function TenantDoesNotExistException_is_thrown_when_find_is_called_on_an_id_that_does_not_belong_to_any_tenant()
|
public function TenantDoesNotExistException_is_thrown_when_find_is_called_on_an_id_that_does_not_belong_to_any_tenant()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue