ldc-cocotte/tests/Feature/App/Console/Commands/UserCreateTest.php

40 lines
1.4 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Tests\Feature\App\Console\Commands;
use App\Models\User;
use Domain\Enums\UserRole;
use Illuminate\Support\Facades\Hash;
use Illuminate\Testing\PendingCommand;
use Webmozart\Assert\Assert;
it('creates an admin user via uwil:user:create command using Executor', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$roleChoice = UserRole::Admin->value() . ' : ' . UserRole::Admin->label();
// WHEN / THEN
$command = $this->artisan('uwil:user:create');
Assert::isInstanceOf($command, PendingCommand::class);
$command
->expectsQuestion('What is the firstname?', 'Alice')
->expectsQuestion('What is the lastname?', 'Martin')
->expectsQuestion('What is the email?', 'alice.martin@ldc.fr')
->expectsQuestion('What is the password?', 'Str0ng!Password')
->expectsQuestion('Confirm : What is the password?', 'Str0ng!Password')
->expectsQuestion('What is the role ?', $roleChoice)
->expectsOutput('User created.')
->assertSuccessful()
->execute();
$user = User::query()->where('email', 'alice.martin@ldc.fr')->first();
expect($user)->not->toBeNull()
->and($user?->firstname)->toBe('Alice')
->and($user?->lastname)->toBe('Martin')
->and($user?->role)->toBe(UserRole::Admin)
->and(Hash::check('Str0ng!Password', (string) $user?->password))->toBeTrue();
});