ldc-cocotte/tests/Feature/App/Http/Controllers/Fo/Auth/RegisterControllerTest.php

272 lines
9.8 KiB
PHP
Raw Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
use App\Contracts\Executor;
use App\Cqs\Actions\Customers\RegisterCustomer;
use App\Cqs\Actions\Customers\RegisterCustomerInput;
use App\Cqs\Actions\Customers\RegisterCustomerOutput;
use App\Http\Controllers\Fo\Auth\RegisterController;
use App\Http\Requests\Fo\Auth\RegisterRequest;
use App\Models\Customer;
use App\Models\Employee;
2026-06-14 21:32:07 +02:00
use App\Models\Page;
use Database\Seeders\ContentSeeder;
use Domain\Models\ValueObjects\Code;
use Domain\Models\ValueObjects\Email;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Illuminate\Contracts\View\View as ViewContract;
use Illuminate\Http\RedirectResponse;
2026-06-14 21:32:07 +02:00
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\View;
use Ramsey\Uuid\Uuid;
use Shared\ValueObjects\CrmHashcode;
2026-06-14 21:32:07 +02:00
describe('RegisterController', function () {
it('can access get /auth/inscription', function () {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
// WHEN
$response = $this->get('/auth/inscription');
// THEN
$response->assertStatus(200);
});
it('can access post /auth/inscription', function () {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
// WHEN
$response = $this->post('/auth/inscription', []);
// THEN
$response->assertStatus(302);
});
it('returns 422 validation error on postJson /auth/inscription when employee already has a customer_id', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$customer = new Customer(['email' => new Email('existing@example.com')]);
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->saveOrFail();
$employee = new Employee();
$employee->matricule = new Matricule('1234567');
$employee->code = new Code('ABCDEF');
$employee->nom = 'Doe';
$employee->prenom = 'John';
$employee->etablissement = new ShopperEstablishmentCode('ETAB01');
$employee->site = new ShopperSiteCode('SITE');
$employee->service = new ShopperServiceCode('1');
$employee->customer()->associate($customer);
$employee->saveOrFail();
// WHEN
$response = $this->postJson('/auth/inscription', [
'matricule' => '1234567',
'code' => 'ABCDEF',
'lastname' => 'Doe',
'firstname' => 'John',
'postal_code' => '75001',
'city' => 'Paris',
'email' => 'john.doe@example.com',
'password' => 'Str0ng!Pass#2026',
'password_confirmation' => 'Str0ng!Pass#2026',
'ok_cgv' => '1',
'birth_date' => '1990-01-01',
]);
// THEN
$response->assertStatus(422)
->assertJsonValidationErrors(['matricule', 'code']);
});
it('renders verify view when RegisterCustomer returns SUCCESS', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$customer = new Customer(['email' => new Email('john.doe@example.com')]);
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->saveOrFail();
$input = new RegisterCustomerInput(
matricule: new Matricule('1234567'),
code: new Code('ABCDEF'),
firstname: 'John',
lastname: 'Doe',
email: new Email('john.doe@example.com'),
password: 'Str0ng!Pass#2026',
birthDate: new \DateTimeImmutable('1990-01-01'),
phone: null,
address: null,
postcode: '75001',
city: 'Paris',
);
$request = \Mockery::mock(RegisterRequest::class);
$request->shouldReceive('toRegisterCustomerInput')->once()->andReturn($input);
$action = $this->app->make(RegisterCustomer::class);
$executor = \Mockery::mock(Executor::class);
$executor->shouldReceive('execute')
->once()
->with($action, $input)
->andReturn(RegisterCustomerOutput::success($customer));
$controller = new RegisterController($executor);
// WHEN
$result = $controller->submitForm($request, $action);
// THEN
expect($result)->toBeInstanceOf(ViewContract::class)
->and($result->name())->toBe('fo.auth.verify');
});
it('redirects back with matricule error when RegisterCustomer returns EMPLOYEE_ALREADY_REGISTERED', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$input = new RegisterCustomerInput(
matricule: new Matricule('1234567'),
code: new Code('ABCDEF'),
firstname: 'John',
lastname: 'Doe',
email: new Email('john.doe@example.com'),
password: 'Str0ng!Pass#2026',
birthDate: new \DateTimeImmutable('1990-01-01'),
phone: null,
address: null,
postcode: '75001',
city: 'Paris',
);
$request = \Mockery::mock(RegisterRequest::class);
$request->shouldReceive('toRegisterCustomerInput')->once()->andReturn($input);
$request->shouldReceive('except')
->once()
->with(['password', 'password_confirmation'])
->andReturn(['matricule' => '1234567']);
$action = $this->app->make(RegisterCustomer::class);
$executor = \Mockery::mock(Executor::class);
$executor->shouldReceive('execute')
->once()
->with($action, $input)
->andReturn(RegisterCustomerOutput::employeeAlreadyRegistered());
$controller = new RegisterController($executor);
// WHEN
$result = $controller->submitForm($request, $action);
// THEN
expect($result)->toBeInstanceOf(RedirectResponse::class);
$errors = $result->getSession()?->get('errors');
expect($errors?->get('matricule'))->toBe([__('This employee already has an account.')]);
});
it('redirects back with matricule error when RegisterCustomer returns LOCK_TIMEOUT', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$input = new RegisterCustomerInput(
matricule: new Matricule('1234567'),
code: new Code('ABCDEF'),
firstname: 'John',
lastname: 'Doe',
email: new Email('john.doe@example.com'),
password: 'Str0ng!Pass#2026',
birthDate: new \DateTimeImmutable('1990-01-01'),
phone: null,
address: null,
postcode: '75001',
city: 'Paris',
);
$request = \Mockery::mock(RegisterRequest::class);
$request->shouldReceive('toRegisterCustomerInput')->once()->andReturn($input);
$request->shouldReceive('except')
->once()
->with(['password', 'password_confirmation'])
->andReturn(['matricule' => '1234567']);
$action = $this->app->make(RegisterCustomer::class);
$executor = \Mockery::mock(Executor::class);
$executor->shouldReceive('execute')
->once()
->with($action, $input)
->andReturn(RegisterCustomerOutput::lockTimeout());
$controller = new RegisterController($executor);
// WHEN
$result = $controller->submitForm($request, $action);
// THEN
expect($result)->toBeInstanceOf(RedirectResponse::class);
$errors = $result->getSession()?->get('errors');
expect($errors?->get('matricule'))->toBe([__('Registration is already in progress. Please try again in a moment.')]);
});
it('redirects back with matricule error when RegisterCustomer returns EMPLOYEE_INACTIVE', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$input = new RegisterCustomerInput(
matricule: new Matricule('1234567'),
code: new Code('ABCDEF'),
firstname: 'John',
lastname: 'Doe',
email: new Email('john.doe@example.com'),
password: 'Str0ng!Pass#2026',
birthDate: new \DateTimeImmutable('1990-01-01'),
phone: null,
address: null,
postcode: '75001',
city: 'Paris',
);
$request = \Mockery::mock(RegisterRequest::class);
$request->shouldReceive('toRegisterCustomerInput')->once()->andReturn($input);
$request->shouldReceive('except')
->once()
->with(['password', 'password_confirmation'])
->andReturn(['matricule' => '1234567']);
$action = $this->app->make(RegisterCustomer::class);
$executor = \Mockery::mock(Executor::class);
$executor->shouldReceive('execute')
->once()
->with($action, $input)
->andReturn(RegisterCustomerOutput::employeeInactive());
$controller = new RegisterController($executor);
// WHEN
$result = $controller->submitForm($request, $action);
// THEN
expect($result)->toBeInstanceOf(RedirectResponse::class);
$errors = $result->getSession()?->get('errors');
expect($errors?->get('matricule'))->toBe([__('This employee account is no longer active.')]);
});
2026-06-14 21:32:07 +02:00
});