136 lines
4.1 KiB
PHP
136 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Http\Requests\Fo\Auth\RegisterRequest;
|
|
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
|
|
|
describe('RegisterRequest', function () {
|
|
it('authorizes when guest', function () {
|
|
// GIVEN
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
$authService->shouldReceive('guest')->once()->andReturn(true);
|
|
|
|
$request = new RegisterRequest($authService);
|
|
|
|
// WHEN
|
|
$request->initialize();
|
|
|
|
// THEN
|
|
expect($request->authorize())->toBeTrue();
|
|
});
|
|
|
|
it('does not authorize when not guest', function () {
|
|
// GIVEN
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
$authService->shouldReceive('guest')->once()->andReturn(false);
|
|
|
|
$request = new RegisterRequest($authService);
|
|
|
|
// WHEN
|
|
$request->initialize();
|
|
|
|
// THEN
|
|
expect($request->authorize())->toBeFalse();
|
|
});
|
|
|
|
it('validates rules', function () {
|
|
// GIVEN
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
$request = new RegisterRequest($authService);
|
|
|
|
// WHEN
|
|
$request->initialize();
|
|
|
|
// THEN
|
|
expect($request->rules())->toBeArray()
|
|
->and($request->rules())->toHaveKeys([
|
|
'matricule',
|
|
'code',
|
|
'lastname',
|
|
'firstname',
|
|
'phone',
|
|
'address',
|
|
'postal_code',
|
|
'city',
|
|
'email',
|
|
'password',
|
|
'ok_cgv',
|
|
'optin',
|
|
'birth_date',
|
|
]);
|
|
});
|
|
|
|
it('gets form data correctly', function () {
|
|
// GIVEN
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
$request = new RegisterRequest($authService);
|
|
$request->initialize();
|
|
$request->merge([
|
|
'matricule' => 'M123',
|
|
'code' => 'C123',
|
|
'lastname' => 'Doe',
|
|
'firstname' => 'John',
|
|
'phone' => '0102030405',
|
|
'address' => '123 Main St',
|
|
'postal_code' => '75000',
|
|
'city' => 'Paris',
|
|
'email' => 'john.doe@example.com',
|
|
'password' => 'secret',
|
|
'optin' => '1',
|
|
'birth_date' => '1990-01-01',
|
|
]);
|
|
|
|
$reflection = new \ReflectionClass($request);
|
|
$method = $reflection->getMethod('prepareForValidation');
|
|
$method->setAccessible(true);
|
|
$method->invoke($request);
|
|
|
|
// WHEN
|
|
$data = $request->getFormData();
|
|
|
|
// THEN
|
|
expect($data)->toBeArray()
|
|
->and($data['matricule'])->toBe('M123')
|
|
->and($data['optin'])->toBeTrue()
|
|
->and($data['birth_date'])->toBeInstanceOf(\DateTimeImmutable::class)
|
|
->and($data['birth_date']->format('Y-m-d'))->toBe('1990-01-01');
|
|
});
|
|
|
|
it('throws exception on invalid birth date in getFormData', function () {
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
$request = new RegisterRequest($authService);
|
|
$request->initialize();
|
|
$request->merge([
|
|
'birth_date' => 'invalid-date',
|
|
]);
|
|
|
|
$request->getFormData();
|
|
})->throws(\InvalidArgumentException::class, 'Invalid birth date format');
|
|
|
|
it('returns custom attributes', function () {
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
// GIVEN
|
|
$request = new RegisterRequest($authService);
|
|
|
|
// WHEN
|
|
$request->initialize();
|
|
|
|
// THEN
|
|
expect($request->attributes())->toBeArray()
|
|
->and($request->attributes())->toHaveKeys([
|
|
'firstname',
|
|
'lastname',
|
|
'birth_date',
|
|
'address',
|
|
'phone',
|
|
'city',
|
|
'postcode',
|
|
'password',
|
|
'matricule',
|
|
'code',
|
|
'ok_cgv',
|
|
'optin',
|
|
]);
|
|
});
|
|
});
|