ldc-cocotte/tests/Integration/WithApp/Infra/Adapters/Services/AppCustomerAuthServiceTest.php

385 lines
12 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\Infra\Adapters\Services;
use App\Exceptions\CrmServiceException;
use App\Models\Customer;
use App\Services\CrmService;
use Domain\Contracts\CustomerDetails;
use Domain\Enums\Country;
use Domain\Exceptions\AuthServiceException;
use Domain\Models\ValueObjects\CrmCustomer;
use Domain\Models\ValueObjects\Email;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\StatefulGuard;
use Infrastructure\Adapters\Services\AppCustomerAuthService;
use Mockery;
use Shared\Helpers\UuidHelper;
use Shared\ValueObjects\CrmHashcode;
describe('AppCustomerAuthService', function (): void {
it('checks abilities with customer', function (): void {
// GIVEN
$guard = Mockery::mock(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$customer = new class () extends Customer
{
/** @param iterable<string>|string $abilities */
public function can($abilities, $arguments = []): bool
{
return $abilities === 'view-order';
}
};
$guard->shouldReceive('user')->andReturn($customer);
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
$result = $service->canWithCustomer('view-order');
// THEN
expect($result)->toBeTrue();
});
it('returns false for abilities if customer is null', function (): void {
// GIVEN
$guard = Mockery::mock(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$guard->shouldReceive('user')->andReturn(null);
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
$result = $service->canWithCustomer('view-order');
// THEN
expect($result)->toBeFalse();
});
it('logs in customer when active', function (): void {
// GIVEN
/** @var StatefulGuard&\Mockery\MockInterface $guard */
$guard = Mockery::spy(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$customer = new Customer();
$customer->id = (string) UuidHelper::v7();
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
$service->logIn($customer);
// THEN
$guard->shouldHaveReceived('login')->with($customer)->once();
});
it('throws exception if logging in non-authenticatable', function (): void {
// GIVEN
$guard = Mockery::mock(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$invalidCustomer = new class () implements CustomerDetails
{
};
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
/** @phpstan-ignore argument.type */
$action = fn () => $service->logIn($invalidCustomer);
// THEN
expect($action)->toThrow(\InvalidArgumentException::class, 'Expected an instance of Authenticatable and Customer for logging in');
});
it('logs out', function (): void {
// GIVEN
/** @var StatefulGuard&\Mockery\MockInterface $guard */
$guard = Mockery::spy(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
$service->logout();
// THEN
$guard->shouldHaveReceived('logout')->once();
});
it('checks if user is logged in', function (): void {
// GIVEN
$guard = Mockery::mock(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$guard->shouldReceive('check')->andReturn(true);
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
$check = $service->check();
$guest = $service->guest();
// THEN
expect($check)->toBeTrue()
->and($guest)->toBeFalse();
});
it('gets customer safely', function (): void {
// GIVEN
$guard = Mockery::mock(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$customer = new Customer();
$guard->shouldReceive('user')->andReturn($customer);
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
$returned = $service->getCustomer();
// THEN
expect($returned)->toBeInstanceOf(Customer::class)
->and($returned)->not->toBe($customer); // Should be a clone
});
it('throws exception when customer is not a frontend Customer model', function (): void {
// GIVEN
$guard = Mockery::mock(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$user = new class () implements Authenticatable
{
public function getAuthIdentifierName()
{
return 'id';
}
public function getAuthIdentifier()
{
return 1;
}
public function getAuthPassword()
{
return 'password';
}
public function getAuthPasswordName()
{
return 'password';
}
public function getRememberToken(): string
{
return '';
}
public function setRememberToken($value)
{
}
public function getRememberTokenName()
{
return '';
}
};
$guard->shouldReceive('user')->andReturn($user);
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
$action = fn () => $service->getCustomer();
// THEN
expect($action)->toThrow(\InvalidArgumentException::class, 'Auth service needs a frontend guard');
});
it('gets customer as mandatory', function (): void {
// GIVEN
$guard = Mockery::mock(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$customer = new Customer();
$customer->id = 'cust-123';
$guard->shouldReceive('user')->andReturn($customer);
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
$returned = $service->getCustomerAsMandatory();
// THEN
expect($returned->id)->toBe('cust-123');
});
it('throws exception when getting customer as mandatory but null', function (): void {
// GIVEN
$guard = Mockery::mock(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$guard->shouldReceive('user')->andReturn(null);
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
$action = fn () => $service->getCustomerAsMandatory();
// THEN
expect($action)->toThrow(AuthServiceException::class, 'Authenticated user is required from here');
});
it('resets password', function (): void {
// GIVEN
$guard = Mockery::mock(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$customer = new Customer();
$customer->email = new Email('test@example.com');
$crmService->shouldReceive('resetPasswordFor')->with($customer->email, 'newpass')->andReturn(true);
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
$result = $service->resetPasswordFor($customer, 'newpass');
// THEN
expect($result)->toBeTrue();
});
it('checks if email is verified', function (): void {
// GIVEN
$guard = Mockery::mock(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$customer = new Customer();
$hashcode = new CrmHashcode(UuidHelper::v4());
$customer->hashcode = $hashcode;
// DO NOT MOCK CrmCustomer
$crmCustomer = new CrmCustomer(
hashcode: $hashcode->value,
email: 'test@example.com',
password: 'password',
firstname: 'John',
lastname: 'Doe',
birthDate: '1990-01-01',
phone: '123456789',
address: '123 Main St',
address2: null,
city: 'Paris',
postcode: '75000',
country: Country::france,
optIn: true,
status: true,
lastConnection: null
);
$crmService->shouldReceive('getCustomerFromHashCode')->with($customer->hashcode)->andReturn($crmCustomer);
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
$result = $service->hasVerifiedEmail($customer);
// THEN
expect($result)->toBeTrue();
});
it('returns false for hasVerifiedEmail if crm customer not found', function (): void {
// GIVEN
$guard = Mockery::mock(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$customer = new Customer();
$hashcode = new CrmHashcode(UuidHelper::v4());
$customer->hashcode = $hashcode;
$crmService->shouldReceive('getCustomerFromHashCode')->with($customer->hashcode)->andReturn(null);
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
$result = $service->hasVerifiedEmail($customer);
// THEN
expect($result)->toBeFalse();
});
it('verifies email successfully', function (): void {
// GIVEN
$guard = Mockery::mock(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$customer = new Customer();
$hashcode = new CrmHashcode(UuidHelper::v4());
$customer->hashcode = $hashcode;
$crmCustomer = new CrmCustomer(
hashcode: $hashcode->value,
email: 'test@example.com',
password: 'password',
firstname: 'John',
lastname: 'Doe',
birthDate: '1990-01-01',
phone: '123456789',
address: '123 Main St',
address2: null,
city: 'Paris',
postcode: '75000',
country: Country::france,
optIn: true,
status: true,
lastConnection: null
);
$crmService->shouldReceive('getCustomerFromHashCode')->with($customer->hashcode)->andReturn($crmCustomer);
$activation = new \stdClass();
$activation->result = true;
$crmService->shouldReceive('activate')->with($crmCustomer)->andReturn($activation);
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
$result = $service->verifyEmail($customer);
// THEN
expect($result)->toBeTrue();
});
it('returns false for verifyEmail if crm customer not found', function (): void {
// GIVEN
$guard = Mockery::mock(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$customer = new Customer();
$hashcode = new CrmHashcode(UuidHelper::v4());
$customer->hashcode = $hashcode;
$crmService->shouldReceive('getCustomerFromHashCode')->with($customer->hashcode)->andReturn(null);
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
$result = $service->verifyEmail($customer);
// THEN
expect($result)->toBeFalse();
});
it('throws CrmServiceException from getCrmCustomerFrom', function (): void {
// GIVEN
$guard = Mockery::mock(StatefulGuard::class);
$crmService = Mockery::mock(CrmService::class);
$customer = new Customer();
$hashcode = new CrmHashcode(UuidHelper::v4());
$customer->hashcode = $hashcode;
$exception = new CrmServiceException('Crm Error');
$crmService->shouldReceive('getCustomerFromHashCode')->with($customer->hashcode)->andReturn($exception);
$service = new AppCustomerAuthService($guard, $crmService);
// WHEN
$action = fn () => $service->hasVerifiedEmail($customer);
// THEN
expect($action)->toThrow(CrmServiceException::class, 'Crm Error');
});
});