Ensure that all mocked dependencies in RegisterCustomerTest are properly bound to the Laravel service container using App::instance(). This prevents any code that resolves dependencies via container injection from getting real service instances instead of mocks. This fix aligns the Integration test pattern with Feature tests which already use App::instance() to bind mocks to the container. Changes: - Add import for Illuminate\Support\Facades\App - Add App::instance() calls after each mock creation in all 6 test cases - Add mockCrmService() helper method to TestCase base class for future tests Fixes CI failures where CRM service was being called in real instead of being mocked.
349 lines
12 KiB
PHP
349 lines
12 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Actions\Customers\RegisterCustomer;
|
|
use App\Actions\Customers\RegisterCustomerInput;
|
|
use App\Contracts\ActionInput;
|
|
use App\Contracts\Repositories\EmployeeRepository;
|
|
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);
|
|
});
|
|
});
|