From 8885799486ea6608cb6638c450d32db8d3c88581 Mon Sep 17 00:00:00 2001 From: Dan Johnson Date: Fri, 21 Mar 2025 08:58:18 +0000 Subject: [PATCH 1/3] test: add tests for making user using command --- tests/Unit/Commands/MakeUserCommandTest.php | 59 +++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tests/Unit/Commands/MakeUserCommandTest.php diff --git a/tests/Unit/Commands/MakeUserCommandTest.php b/tests/Unit/Commands/MakeUserCommandTest.php new file mode 100644 index 00000000..cccb544f --- /dev/null +++ b/tests/Unit/Commands/MakeUserCommandTest.php @@ -0,0 +1,59 @@ +artisan('cachet:make:user', [ + 'email' => 'dan@example.com', + '--name' => 'Dan', + '--admin' => true, + '--password' => 'password', + ]); + + $this->assertDatabaseHas('users', [ + 'email' => 'dan@example.com', + 'name' => 'Dan', + 'is_admin' => true, + ]); +}); + +it('creates the standard user', function () { + $this->artisan('cachet:make:user', [ + 'email' => 'dan@example.com', + '--name' => 'Dan', + '--admin' => false, + '--password' => 'password', + ]); + + $this->assertDatabaseHas('users', [ + 'email' => 'dan@example.com', + 'name' => 'Dan', + 'is_admin' => false, + ]); +}); + +it('creates the standard user using prompts', function () { + $this->artisan('cachet:make:user') + ->expectsQuestion('What is the user\'s name?', 'Dan') + ->expectsQuestion('What is the user\'s email?', 'dan@example.com') + ->expectsConfirmation('Is the user an admin?', 'No') + ->expectsQuestion('What is the user\'s password?', 'password'); + + $this->assertDatabaseHas('users', [ + 'email' => 'dan@example.com', + 'name' => 'Dan', + 'is_admin' => false, + ]); +}); + +it('creates the admin user using prompts', function () { + $this->artisan('cachet:make:user') + ->expectsQuestion('What is the user\'s name?', 'Dan') + ->expectsQuestion('What is the user\'s email?', 'dan@example.com') + ->expectsConfirmation('Is the user an admin?', 'Yes') + ->expectsQuestion('What is the user\'s password?', 'password'); + + $this->assertDatabaseHas('users', [ + 'email' => 'dan@example.com', + 'name' => 'Dan', + 'is_admin' => true, + ]); +}); From 55f6f414510f65322258d85dff32afa3009dd51e Mon Sep 17 00:00:00 2001 From: Dan Johnson Date: Fri, 21 Mar 2025 08:58:31 +0000 Subject: [PATCH 2/3] fix: bug where admin flag was being prompted even when setting no --- src/Commands/MakeUserCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Commands/MakeUserCommand.php b/src/Commands/MakeUserCommand.php index 2ea2f390..f0901cc7 100644 --- a/src/Commands/MakeUserCommand.php +++ b/src/Commands/MakeUserCommand.php @@ -16,7 +16,7 @@ class MakeUserCommand extends Command * * @var string */ - protected $signature = 'cachet:make:user {email?} {--password= : The user\'s password} {--admin : Whether the user is an admin} {--name= : The name of the user }'; + protected $signature = 'cachet:make:user {email?} {--password= : The user\'s password} {--admin= : Whether the user is an admin} {--name= : The name of the user }'; /** * The console command description. @@ -63,7 +63,7 @@ public function handle(): int $this->promptEmail(); } - if (! $this->isAdmin) { + if ($this->isAdmin === null) { $this->promptIsAdmin(); } From 060da91426f2b0f8fc4452ff6d1d8b0009d59172 Mon Sep 17 00:00:00 2001 From: Dan Johnson Date: Fri, 21 Mar 2025 09:09:57 +0000 Subject: [PATCH 3/3] fix: only set prop to bool or null --- src/Commands/MakeUserCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/MakeUserCommand.php b/src/Commands/MakeUserCommand.php index f0901cc7..8690e782 100644 --- a/src/Commands/MakeUserCommand.php +++ b/src/Commands/MakeUserCommand.php @@ -51,7 +51,7 @@ class MakeUserCommand extends Command public function handle(): int { $this->email = $this->argument('email'); - $this->isAdmin = $this->option('admin'); + $this->isAdmin = $this->option('admin') !== null ? (bool) $this->option('admin') : null; $this->password = $this->option('password'); $this->data['name'] = $this->option('name');