-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add artisan command to create new account (#4745)
- Loading branch information
1 parent
175b231
commit b9ee793
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\Account\Account; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Console\ConfirmableTrait; | ||
|
||
class CreateAccount extends Command | ||
{ | ||
use ConfirmableTrait; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'account:create | ||
{--email= : Login email for the account.} | ||
{--password= : Password to set for the account.} | ||
{--firstname= : First name for the account.} | ||
{--lastname= : Last name for the account.}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create a new account'; | ||
|
||
/** | ||
* Missing argument errors. Exposed for testing. | ||
*/ | ||
const ERROR_MISSING_EMAIL = '! You must specify an email'; | ||
const ERROR_MISSING_PASSWORD = '! You must specify a password'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
$email = $this->option('email'); | ||
if (empty($email)) { | ||
$this->error($this::ERROR_MISSING_EMAIL); | ||
} | ||
|
||
$password = $this->option('password'); | ||
if (empty($password)) { | ||
$this->error($this::ERROR_MISSING_PASSWORD); | ||
} | ||
|
||
$firstName = $this->option('firstname') ?? 'John'; | ||
|
||
$lastName = $this->option('lastname') ?? 'Doe'; | ||
|
||
if (empty($email) || empty($password)) { | ||
return; | ||
} | ||
|
||
if ($this->confirmToProceed('This will create a new user for '.$firstName.' '.$lastName.' with email '.$email)) { | ||
Account::createDefault($firstName, $lastName, $email, $password); | ||
|
||
$this->info('| You can now sign in to your account:'); | ||
$this->line('| username: '.$email); | ||
$this->line('| password: <hidden>'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Tests\Commands; | ||
|
||
use Tests\TestCase; | ||
use App\Models\User\User; | ||
use App\Console\Commands\CreateAccount; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
|
||
class CreateAccountTest extends TestCase | ||
{ | ||
use DatabaseTransactions; | ||
|
||
/** @test */ | ||
public function it_creates_account() | ||
{ | ||
$email = 'user1@example.com'; | ||
$this->artisan('account:create', ['--email' => 'user1@example.com', '--password' => 'astrongpassword']); | ||
|
||
$user = User::where('email', '=', $email)->first(); | ||
$this->assertNotEmpty($user); | ||
$account = $user->account; | ||
} | ||
|
||
/** @test */ | ||
public function it_creates_account_with_specified_name() | ||
{ | ||
$email = 'user1@example.com'; | ||
$firstname = 'firstname'; | ||
$lastname = 'lastname'; | ||
$this->artisan('account:create', [ | ||
'--email' => $email, | ||
'--password' => 'astrongpassword', | ||
'--firstname' => $firstname, | ||
'--lastname' => $lastname, | ||
]); | ||
|
||
$user = User::where('email', '=', $email)->first(); | ||
$this->assertNotEmpty($user); | ||
$account = $user->account; | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_creation_without_email() | ||
{ | ||
$firstname = 'firstname'; | ||
$lastname = 'lastname'; | ||
$this->artisan('account:create', [ | ||
'--password' => 'astrongpassword', | ||
]) | ||
->expectsOutput(CreateAccount::ERROR_MISSING_EMAIL) | ||
->doesntExpectOutput(CreateAccount::ERROR_MISSING_PASSWORD); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_creation_without_password() | ||
{ | ||
$email = 'user1@example.com'; | ||
$firstname = 'firstname'; | ||
$lastname = 'lastname'; | ||
$this->artisan('account:create', [ | ||
'--email' => $email, | ||
]) | ||
->expectsOutput(CreateAccount::ERROR_MISSING_PASSWORD) | ||
->doesntExpectOutput(CreateAccount::ERROR_MISSING_EMAIL); | ||
} | ||
} |