test(tests): remove Reflection attribute/constructor hacks and pass-through InitializeOrderTunnel (lot 4A)
This commit is contained in:
parent
49c17d61f0
commit
8a6b1442e6
5 changed files with 95 additions and 145 deletions
|
|
@ -4,13 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Tests\Integration\WithApp\App\Cqs\Actions\Customers;
|
||||
|
||||
use App\Attributes\Locked;
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Contracts\Executor;
|
||||
use App\Contracts\HandlesFailure;
|
||||
use App\Contracts\HandlesLockTimeout;
|
||||
use App\Contracts\HandlesSuccess;
|
||||
use App\Contracts\ProvidesLockKey;
|
||||
use App\Cqs\Actions\Customers\RegisterCustomer;
|
||||
use App\Cqs\Actions\Customers\RegisterCustomerInput;
|
||||
use App\Cqs\Actions\Customers\RegisterCustomerOutput;
|
||||
|
|
@ -19,6 +14,7 @@ use App\Exceptions\Crm\CrmCustomerExistsException;
|
|||
use App\Exceptions\CrmServiceException;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Employee;
|
||||
use App\Services\ActionExecutorService;
|
||||
use App\Services\CrmService;
|
||||
use Domain\Enums\Country;
|
||||
use Domain\Models\ValueObjects\Code;
|
||||
|
|
@ -35,8 +31,12 @@ use Illuminate\Support\Facades\Date;
|
|||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Infrastructure\Adapters\Events\LaravelDomainEventDispatcher;
|
||||
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
||||
use Psr\Log\NullLogger;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Shared\Concurrency\Lock;
|
||||
use Shared\Concurrency\LockTimeoutException;
|
||||
use Shared\ValueObjects\CrmHashcode;
|
||||
|
||||
describe('RegisterCustomer action', function (): void {
|
||||
|
|
@ -242,7 +242,7 @@ describe('RegisterCustomer action', function (): void {
|
|||
$result = $action($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(RegisterCustomerOutput::class)
|
||||
expect($result)
|
||||
->and($result->status)->toBe(RegisterCustomerStatus::EMPLOYEE_INACTIVE)
|
||||
->and($result->customer)->toBeNull()
|
||||
->and(Customer::query()->count())->toBe(0);
|
||||
|
|
@ -450,30 +450,49 @@ describe('RegisterCustomer action', function (): void {
|
|||
expect(fn () => $action($invalidInput))->toThrow(\InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
it('has Locked attribute with expected TTL and wait seconds and implements lock contracts', function (): void {
|
||||
it('acquires matricule lock via ActionExecutorService and returns LOCK_TIMEOUT when lock fails', function (): void {
|
||||
// GIVEN
|
||||
$crmService = \Mockery::mock(CrmService::class);
|
||||
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
||||
$action = new RegisterCustomer(
|
||||
$crmService = \Mockery::mock(CrmService::class);
|
||||
$crmService->shouldNotReceive('persist');
|
||||
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
||||
$action = new RegisterCustomer(
|
||||
authService: $authService,
|
||||
crmService: $crmService,
|
||||
);
|
||||
|
||||
$reflection = new \ReflectionClass(RegisterCustomer::class);
|
||||
$attributes = $reflection->getAttributes(Locked::class);
|
||||
$input = new RegisterCustomerInput(
|
||||
matricule: new Matricule('1234567'),
|
||||
code: new Code('ABCDEF'),
|
||||
firstname: 'John',
|
||||
lastname: 'Doe',
|
||||
email: new Email('john.doe@example.com'),
|
||||
password: 'secretPassword123!',
|
||||
birthDate: new \DateTimeImmutable('1990-01-01'),
|
||||
phone: null,
|
||||
address: null,
|
||||
postcode: '75001',
|
||||
city: 'Paris',
|
||||
);
|
||||
|
||||
/** @var Lock&\Mockery\MockInterface $lock */
|
||||
$lock = \Mockery::mock(Lock::class);
|
||||
$lock->shouldReceive('synchronize')
|
||||
->once()
|
||||
->with('customer:register:employee:1234567', \Mockery::type(\Closure::class), 30, 10)
|
||||
->andThrow(new LockTimeoutException('Lock timed out'));
|
||||
|
||||
$executor = new ActionExecutorService(
|
||||
$lock,
|
||||
new NullLogger(),
|
||||
new LaravelDomainEventDispatcher(),
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$locked = ($attributes[0] ?? null)?->newInstance();
|
||||
$output = $executor->execute($action, $input);
|
||||
|
||||
// THEN
|
||||
expect($locked)->toBeInstanceOf(Locked::class)
|
||||
->and($locked->ttlSeconds)->toBe(30)
|
||||
->and($locked->waitSeconds)->toBe(10)
|
||||
->and($locked->key)->toBeNull()
|
||||
->and($action)->toBeInstanceOf(ProvidesLockKey::class)
|
||||
->and($action)->toBeInstanceOf(HandlesLockTimeout::class)
|
||||
->and($action)->toBeInstanceOf(HandlesSuccess::class)
|
||||
->and($action)->toBeInstanceOf(HandlesFailure::class);
|
||||
expect($output)
|
||||
->and($output->status)->toBe(RegisterCustomerStatus::LOCK_TIMEOUT);
|
||||
});
|
||||
|
||||
it('builds lockKey from input matricule', function (): void {
|
||||
|
|
@ -533,7 +552,7 @@ describe('RegisterCustomer action', function (): void {
|
|||
$result = $action->onLockTimeout($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(RegisterCustomerOutput::class)
|
||||
expect($result)
|
||||
->and($result->status)->toBe(RegisterCustomerStatus::LOCK_TIMEOUT)
|
||||
->and($result->customer)->toBeNull()
|
||||
->and($result->errorMessage)->toBeNull();
|
||||
|
|
|
|||
|
|
@ -465,4 +465,52 @@ describe('Order placement two-step CQS actions', function (): void {
|
|||
);
|
||||
expect($lockOut->status)->toBe(PlacePendingOrderStatus::LOCK_TIMEOUT);
|
||||
});
|
||||
|
||||
it('executes ExportOrderToRegate without opening a DB transaction and returns LOCK_TIMEOUT when lock fails', function (): void {
|
||||
// GIVEN
|
||||
$orderId = new OrderId(Uuid::uuid7());
|
||||
$input = new ExportOrderToRegateInput($orderId);
|
||||
$levelBeforeExecute = DB::transactionLevel();
|
||||
$levelInsideAction = null;
|
||||
|
||||
/** @var OrderTunnelService&\Mockery\MockInterface $tunnelService */
|
||||
$tunnelService = \Mockery::mock(OrderTunnelService::class);
|
||||
$tunnelService->shouldReceive('exportOrderToRegate')
|
||||
->once()
|
||||
->with($orderId)
|
||||
->andReturnUsing(function () use (&$levelInsideAction): void {
|
||||
$levelInsideAction = DB::transactionLevel();
|
||||
});
|
||||
|
||||
$memoryExecutor = new ActionExecutorService(
|
||||
new LaravelCacheLock(new Repository(new ArrayStore())),
|
||||
new NullLogger(),
|
||||
new LaravelDomainEventDispatcher(),
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$sentOutput = $memoryExecutor->execute(new ExportOrderToRegate($tunnelService), $input);
|
||||
|
||||
// GIVEN (contended lock)
|
||||
/** @var Lock&\Mockery\MockInterface $contendedLock */
|
||||
$contendedLock = \Mockery::mock(Lock::class);
|
||||
$contendedLock->shouldReceive('synchronize')
|
||||
->once()
|
||||
->with("order:export:{$orderId->value}", \Mockery::type(\Closure::class), 30, 10)
|
||||
->andThrow(new LockTimeoutException('Lock timed out'));
|
||||
|
||||
$contendedExecutor = new ActionExecutorService(
|
||||
$contendedLock,
|
||||
new NullLogger(),
|
||||
new LaravelDomainEventDispatcher(),
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$timeoutOutput = $contendedExecutor->execute(new ExportOrderToRegate($tunnelService), $input);
|
||||
|
||||
// THEN
|
||||
expect($sentOutput)->toBe(ExportOrderToRegateOutput::SENT)
|
||||
->and($levelInsideAction)->toBe($levelBeforeExecute)
|
||||
->and($timeoutOutput)->toBe(ExportOrderToRegateOutput::LOCK_TIMEOUT);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,38 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\App\Cqs\Actions\Orders;
|
||||
|
||||
use App\Cqs\Actions\Orders\InitializeOrderTunnel;
|
||||
use App\Cqs\Actions\Orders\InitializeOrderTunnelInput;
|
||||
use Domain\Models\Entities\Basket;
|
||||
use Domain\Models\ValueObjects\Entities\BasketId;
|
||||
use Domain\Models\ValueObjects\Entities\ShopperId;
|
||||
use Domain\Services\OrderTunnelService;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
describe('InitializeOrderTunnel', function (): void {
|
||||
it('initializes the tunnel from basket via OrderTunnelService', function (): void {
|
||||
// GIVEN
|
||||
$basket = new Basket(new BasketId(Uuid::uuid7()));
|
||||
$shopperId = new ShopperId(Uuid::uuid7());
|
||||
$input = new InitializeOrderTunnelInput(
|
||||
basket: $basket,
|
||||
shopperId: $shopperId,
|
||||
);
|
||||
|
||||
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
|
||||
$orderTunnelService->shouldReceive('initializeFromBasket')
|
||||
->once()
|
||||
->with($basket, $shopperId);
|
||||
|
||||
$action = new InitializeOrderTunnel($orderTunnelService);
|
||||
|
||||
// WHEN
|
||||
$action($input);
|
||||
|
||||
// THEN
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
|
@ -4,12 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Tests\Unit\App\Cqs\Actions\Orders;
|
||||
|
||||
use App\Attributes\Locked;
|
||||
use App\Attributes\WithoutTransaction;
|
||||
use App\Contracts\HandlesFailure;
|
||||
use App\Contracts\HandlesLockTimeout;
|
||||
use App\Contracts\HandlesSuccess;
|
||||
use App\Contracts\ProvidesLockKey;
|
||||
use App\Cqs\Actions\Orders\CancelPendingOrder;
|
||||
use App\Cqs\Actions\Orders\CancelPendingOrderInput;
|
||||
use App\Cqs\Actions\Orders\CancelPendingOrderOutput;
|
||||
|
|
@ -195,7 +191,7 @@ describe('PlacePendingOrder', function (): void {
|
|||
->and($result->errorMessage)->toBe('The price of one or more products in your basket has changed.');
|
||||
});
|
||||
|
||||
it('has Locked attribute and lock key contracts', function (): void {
|
||||
it('builds shopper lock key and returns LOCK_TIMEOUT output on lock timeout', function (): void {
|
||||
// GIVEN
|
||||
$shopperId = new ShopperId(Uuid::uuid7());
|
||||
$input = new PlacePendingOrderInput(shopperId: $shopperId);
|
||||
|
|
@ -204,40 +200,19 @@ describe('PlacePendingOrder', function (): void {
|
|||
/** @var \Mockery\MockInterface&BasketRepository $basketRepository */
|
||||
$basketRepository = \Mockery::mock(BasketRepository::class);
|
||||
|
||||
$action = new PlacePendingOrder($orderTunnelService, $basketRepository);
|
||||
$reflection = new \ReflectionClass(PlacePendingOrder::class);
|
||||
$attributes = $reflection->getAttributes(Locked::class);
|
||||
$locked = ($attributes[0] ?? null)?->newInstance();
|
||||
$action = new PlacePendingOrder($orderTunnelService, $basketRepository);
|
||||
|
||||
// WHEN
|
||||
$key = $action->lockKey($input);
|
||||
$timeout = $action->onLockTimeout($input);
|
||||
|
||||
// THEN
|
||||
expect($locked)->toBeInstanceOf(Locked::class)
|
||||
->and($locked?->ttlSeconds)->toBe(30)
|
||||
->and($locked?->waitSeconds)->toBe(10)
|
||||
->and($action)->toBeInstanceOf(ProvidesLockKey::class)
|
||||
->and($action)->toBeInstanceOf(HandlesLockTimeout::class)
|
||||
->and($key)->toBe("order:finalize:shopper:{$shopperId->value}")
|
||||
expect($key)->toBe("order:finalize:shopper:{$shopperId->value}")
|
||||
->and($timeout->status)->toBe(PlacePendingOrderStatus::LOCK_TIMEOUT);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ExportOrderToRegate, ConfirmPlacedOrder and CancelPendingOrder', function (): void {
|
||||
it('marks ExportOrderToRegate with WithoutTransaction and Locked attributes', function (): void {
|
||||
// GIVEN
|
||||
$reflection = new \ReflectionClass(ExportOrderToRegate::class);
|
||||
|
||||
// WHEN
|
||||
$withoutTx = $reflection->getAttributes(WithoutTransaction::class);
|
||||
$locked = $reflection->getAttributes(Locked::class);
|
||||
|
||||
// THEN
|
||||
expect($withoutTx)->toHaveCount(1)
|
||||
->and($locked)->toHaveCount(1);
|
||||
});
|
||||
|
||||
it('exports order via OrderTunnelService and handles lock timeout', function (): void {
|
||||
// GIVEN
|
||||
$orderId = new OrderId(Uuid::uuid7());
|
||||
|
|
|
|||
|
|
@ -4,15 +4,17 @@ declare(strict_types=1);
|
|||
|
||||
namespace Tests\Unit\Domain\Models\ValueObjects;
|
||||
|
||||
use Domain\Enums\DiscountType;
|
||||
use Domain\Models\ValueObjects\DiscountValue;
|
||||
use Money\Currency;
|
||||
use Money\Money;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
describe('DiscountValue', function () {
|
||||
|
||||
function getDiscountMoney(int $amount, string $currency = 'EUR'): Money
|
||||
{
|
||||
Assert::notEmpty($currency);
|
||||
|
||||
return new Money($amount, new Currency($currency));
|
||||
}
|
||||
|
||||
|
|
@ -24,7 +26,6 @@ describe('DiscountValue', function () {
|
|||
$vo = DiscountValue::fromAmount($amount);
|
||||
|
||||
// THEN
|
||||
expect($vo)->toBeInstanceOf(DiscountValue::class);
|
||||
expect($vo->isPercentage())->toBeFalse();
|
||||
});
|
||||
|
||||
|
|
@ -36,7 +37,6 @@ describe('DiscountValue', function () {
|
|||
$vo = DiscountValue::fromPercentage($rate);
|
||||
|
||||
// THEN
|
||||
expect($vo)->toBeInstanceOf(DiscountValue::class);
|
||||
expect($vo->isPercentage())->toBeTrue();
|
||||
});
|
||||
|
||||
|
|
@ -174,57 +174,3 @@ describe('DiscountValue', function () {
|
|||
expect($action)->toThrow(\DomainException::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('DiscountValue exceptions', function (): void {
|
||||
it('throws when percentage is missing for percent type', function (): void {
|
||||
// GIVEN
|
||||
$reflection = new \ReflectionClass(DiscountValue::class);
|
||||
$method = $reflection->getMethod('__construct');
|
||||
$vo = $reflection->newInstanceWithoutConstructor();
|
||||
// WHEN
|
||||
$action = fn () => $method->invoke($vo, DiscountType::PERCENT, null, null);
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class);
|
||||
});
|
||||
|
||||
it('throws when amount is missing for amount type', function (): void {
|
||||
// GIVEN
|
||||
$reflection = new \ReflectionClass(DiscountValue::class);
|
||||
$method = $reflection->getMethod('__construct');
|
||||
$vo = $reflection->newInstanceWithoutConstructor();
|
||||
// WHEN
|
||||
$action = fn () => $method->invoke($vo, DiscountType::AMOUNT, null, null);
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class);
|
||||
});
|
||||
|
||||
it('throws unexpected value if percentage is null', function (): void {
|
||||
// GIVEN
|
||||
$reflection = new \ReflectionClass(DiscountValue::class);
|
||||
$vo = $reflection->newInstanceWithoutConstructor();
|
||||
$prop = $reflection->getProperty('type');
|
||||
$prop->setValue($vo, DiscountType::PERCENT);
|
||||
$prop2 = $reflection->getProperty('percentage');
|
||||
$prop2->setValue($vo, null);
|
||||
|
||||
// WHEN
|
||||
$action = fn () => $vo->getPercentage();
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class);
|
||||
});
|
||||
|
||||
it('throws unexpected value if amount is null', function (): void {
|
||||
// GIVEN
|
||||
$reflection = new \ReflectionClass(DiscountValue::class);
|
||||
$vo = $reflection->newInstanceWithoutConstructor();
|
||||
$prop = $reflection->getProperty('type');
|
||||
$prop->setValue($vo, DiscountType::AMOUNT);
|
||||
$prop2 = $reflection->getProperty('amount');
|
||||
$prop2->setValue($vo, null);
|
||||
|
||||
// WHEN
|
||||
$action = fn () => $vo->getAmount();
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue