1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 06:04:03 +00:00
tenancy/tests/Traits/TenantAwareCommandTest.php
michael lundbøl 39c1e65172
Add a hook that should run before trying to fetch tenants
Signed-off-by: michael lundbøl <michael.lundboel@gmail.com>
2020-05-06 22:15:18 +02:00

75 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Tests\Traits;
use Stancl\Tenancy\Tenant;
use Stancl\Tenancy\Tests\TestCase;
class TenantAwareCommandTest extends TestCase
{
public $autoCreateTenant = false;
public $autoInitTenancy = false;
/** @test */
public function commands_run_globally_are_tenant_aware_and_return_valid_exit_code()
{
$tenant1 = Tenant::new()->save();
$tenant2 = Tenant::new()->save();
\Artisan::call('tenants:migrate', [
'--tenants' => [$tenant1['id'], $tenant2['id']],
]);
$this->artisan('user:add')
->assertExitCode(0);
tenancy()->initializeTenancy($tenant1);
$this->assertNotEmpty(\DB::table('users')->get());
tenancy()->end();
tenancy()->initializeTenancy($tenant2);
$this->assertNotEmpty(\DB::table('users')->get());
tenancy()->end();
}
/** @test */
public function commands_can_optionally_specify_a_hook_that_should_be_ran_before_running_the_command_within_a_tenant()
{
$tenant1 = Tenant::new()->save();
$tenant2 = Tenant::new()->save();
\Artisan::call('tenants:migrate', [
'--tenants' => [$tenant1['id'], $tenant2['id']],
]);
$this->withoutMockingConsoleOutput();
$response = $this->artisan('user:add_conditionally', [
'--tenants' => [$tenant1['id'], $tenant2['id']],
'--stop' => true
]);
$this->assertEquals(1, $response);
tenancy()->initializeTenancy($tenant1);
$this->assertEmpty(\DB::table('users')->get());
tenancy()->end();
tenancy()->initializeTenancy($tenant2);
$this->assertEmpty(\DB::table('users')->get());
tenancy()->end();
$this->artisan('user:add_conditionally', [
'--tenants' => [$tenant1['id'], $tenant2['id']],
'--stop' => false
]);
tenancy()->initializeTenancy($tenant1);
$this->assertNotEmpty(\DB::table('users')->get());
tenancy()->end();
tenancy()->initializeTenancy($tenant2);
$this->assertNotEmpty(\DB::table('users')->get());
tenancy()->end();
}
}