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

Refine seeding tests (#1)

Refine seeding tests
This commit is contained in:
Samuel Štancl 2019-10-17 19:00:05 +02:00 committed by Chris Brown
parent ee73ac5dce
commit 7ebe9450fb
4 changed files with 33 additions and 34 deletions

View file

@ -92,7 +92,9 @@ return [
'queue_database_creation' => false, 'queue_database_creation' => false,
'migrate_after_creation' => false, // run migrations after creating a tenant 'migrate_after_creation' => false, // run migrations after creating a tenant
'seed_after_migration' => false, // should the seeder run after automatic migration 'seed_after_migration' => false, // should the seeder run after automatic migration
'seeder_class' => 'DatabaseSeeder', // root seeder class to run after automatic migrations, eg: 'DatabaseSeeder' 'seeder_parameters' => [
'--class' => 'DatabaseSeeder', // root seeder class to run after automatic migrations, eg: 'DatabaseSeeder'
],
'queue_database_deletion' => false, 'queue_database_deletion' => false,
'delete_database_after_tenant_deletion' => false, // delete the tenant's database after deleting the tenant 'delete_database_after_tenant_deletion' => false, // delete the tenant's database after deleting the tenant
'unique_id_generator' => Stancl\Tenancy\UniqueIDGenerators\UUIDGenerator::class, 'unique_id_generator' => Stancl\Tenancy\UniqueIDGenerators\UUIDGenerator::class,

View file

@ -20,12 +20,12 @@ class QueuedTenantDatabaseSeeder implements ShouldQueue
protected $tenantId; protected $tenantId;
/** @var array */ /** @var array */
protected $seederClassParameter = []; protected $seederParameters = [];
public function __construct(Tenant $tenant, string $seederClassName) public function __construct(Tenant $tenant, $seederParameters = [])
{ {
$this->tenantId = $tenant->id; $this->tenantId = $tenant->id;
$this->seederClassParameter = ! empty($seederClassName) ? ['--class' => $seederClassName] : []; $this->seederParameters = $seederParameters;
} }
/** /**
@ -37,6 +37,6 @@ class QueuedTenantDatabaseSeeder implements ShouldQueue
{ {
Artisan::call('tenants:seed', [ Artisan::call('tenants:seed', [
'--tenants' => [$this->tenantId], '--tenants' => [$this->tenantId],
] + $this->seederClassParameter); ] + $this->seederParameters);
} }
} }

View file

@ -68,30 +68,23 @@ class TenantManager
$afterCreating = []; $afterCreating = [];
if ($this->shouldMigrateAfterCreation()) { if ($this->shouldMigrateAfterCreation()) {
$afterCreating = array_merge($afterCreating, $this->databaseCreationQueued() ? [ $afterCreating[] = $this->databaseCreationQueued()
new QueuedTenantDatabaseMigrator($tenant), ? new QueuedTenantDatabaseMigrator($tenant)
] : [ : function () use ($tenant) {
function () use ($tenant) {
$this->artisan->call('tenants:migrate', [ $this->artisan->call('tenants:migrate', [
'--tenants' => [$tenant['id']], '--tenants' => [$tenant['id']],
]); ]);
}, };
]); }
if ($this->shouldSeedAfterMigration()) { if ($this->shouldSeedAfterMigration()) {
$seederClassName = $this->getSeederRootClass(); $afterCreating[] = $this->databaseCreationQueued()
$seederClassParameter = ! empty($seederClassName) ? ['--class' => $seederClassName] : []; ? new QueuedTenantDatabaseSeeder($tenant, $this->getSeederParameters())
: function () use ($tenant) {
$afterCreating = array_merge($afterCreating, $this->databaseCreationQueued() ? [
new QueuedTenantDatabaseSeeder($tenant, $seederClassName),
] : [
function () use ($tenant, $seederClassParameter) {
$this->artisan->call('tenants:seed', [ $this->artisan->call('tenants:seed', [
'--tenants' => [$tenant['id']], '--tenants' => [$tenant['id']],
] + $seederClassParameter); ] + $this->getSeederParameters());
}, };
]);
}
} }
$this->database->createDatabase($tenant, $afterCreating); $this->database->createDatabase($tenant, $afterCreating);
@ -352,9 +345,9 @@ class TenantManager
return $this->app['config']['tenancy.delete_database_after_tenant_deletion'] ?? false; return $this->app['config']['tenancy.delete_database_after_tenant_deletion'] ?? false;
} }
public function getSeederRootClass() public function getSeederParameters()
{ {
return $this->app['config']['tenancy.seeder_class'] ?? null; return $this->app['config']['tenancy.seeder_parameters'] ?? [];
} }
/** /**

View file

@ -15,6 +15,7 @@ use Stancl\Tenancy\Jobs\QueuedTenantDatabaseMigrator;
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseSeeder; use Stancl\Tenancy\Jobs\QueuedTenantDatabaseSeeder;
use Stancl\Tenancy\Tenant; use Stancl\Tenancy\Tenant;
use Stancl\Tenancy\TenantManager; use Stancl\Tenancy\TenantManager;
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
class TenantManagerTest extends TestCase class TenantManagerTest extends TestCase
{ {
@ -242,19 +243,22 @@ class TenantManagerTest extends TestCase
/** @test */ /** @test */
public function automatic_seeding_works() public function automatic_seeding_works()
{ {
config(['tenancy.migrate_after_creation' => true]);
$tenant = Tenant::create(['foo.localhost']); $tenant = Tenant::create(['foo.localhost']);
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$this->assertFalse(\Schema::hasTable('users')); $this->assertSame(0, \DB::table('users')->count());
config([ config([
'tenancy.migrate_after_creation' => true,
'tenancy.seed_after_migration' => true, 'tenancy.seed_after_migration' => true,
'tenancy.seeder_class' => \Stancl\Tenancy\Tests\Etc\ExampleSeeder::class, 'tenancy.seeder_parameters' => [
'--class' => ExampleSeeder::class,
],
]); ]);
$tenant2 = Tenant::create(['bar.localhost']); $tenant2 = Tenant::create(['bar.localhost']);
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
$this->assertTrue(\Schema::hasTable('users')); $this->assertSame(1, \DB::table('users')->count());
$this->assertCount(1, \DB::select('select * from users'));
} }
/** @test */ /** @test */
@ -293,7 +297,7 @@ class TenantManagerTest extends TestCase
} }
/** @test */ /** @test */
public function auto_seeding_is_queued_when_enabled() public function autoseeding_is_queued_when_db_creation_is_queued()
{ {
Queue::fake(); Queue::fake();
@ -303,7 +307,7 @@ class TenantManagerTest extends TestCase
'tenancy.seed_after_migration' => true, 'tenancy.seed_after_migration' => true,
]); ]);
$tenant = Tenant::new()->save(); Tenant::new()->save();
Queue::assertPushedWithChain(QueuedTenantDatabaseCreator::class, [ Queue::assertPushedWithChain(QueuedTenantDatabaseCreator::class, [
QueuedTenantDatabaseMigrator::class, QueuedTenantDatabaseMigrator::class,