Skip to content

Commit

Permalink
feat: add artisan command to create new account (#4745)
Browse files Browse the repository at this point in the history
  • Loading branch information
codemonkeysoftware authored Jan 31, 2021
1 parent 175b231 commit b9ee793
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### New features:

* Add an artisan command to create an account
* Add the notion of AddressBooks
* Allow customization of life event types

Expand Down
70 changes: 70 additions & 0 deletions app/Console/Commands/CreateAccount.php
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>');
}
}
}
67 changes: 67 additions & 0 deletions tests/Commands/CreateAccountTest.php
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);
}
}

0 comments on commit b9ee793

Please sign in to comment.