203 lines
7 KiB
PHP
203 lines
7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\App\Cqs\Actions\Crm;
|
|
|
|
use App\Cqs\Actions\Crm\UpdateCustomer;
|
|
use App\Cqs\Actions\Crm\UpdateCustomerInput;
|
|
use App\Models\Customer;
|
|
use App\Services\CrmService;
|
|
use Domain\Enums\Country;
|
|
use Domain\Exceptions\AuthServiceException;
|
|
use Domain\Models\ValueObjects\CrmCustomer;
|
|
use Domain\Models\ValueObjects\Email;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
|
use Shared\Helpers\UuidHelper;
|
|
use Shared\ValueObjects\CrmHashcode;
|
|
|
|
function makeCrmCustomerDto(string $hashcode, string $email): CrmCustomer
|
|
{
|
|
return new CrmCustomer(
|
|
hashcode: $hashcode,
|
|
email: $email,
|
|
password: null,
|
|
firstname: 'Jeanne',
|
|
lastname: 'Martin',
|
|
birthDate: '1992-05-14',
|
|
phone: '+33611223344',
|
|
address: '12 rue des Lilas',
|
|
address2: null,
|
|
city: 'Laval',
|
|
postcode: '53000',
|
|
country: Country::france,
|
|
optIn: true,
|
|
status: true,
|
|
lastConnection: null,
|
|
);
|
|
}
|
|
|
|
describe('UpdateCustomer action', function (): void {
|
|
it('updates customer information in CRM for the authenticated customer', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$hashcodeUuid = UuidHelper::v4();
|
|
$hashcode = $hashcodeUuid->toString();
|
|
$customer = new Customer();
|
|
$customer->email = new Email('jeanne.martin@example.com');
|
|
$customer->hashcode = new CrmHashcode($hashcodeUuid);
|
|
$customer->saveOrFail();
|
|
|
|
$this->actingAs($customer, 'frontend');
|
|
|
|
$expectedCrmCustomer = makeCrmCustomerDto($hashcode, $customer->email->value);
|
|
|
|
/** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */
|
|
$crmService = \Mockery::mock(CrmService::class);
|
|
$crmService->shouldReceive('updateCustomerInfos')
|
|
->once()
|
|
->withArgs(function (Customer $passedCustomer, array $data) use ($customer): bool {
|
|
return $passedCustomer->id === $customer->id
|
|
&& $data === [
|
|
'firstname' => 'Jeanne',
|
|
'lastname' => 'Martin',
|
|
'birth_date' => '1992-05-14',
|
|
'postcode' => '53000',
|
|
'city' => 'Laval',
|
|
'opt_in' => true,
|
|
'phone' => '+33611223344',
|
|
'address' => '12 rue des Lilas',
|
|
];
|
|
})
|
|
->andReturn($expectedCrmCustomer);
|
|
|
|
$authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService);
|
|
$action = new UpdateCustomer($authService, $crmService);
|
|
|
|
$input = new UpdateCustomerInput(
|
|
firstname: 'Jeanne',
|
|
lastname: 'Martin',
|
|
birthDate: '1992-05-14',
|
|
phone: '+33611223344',
|
|
address: '12 rue des Lilas',
|
|
postcode: '53000',
|
|
city: 'Laval',
|
|
optIn: true,
|
|
);
|
|
|
|
// WHEN
|
|
$output = $action($input);
|
|
|
|
// THEN
|
|
expect($output->result)->toBe($expectedCrmCustomer);
|
|
});
|
|
|
|
it('omits nullable phone and address when null and propagates validation exceptions', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$hashcodeUuid = UuidHelper::v4();
|
|
$customer = new Customer();
|
|
$customer->email = new Email('no.phone@example.com');
|
|
$customer->hashcode = new CrmHashcode($hashcodeUuid);
|
|
$customer->saveOrFail();
|
|
|
|
$this->actingAs($customer, 'frontend');
|
|
|
|
$validationException = ValidationException::withMessages(['postcode' => ['Code postal invalide']]);
|
|
|
|
/** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */
|
|
$crmService = \Mockery::mock(CrmService::class);
|
|
$crmService->shouldReceive('updateCustomerInfos')
|
|
->once()
|
|
->withArgs(function (Customer $_, array $data): bool {
|
|
return ! array_key_exists('phone', $data)
|
|
&& ! array_key_exists('address', $data)
|
|
&& $data['opt_in'] === false;
|
|
})
|
|
->andReturn($validationException);
|
|
|
|
$authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService);
|
|
$action = new UpdateCustomer($authService, $crmService);
|
|
|
|
$input = new UpdateCustomerInput(
|
|
firstname: 'Paul',
|
|
lastname: 'Durand',
|
|
birthDate: '1985-03-10',
|
|
phone: null,
|
|
address: null,
|
|
postcode: '00000',
|
|
city: 'Sablé',
|
|
optIn: false,
|
|
);
|
|
|
|
// WHEN
|
|
$output = $action($input);
|
|
|
|
// THEN
|
|
expect($output->result)->toBe($validationException);
|
|
});
|
|
|
|
it('returns false output when CRM service throws an unexpected error', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$customer = new Customer();
|
|
$customer->email = new Email('err.crm@example.com');
|
|
$customer->hashcode = new CrmHashcode(UuidHelper::v4());
|
|
$customer->saveOrFail();
|
|
|
|
$this->actingAs($customer, 'frontend');
|
|
|
|
/** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */
|
|
$crmService = \Mockery::mock(CrmService::class);
|
|
$crmService->shouldReceive('updateCustomerInfos')
|
|
->once()
|
|
->andThrow(new \RuntimeException('CRM unavailable'));
|
|
|
|
$authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService);
|
|
$action = new UpdateCustomer($authService, $crmService);
|
|
|
|
$input = new UpdateCustomerInput(
|
|
firstname: 'Paul',
|
|
lastname: 'Durand',
|
|
birthDate: '1985-03-10',
|
|
phone: null,
|
|
address: null,
|
|
postcode: '72300',
|
|
city: 'Sablé',
|
|
optIn: true,
|
|
);
|
|
|
|
// WHEN
|
|
$output = $action($input);
|
|
|
|
// THEN
|
|
expect($output->result)->toBeFalse();
|
|
});
|
|
|
|
it('throws AuthServiceException when no customer is authenticated', function (): void {
|
|
// GIVEN
|
|
/** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */
|
|
$crmService = \Mockery::mock(CrmService::class);
|
|
$crmService->shouldNotReceive('updateCustomerInfos');
|
|
|
|
$authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService);
|
|
$action = new UpdateCustomer($authService, $crmService);
|
|
|
|
$input = new UpdateCustomerInput(
|
|
firstname: 'Guest',
|
|
lastname: 'User',
|
|
birthDate: '1990-01-01',
|
|
phone: null,
|
|
address: null,
|
|
postcode: '72300',
|
|
city: 'Sablé',
|
|
optIn: false,
|
|
);
|
|
|
|
// WHEN & THEN
|
|
expect(fn () => $action($input))
|
|
->toThrow(AuthServiceException::class);
|
|
});
|
|
});
|