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

34 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\User;
use Domain\Enums\UserRole;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
uses(RefreshDatabase::class);
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
$this->artisan('uwil:user:create')
->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();
$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();
});