154 lines
5.7 KiB
PHP
154 lines
5.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Cqs\Actions\Crm\UpdateCustomerPassword;
|
|
use App\Cqs\Actions\Crm\UpdateCustomerPasswordInput;
|
|
use App\Cqs\Actions\Crm\UpdateCustomerPasswordOutput;
|
|
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\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
|
use Ramsey\Uuid\Uuid;
|
|
use Shared\ValueObjects\CrmHashcode;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
function makePasswordTestCrmCustomer(string $hashcode, string $email): CrmCustomer
|
|
{
|
|
return new CrmCustomer(
|
|
hashcode: $hashcode,
|
|
email: $email,
|
|
password: null,
|
|
firstname: 'Claire',
|
|
lastname: 'Dupuis',
|
|
birthDate: '1991-07-20',
|
|
phone: null,
|
|
address: null,
|
|
address2: null,
|
|
city: 'Laval',
|
|
postcode: '53000',
|
|
country: Country::france,
|
|
optIn: false,
|
|
status: true,
|
|
lastConnection: null,
|
|
);
|
|
}
|
|
|
|
describe('UpdateCustomerPassword action', function (): void {
|
|
it('changes password in CRM for authenticated customer', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$hashcodeUuid = Uuid::uuid4();
|
|
$hashcode = $hashcodeUuid->toString();
|
|
$customer = new Customer();
|
|
$customer->email = new Email('claire.dupuis@example.com');
|
|
$customer->hashcode = new CrmHashcode($hashcodeUuid);
|
|
$customer->saveOrFail();
|
|
|
|
$this->actingAs($customer, 'frontend');
|
|
|
|
$crmCustomer = makePasswordTestCrmCustomer($hashcode, $customer->email->value);
|
|
|
|
/** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */
|
|
$crmService = \Mockery::mock(CrmService::class);
|
|
$crmService->shouldReceive('getCustomerFromHashCode')
|
|
->once()
|
|
->with(\Mockery::on(static fn (CrmHashcode $h): bool => $h->value === $hashcode))
|
|
->andReturn($crmCustomer);
|
|
$crmService->shouldReceive('changePassword')
|
|
->once()
|
|
->with($crmCustomer, 'OldPass123!', 'NewPass456!')
|
|
->andReturn($crmCustomer);
|
|
|
|
$authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService);
|
|
$action = new UpdateCustomerPassword($authService, $crmService);
|
|
|
|
// WHEN
|
|
$output = $action(new UpdateCustomerPasswordInput('OldPass123!', 'NewPass456!'));
|
|
|
|
// THEN
|
|
expect($output)->toBeInstanceOf(UpdateCustomerPasswordOutput::class)
|
|
->and($output->result)->toBe($crmCustomer);
|
|
});
|
|
|
|
it('returns false output when customer cannot be resolved in CRM', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$customer = new Customer();
|
|
$customer->email = new Email('missing.crm@example.com');
|
|
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
|
|
$customer->saveOrFail();
|
|
|
|
$this->actingAs($customer, 'frontend');
|
|
|
|
/** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */
|
|
$crmService = \Mockery::mock(CrmService::class);
|
|
$crmService->shouldReceive('getCustomerFromHashCode')
|
|
->once()
|
|
->andReturnNull();
|
|
$crmService->shouldNotReceive('changePassword');
|
|
|
|
$authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService);
|
|
$action = new UpdateCustomerPassword($authService, $crmService);
|
|
|
|
// WHEN
|
|
$output = $action(new UpdateCustomerPasswordInput('OldPass123!', 'NewPass456!'));
|
|
|
|
// THEN
|
|
expect($output->result)->toBeFalse();
|
|
});
|
|
|
|
it('returns false output when changePassword throws an unexpected exception', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$hashcodeUuid = Uuid::uuid4();
|
|
$hashcode = $hashcodeUuid->toString();
|
|
$customer = new Customer();
|
|
$customer->email = new Email('pwd.error@example.com');
|
|
$customer->hashcode = new CrmHashcode($hashcodeUuid);
|
|
$customer->saveOrFail();
|
|
|
|
$this->actingAs($customer, 'frontend');
|
|
|
|
$crmCustomer = makePasswordTestCrmCustomer($hashcode, $customer->email->value);
|
|
|
|
/** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */
|
|
$crmService = \Mockery::mock(CrmService::class);
|
|
$crmService->shouldReceive('getCustomerFromHashCode')
|
|
->once()
|
|
->andReturn($crmCustomer);
|
|
$crmService->shouldReceive('changePassword')
|
|
->once()
|
|
->andThrow(new \RuntimeException('CRM error'));
|
|
|
|
$authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService);
|
|
$action = new UpdateCustomerPassword($authService, $crmService);
|
|
|
|
// WHEN
|
|
$output = $action(new UpdateCustomerPasswordInput('OldPass123!', 'NewPass456!'));
|
|
|
|
// THEN
|
|
expect($output->result)->toBeFalse();
|
|
});
|
|
|
|
it('throws AuthServiceException when unauthenticated', function (): void {
|
|
// GIVEN
|
|
/** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */
|
|
$crmService = \Mockery::mock(CrmService::class);
|
|
$crmService->shouldNotReceive('getCustomerFromHashCode');
|
|
$crmService->shouldNotReceive('changePassword');
|
|
|
|
$authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService);
|
|
$action = new UpdateCustomerPassword($authService, $crmService);
|
|
|
|
// WHEN & THEN
|
|
expect(fn () => $action(new UpdateCustomerPasswordInput('OldPass123!', 'NewPass456!')))
|
|
->toThrow(AuthServiceException::class);
|
|
});
|
|
});
|