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

extract tests to helpers

This commit is contained in:
Abrar Ahmad 2022-06-30 16:15:01 +05:00
parent 8bd7c95fff
commit c3d740253f
2 changed files with 112 additions and 209 deletions

View file

@ -115,72 +115,21 @@ test('rollback command works', function () {
expect(Schema::hasTable('users'))->toBeFalse();
});
test('seed command works', function () {
$this->markTestIncomplete();
});
// Incomplete test
test('seed command works');
test('database connection is switched to default', function () {
$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);
// $this->run_commands_works();
$id = Tenant::create()->getTenantKey();
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');
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
databaseConnectionSwitchedToDefault();
});
test('database connection is switched to default when tenancy has been initialized', function () {
tenancy()->initialize(Tenant::create());
// $this->database_connection_is_switched_to_default();
$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);
// $this->run_commands_works();
$id = Tenant::create()->getTenantKey();
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');
expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
databaseConnectionSwitchedToDefault();
});
test('run commands works', function () {
$id = Tenant::create()->getTenantKey();
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');
runCommandWorks();
});
test('install command works', function () {
@ -229,3 +178,33 @@ test('run command with array of tenants works', function () {
->expectsOutput('Tenant: ' . $tenantId1)
->expectsOutput('Tenant: ' . $tenantId2);
});
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');
}
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);
}