Refactored the codebase to change the namespaces of various actions and queries from `App\Actions` to `App\Cqs\Actions` and `App\Queries` to `App\Cqs\Queries`. This restructuring improves organization and clarity in the codebase.
349 lines
12 KiB
PHP
349 lines
12 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Contracts\ActionInput;
|
|
use App\Contracts\Repositories\EmployeeRepository;
|
|
use App\Cqs\Actions\Customers\RegisterCustomer;
|
|
use App\Cqs\Actions\Customers\RegisterCustomerInput;
|
|
use App\Exceptions\Crm\CrmCustomerExistsException;
|
|
use App\Exceptions\CrmServiceException;
|
|
use App\Models\Customer;
|
|
use App\Models\Employee;
|
|
use App\Services\CrmService;
|
|
use Domain\Enums\Country;
|
|
use Domain\Models\ValueObjects\Code;
|
|
use Domain\Models\ValueObjects\CrmCustomer;
|
|
use Domain\Models\ValueObjects\Email;
|
|
use Domain\Models\ValueObjects\Matricule;
|
|
use Domain\Models\ValueObjects\PhoneNumber;
|
|
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
|
|
use Domain\Models\ValueObjects\ShopperServiceCode;
|
|
use Domain\Models\ValueObjects\ShopperSiteCode;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Notification;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
describe('RegisterCustomer action', function (): void {
|
|
beforeEach(function (): void {
|
|
Notification::fake();
|
|
});
|
|
|
|
it('registers and associates a customer successfully', function (): void {
|
|
// GIVEN
|
|
$matricule = new Matricule('1234567');
|
|
$code = new Code('ABCDEF');
|
|
$email = new Email('john.doe@example.com');
|
|
$birthDate = new \DateTimeImmutable('1990-01-01');
|
|
$phone = new PhoneNumber('+33612345678');
|
|
|
|
$employee = new Employee();
|
|
$employee->matricule = $matricule;
|
|
$employee->code = $code;
|
|
$employee->nom = 'Doe';
|
|
$employee->prenom = 'John';
|
|
$employee->etablissement = new ShopperEstablishmentCode('ETAB01');
|
|
$employee->site = new ShopperSiteCode('SITE');
|
|
$employee->service = new ShopperServiceCode('1');
|
|
$employee->saveOrFail();
|
|
|
|
$crmReturnedHashcode = Uuid::uuid4()->toString();
|
|
|
|
$crmService = \Mockery::mock(CrmService::class);
|
|
$crmService->shouldReceive('persist')
|
|
->once()
|
|
->withArgs(function (CrmCustomer $crmCust) use ($email): bool {
|
|
return $crmCust->email === $email->value
|
|
&& $crmCust->firstname === 'John'
|
|
&& $crmCust->lastname === 'Doe'
|
|
&& $crmCust->city === 'Paris'
|
|
&& $crmCust->country === Country::france;
|
|
})
|
|
->andReturn(new CrmCustomer(
|
|
hashcode: $crmReturnedHashcode,
|
|
email: $email->value,
|
|
password: null,
|
|
firstname: 'John',
|
|
lastname: 'Doe',
|
|
birthDate: '1990-01-01',
|
|
phone: '+33612345678',
|
|
address: '10 rue de Paris',
|
|
address2: null,
|
|
city: 'Paris',
|
|
postcode: '75001',
|
|
country: Country::france,
|
|
optIn: true,
|
|
status: false,
|
|
lastConnection: null,
|
|
));
|
|
App::instance(CrmService::class, $crmService);
|
|
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
$authService->shouldReceive('logIn')
|
|
->once()
|
|
->withArgs(fn (Customer $c) => $c->email->value === $email->value);
|
|
App::instance(AppCustomerAuthService::class, $authService);
|
|
|
|
$employeeRepo = resolve(EmployeeRepository::class);
|
|
|
|
$action = new RegisterCustomer(
|
|
authService: $authService,
|
|
crmService: $crmService,
|
|
employeeRepository: $employeeRepo,
|
|
);
|
|
|
|
$input = new RegisterCustomerInput(
|
|
matricule: $matricule,
|
|
code: $code,
|
|
firstname: 'John',
|
|
lastname: 'Doe',
|
|
email: $email,
|
|
password: 'secretPassword123!',
|
|
birthDate: $birthDate,
|
|
phone: $phone,
|
|
address: '10 rue de Paris',
|
|
postcode: '75001',
|
|
city: 'Paris',
|
|
optIn: true,
|
|
);
|
|
|
|
// WHEN
|
|
$customer = $action($input);
|
|
|
|
// THEN
|
|
expect($customer)->toBeInstanceOf(Customer::class)
|
|
->and($customer->email->value)->toBe('john.doe@example.com')
|
|
->and($customer->hashcode->value)->toBe($crmReturnedHashcode);
|
|
|
|
$freshEmployee = Employee::query()->find($employee->id);
|
|
expect($freshEmployee?->customer_id)->toBe($customer->id);
|
|
});
|
|
|
|
it('throws ModelNotFoundException when employee does not exist', function (): void {
|
|
// GIVEN
|
|
$crmService = \Mockery::mock(CrmService::class);
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
App::instance(CrmService::class, $crmService);
|
|
App::instance(AppCustomerAuthService::class, $authService);
|
|
|
|
$employeeRepo = resolve(EmployeeRepository::class);
|
|
|
|
$action = new RegisterCustomer(
|
|
authService: $authService,
|
|
crmService: $crmService,
|
|
employeeRepository: $employeeRepo,
|
|
);
|
|
|
|
$input = new RegisterCustomerInput(
|
|
matricule: new Matricule('9999999'),
|
|
code: new Code('ABCDYZ'), // no I, L, O, V in Code VO
|
|
firstname: 'John',
|
|
lastname: 'Doe',
|
|
email: new Email('ghost@example.com'),
|
|
password: 'secretPassword123!',
|
|
birthDate: new \DateTimeImmutable('1990-01-01'),
|
|
phone: null,
|
|
address: null,
|
|
postcode: '75001',
|
|
city: 'Paris',
|
|
);
|
|
|
|
// WHEN & THEN
|
|
expect(fn () => $action($input))->toThrow(ModelNotFoundException::class);
|
|
});
|
|
|
|
it('throws ValidationException when CRM indicates customer already exists', function (): void {
|
|
// GIVEN
|
|
$matricule = new Matricule('2345678');
|
|
$code = new Code('BCDEFG');
|
|
$email = new Email('existing@example.com');
|
|
|
|
$employee = new Employee();
|
|
$employee->matricule = $matricule;
|
|
$employee->code = $code;
|
|
$employee->nom = 'Existing';
|
|
$employee->prenom = 'User';
|
|
$employee->etablissement = new ShopperEstablishmentCode('ETAB01');
|
|
$employee->site = new ShopperSiteCode('SITE');
|
|
$employee->service = new ShopperServiceCode('1');
|
|
$employee->saveOrFail();
|
|
|
|
$crmService = \Mockery::mock(CrmService::class);
|
|
$crmService->shouldReceive('persist')
|
|
->once()
|
|
->andThrow(new CrmCustomerExistsException());
|
|
App::instance(CrmService::class, $crmService);
|
|
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
App::instance(AppCustomerAuthService::class, $authService);
|
|
|
|
$employeeRepo = resolve(EmployeeRepository::class);
|
|
|
|
$action = new RegisterCustomer(
|
|
authService: $authService,
|
|
crmService: $crmService,
|
|
employeeRepository: $employeeRepo,
|
|
);
|
|
|
|
$input = new RegisterCustomerInput(
|
|
matricule: $matricule,
|
|
code: $code,
|
|
firstname: 'User',
|
|
lastname: 'Existing',
|
|
email: $email,
|
|
password: 'secretPassword123!',
|
|
birthDate: new \DateTimeImmutable('1990-01-01'),
|
|
phone: null,
|
|
address: null,
|
|
postcode: '75001',
|
|
city: 'Paris',
|
|
);
|
|
|
|
// WHEN & THEN
|
|
expect(fn () => $action($input))->toThrow(ValidationException::class);
|
|
});
|
|
|
|
it('throws ValidationException when CRM fails with CrmServiceException', function (): void {
|
|
// GIVEN
|
|
$matricule = new Matricule('3456789');
|
|
$code = new Code('CDEFGH');
|
|
$email = new Email('crmerror@example.com');
|
|
|
|
$employee = new Employee();
|
|
$employee->matricule = $matricule;
|
|
$employee->code = $code;
|
|
$employee->nom = 'Error';
|
|
$employee->prenom = 'Crm';
|
|
$employee->etablissement = new ShopperEstablishmentCode('ETAB01');
|
|
$employee->site = new ShopperSiteCode('SITE');
|
|
$employee->service = new ShopperServiceCode('1');
|
|
$employee->saveOrFail();
|
|
|
|
$crmService = \Mockery::mock(CrmService::class);
|
|
$crmService->shouldReceive('persist')
|
|
->once()
|
|
->andThrow(CrmServiceException::of('CRM service offline'));
|
|
App::instance(CrmService::class, $crmService);
|
|
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
App::instance(AppCustomerAuthService::class, $authService);
|
|
|
|
$employeeRepo = resolve(EmployeeRepository::class);
|
|
|
|
$action = new RegisterCustomer(
|
|
authService: $authService,
|
|
crmService: $crmService,
|
|
employeeRepository: $employeeRepo,
|
|
);
|
|
|
|
$input = new RegisterCustomerInput(
|
|
matricule: $matricule,
|
|
code: $code,
|
|
firstname: 'Crm',
|
|
lastname: 'Error',
|
|
email: $email,
|
|
password: 'secretPassword123!',
|
|
birthDate: new \DateTimeImmutable('1990-01-01'),
|
|
phone: null,
|
|
address: null,
|
|
postcode: '75001',
|
|
city: 'Paris',
|
|
);
|
|
|
|
// WHEN & THEN
|
|
expect(fn () => $action($input))->toThrow(ValidationException::class);
|
|
});
|
|
|
|
it('throws UnexpectedValueException when CRM returns no hashcode', function (): void {
|
|
// GIVEN
|
|
$matricule = new Matricule('4567890');
|
|
$code = new Code('DEFGHJ');
|
|
$email = new Email('nohash@example.com');
|
|
|
|
$employee = new Employee();
|
|
$employee->matricule = $matricule;
|
|
$employee->code = $code;
|
|
$employee->nom = 'No';
|
|
$employee->prenom = 'Hash';
|
|
$employee->etablissement = new ShopperEstablishmentCode('ETAB01');
|
|
$employee->site = new ShopperSiteCode('SITE');
|
|
$employee->service = new ShopperServiceCode('1');
|
|
$employee->saveOrFail();
|
|
|
|
$crmService = \Mockery::mock(CrmService::class);
|
|
$crmService->shouldReceive('persist')
|
|
->once()
|
|
->andReturn(new CrmCustomer(
|
|
hashcode: null,
|
|
email: $email->value,
|
|
password: null,
|
|
firstname: 'Hash',
|
|
lastname: 'No',
|
|
birthDate: '1990-01-01',
|
|
phone: null,
|
|
address: null,
|
|
address2: null,
|
|
city: 'Paris',
|
|
postcode: '75001',
|
|
country: Country::france,
|
|
optIn: false,
|
|
status: false,
|
|
lastConnection: null,
|
|
));
|
|
App::instance(CrmService::class, $crmService);
|
|
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
App::instance(AppCustomerAuthService::class, $authService);
|
|
|
|
$employeeRepo = resolve(EmployeeRepository::class);
|
|
|
|
$action = new RegisterCustomer(
|
|
authService: $authService,
|
|
crmService: $crmService,
|
|
employeeRepository: $employeeRepo,
|
|
);
|
|
|
|
$input = new RegisterCustomerInput(
|
|
matricule: $matricule,
|
|
code: $code,
|
|
firstname: 'Hash',
|
|
lastname: 'No',
|
|
email: $email,
|
|
password: 'secretPassword123!',
|
|
birthDate: new \DateTimeImmutable('1990-01-01'),
|
|
phone: null,
|
|
address: null,
|
|
postcode: '75001',
|
|
city: 'Paris',
|
|
);
|
|
|
|
// WHEN & THEN
|
|
expect(fn () => $action($input))->toThrow(\UnexpectedValueException::class);
|
|
});
|
|
|
|
it('throws when invalid ActionInput is passed', function (): void {
|
|
// GIVEN
|
|
$crmService = \Mockery::mock(CrmService::class);
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
App::instance(CrmService::class, $crmService);
|
|
App::instance(AppCustomerAuthService::class, $authService);
|
|
|
|
$employeeRepo = resolve(EmployeeRepository::class);
|
|
|
|
$action = new RegisterCustomer(
|
|
authService: $authService,
|
|
crmService: $crmService,
|
|
employeeRepository: $employeeRepo,
|
|
);
|
|
|
|
$invalidInput = new class () implements ActionInput
|
|
{
|
|
};
|
|
|
|
// WHEN & THEN
|
|
expect(fn () => $action($invalidInput))->toThrow(\InvalidArgumentException::class);
|
|
});
|
|
});
|