ldc-cocotte/tests/Integration/WithApp/App/Auth/CrmUserProviderTest.php

109 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Auth;
use App\Auth\CrmUserProvider;
use App\Exceptions\Crm\CrmCustomerNotActivatedException;
use App\Models\Customer;
use App\Services\CrmService;
use Domain\Enums\Country;
use Domain\Models\ValueObjects\CrmCustomer;
use Domain\Models\ValueObjects\Email;
use Illuminate\Contracts\Notifications\Dispatcher;
use Illuminate\Hashing\BcryptHasher;
use Illuminate\Support\Str;
describe('CrmUserProvider', function (): void {
it('returns null without creating a local customer when no local record matches credentials', function (): void {
// GIVEN
$hasher = new BcryptHasher();
/** @var \Mockery\MockInterface&\App\Services\CrmService $crmService */
$crmService = \Mockery::mock(CrmService::class);
$crmService->shouldNotReceive('getCustomerFromEmail');
$provider = new CrmUserProvider($hasher, Customer::class, $crmService);
// WHEN
$result = $provider->retrieveByCredentials(['email' => 'crm-only@example.com']);
// THEN
expect($result)->toBeNull()
->and(Customer::query()->count())->toBe(0);
});
it('returns the existing local customer when CRM also confirms it', function (): void {
// GIVEN
/** @var Customer $customer */
$customer = Customer::query()->forceCreate([
'email' => new Email('existing-local@example.com'),
'hashcode' => (string) Str::uuid(),
]);
$crmCustomer = new CrmCustomer(
hashcode: $customer->hashcode->value,
email: 'existing-local@example.com',
password: null,
firstname: 'John',
lastname: 'Doe',
birthDate: null,
phone: null,
address: null,
address2: null,
city: null,
postcode: null,
country: Country::france,
optIn: false,
status: true,
lastConnection: null,
);
$hasher = new BcryptHasher();
/** @var \Mockery\MockInterface&\App\Services\CrmService $crmService */
$crmService = \Mockery::mock(CrmService::class);
$crmService->shouldReceive('getCustomerFromEmail')
->once()
->with('existing-local@example.com')
->andReturn($crmCustomer);
$provider = new CrmUserProvider($hasher, Customer::class, $crmService);
// WHEN
$result = $provider->retrieveByCredentials(['email' => 'existing-local@example.com']);
// THEN
expect($result)->toBeInstanceOf(Customer::class)
->and($result?->getAuthIdentifier())->toBe($customer->id);
});
it('catches mail delivery failures when resending activation email for unactivated accounts', function (): void {
// GIVEN
$customer = new Customer(['email' => new Email('unverified@example.com')]);
$dispatcher = \Mockery::mock(Dispatcher::class);
$dispatcher->shouldReceive('send')
->once()
->andThrow(new \RuntimeException('SMTP connection refused'));
app()->instance(Dispatcher::class, $dispatcher);
$hasher = new BcryptHasher();
/** @var \Mockery\MockInterface&\App\Services\CrmService $crmService */
$crmService = \Mockery::mock(CrmService::class);
$crmService->shouldReceive('login')
->once()
->with('unverified@example.com', 'valid-password')
->andThrow(new CrmCustomerNotActivatedException());
$provider = new CrmUserProvider($hasher, Customer::class, $crmService);
// WHEN
$isValid = $provider->validateCredentials($customer, [
'email' => 'unverified@example.com',
'password' => 'valid-password',
]);
// THEN
expect($isValid)->toBeTrue();
});
});