diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index 4f746387..7d44b3b3 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -179,13 +179,29 @@ test('run command with array of tenants works', function () { ->expectsOutput('Tenant: ' . $tenantId2); }); -test('run command works when sub command asks question and accepts argument', function () { - $id = Tenant::create()->getTenantKey(); +test('run command works when sub command asks question and accepts argument', closure: function () { + $tenant = Tenant::create(); + $id = $tenant->getTenantKey(); - pest()->artisan("tenants:run --tenants=$id 'age:ask Abrar' ") - ->expectsQuestion('What is your age?', 22) + // Run tenant migrations so we have users table + Artisan::call('tenants:migrate'); + + pest()->artisan("tenants:run --tenants=$id 'user:addwithname Abrar' ") + ->expectsQuestion('What is your email?', 'email@localhost') ->expectsOutput("Tenant: $id") - ->expectsOutput("Abrar's age is 22."); + ->expectsOutput("User created: Abrar(email@localhost)"); + + // Assert users table does not exist in the central context + expect(Schema::hasTable('users'))->toBeFalse(); + + // Assert user created in tenant context + tenancy()->initialize($tenant); + expect(Schema::hasTable('users'))->toBeTrue(); + $user = \Stancl\Tenancy\Tests\Etc\User::first(); + + // Assert user is same as provided using command + expect($user->name)->toBe('Abrar'); + expect($user->email)->toBe('email@localhost'); }); // todo@tests diff --git a/tests/Etc/Console/ExampleQuestionCommand.php b/tests/Etc/Console/ExampleQuestionCommand.php index 87b3e834..9a967054 100644 --- a/tests/Etc/Console/ExampleQuestionCommand.php +++ b/tests/Etc/Console/ExampleQuestionCommand.php @@ -3,6 +3,8 @@ namespace Stancl\Tenancy\Tests\Etc\Console; use Illuminate\Console\Command; +use Illuminate\Support\Str; +use Stancl\Tenancy\Tests\Etc\User; class ExampleQuestionCommand extends Command { @@ -11,7 +13,7 @@ class ExampleQuestionCommand extends Command * * @var string */ - protected $signature = 'age:ask {name}'; + protected $signature = 'user:addwithname {name}'; /** * The console command description. @@ -27,8 +29,18 @@ class ExampleQuestionCommand extends Command */ public function handle() { - $age = $this->ask('What is your age?'); + $email = $this->ask('What is your email?'); - $this->line($this->argument('name') . "'s age is $age."); + User::create([ + 'name' => $this->argument('name'), + 'email' => $email, + 'email_verified_at' => now(), + 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password + 'remember_token' => Str::random(10), + ]); + + $this->line("User created: ". $this->argument('name') . "($email)"); + + return 0; } }