From 01af744398fd22c99f6f741655ac8a43fc55e487 Mon Sep 17 00:00:00 2001 From: Proxynth Date: Tue, 7 Jul 2026 13:23:28 +0200 Subject: [PATCH] test: forbid config helper in application layer --- .../Application/UseCases/ReceiveWebhook.php | 10 +++---- .../Application/Data/RetryConfiguration.php | 18 +++++++++++++ .../Ports/RetryConfigurationResolver.php | 12 +++++++++ .../ConfigRetryConfigurationResolver.php | 19 ++++++++++++++ .../Providers/LarawebhookServiceProvider.php | 4 +++ tests/ArchTest.php | 14 +++++----- .../FakeRetryConfigurationResolver.php | 20 ++++++++++++++ .../UseCases/ReceiveWebhookTest.php | 26 ++++++++++++------- 8 files changed, 100 insertions(+), 23 deletions(-) create mode 100644 src/Processing/Application/Data/RetryConfiguration.php create mode 100644 src/Processing/Application/Ports/RetryConfigurationResolver.php create mode 100644 src/Processing/Infrastructure/Config/ConfigRetryConfigurationResolver.php create mode 100644 tests/Fakes/Processing/FakeRetryConfigurationResolver.php diff --git a/src/Ingestion/Application/UseCases/ReceiveWebhook.php b/src/Ingestion/Application/UseCases/ReceiveWebhook.php index 8eff575..7050980 100644 --- a/src/Ingestion/Application/UseCases/ReceiveWebhook.php +++ b/src/Ingestion/Application/UseCases/ReceiveWebhook.php @@ -19,6 +19,7 @@ use Proxynth\Larawebhook\Ingestion\Domain\ValueObjects\Provider; use Proxynth\Larawebhook\Ingestion\Domain\ValueObjects\RawPayload; use Proxynth\Larawebhook\Processing\Application\Ports\IdempotencyResolver; +use Proxynth\Larawebhook\Processing\Application\Ports\RetryConfigurationResolver; use Proxynth\Larawebhook\Processing\Application\Ports\WebhookDuplicateDetector; use Proxynth\Larawebhook\Processing\Domain\Entities\WebhookEvent; use Proxynth\Larawebhook\Processing\Domain\ValueObjects\DeliveryAttempt; @@ -34,6 +35,7 @@ public function __construct( private WebhookDuplicateDetector $duplicateDetector, private WebhookPayloadParserResolver $payloadParserResolver, private WebhookSecretResolver $secretResolver, + private RetryConfigurationResolver $retryConfigurationResolver, private ValidateWebhook $validateWebhook, private RecordWebhookLog $recordWebhookLog, ) {} @@ -155,7 +157,7 @@ public function handle(ReceiveWebhookCommand $command): ReceiveWebhookResult errorMessage: $validation->errorMessage, )); - if ($this->shouldRetryAsync()) { + if ($this->retryConfigurationResolver->resolve()->shouldRetryAsync()) { return ReceiveWebhookResult::acceptedForRetry( log: $log, event: $event, @@ -216,12 +218,6 @@ private function extractExternalId( ->extractExternalId($decodedPayload, $externalIdHeaderValue); } - private function shouldRetryAsync(): bool - { - return config('larawebhook.retries.enabled', true) - && config('larawebhook.retries.async', false); - } - private function failureStatusCode(?string $errorMessage): int { $errorMessage ??= ''; diff --git a/src/Processing/Application/Data/RetryConfiguration.php b/src/Processing/Application/Data/RetryConfiguration.php new file mode 100644 index 0000000..2661a7e --- /dev/null +++ b/src/Processing/Application/Data/RetryConfiguration.php @@ -0,0 +1,18 @@ +enabled && $this->async; + } +} diff --git a/src/Processing/Application/Ports/RetryConfigurationResolver.php b/src/Processing/Application/Ports/RetryConfigurationResolver.php new file mode 100644 index 0000000..2f4c8bd --- /dev/null +++ b/src/Processing/Application/Ports/RetryConfigurationResolver.php @@ -0,0 +1,12 @@ +app->singleton(WebhookSecretResolver::class, ConfigWebhookSecretResolver::class); $this->app->singleton(RetryPolicyResolver::class, ConfigRetryPolicyResolver::class); + + $this->app->singleton(RetryConfigurationResolver::class, ConfigRetryConfigurationResolver::class); } /** diff --git a/tests/ArchTest.php b/tests/ArchTest.php index c6e9e41..ee6e4d2 100644 --- a/tests/ArchTest.php +++ b/tests/ArchTest.php @@ -20,6 +20,10 @@ 'Proxynth\Larawebhook\Shared\Application', ]; +$pureApplications = array_values(array_diff($applications, [ + 'Proxynth\Larawebhook\Shared\Application\Larawebhook', +])); + $controllers = [ 'Proxynth\Larawebhook\Audit\Infrastructure\Laravel\Http\Controllers', 'Proxynth\Larawebhook\Ingestion\Infrastructure\Laravel\Http\Controllers', @@ -109,10 +113,6 @@ 'Proxynth\Larawebhook\Shared\Infrastructure\Laravel\Http\Controllers', ]); -arch('application does not call config helper') - ->expect([ - 'Proxynth\Larawebhook\Ingestion\Application', - 'Proxynth\Larawebhook\Processing\Application', - 'Proxynth\Larawebhook\Audit\Application', - ]) - ->not->toUse('config'); +arch('application layer does not call config helper') + ->expect('config') + ->not->toBeUsedIn($pureApplications); diff --git a/tests/Fakes/Processing/FakeRetryConfigurationResolver.php b/tests/Fakes/Processing/FakeRetryConfigurationResolver.php new file mode 100644 index 0000000..d5d4221 --- /dev/null +++ b/tests/Fakes/Processing/FakeRetryConfigurationResolver.php @@ -0,0 +1,20 @@ +configuration; + } +} diff --git a/tests/Unit/Ingestion/Application/UseCases/ReceiveWebhookTest.php b/tests/Unit/Ingestion/Application/UseCases/ReceiveWebhookTest.php index 900ccb1..987e5be 100644 --- a/tests/Unit/Ingestion/Application/UseCases/ReceiveWebhookTest.php +++ b/tests/Unit/Ingestion/Application/UseCases/ReceiveWebhookTest.php @@ -13,9 +13,12 @@ use Proxynth\Larawebhook\Ingestion\Domain\Events\WebhookRejected; use Proxynth\Larawebhook\Ingestion\Domain\Events\WebhookValidated; use Proxynth\Larawebhook\Ingestion\Domain\ValueObjects\Signature; +use Proxynth\Larawebhook\Processing\Application\Data\RetryConfiguration; +use Proxynth\Larawebhook\Processing\Application\Ports\RetryConfigurationResolver; use Proxynth\Larawebhook\Processing\Application\Ports\WebhookDuplicateDetector; use Proxynth\Larawebhook\Shared\Domain\Enums\WebhookService; use Proxynth\Larawebhook\Tests\Fakes\Ingestion\FakeWebhookSecretResolver; +use Proxynth\Larawebhook\Tests\Fakes\Processing\FakeRetryConfigurationResolver; use Proxynth\Larawebhook\Tests\Fakes\Processing\FakeWebhookDuplicateDetector; function githubPayload(string $deliveryId = 'delivery_123'): string @@ -45,7 +48,13 @@ function githubSignature(string $payload, string $secret = 'github_secret'): Sig config()->set('larawebhook.services.github.webhook_secret', 'secret'); config()->set('larawebhook.retries.enabled', true); - config()->set('larawebhook.retries.async', false); + app()->instance( + RetryConfigurationResolver::class, + new FakeRetryConfigurationResolver(new RetryConfiguration( + enabled: true, + async: false, + )), + ); }); it('receives a valid webhook and logs it successfully', function () { @@ -150,9 +159,6 @@ function githubSignature(string $payload, string $secret = 'github_secret'): Sig }); it('returns failed when signature is invalid and async retries are disabled', function () { - config()->set('larawebhook.retries.enabled', true); - config()->set('larawebhook.retries.async', false); - app()->instance(WebhookSecretResolver::class, new FakeWebhookSecretResolver([ 'github' => 'github_secret', ])); @@ -179,8 +185,13 @@ function githubSignature(string $payload, string $secret = 'github_secret'): Sig }); it('returns accepted for retry when signature is invalid and async retires are enabled', function () { - config()->set('larawebhook.retries.enabled', true); - config()->set('larawebhook.retries.async', true); + app()->instance( + RetryConfigurationResolver::class, + new FakeRetryConfigurationResolver(new RetryConfiguration( + enabled: true, + async: true, + )), + ); app()->instance(WebhookSecretResolver::class, new FakeWebhookSecretResolver([ 'github' => 'github_secret', @@ -271,9 +282,6 @@ function githubSignature(string $payload, string $secret = 'github_secret'): Sig }); it('marks webhook as failed when validation fails and async retries are disabled', function () { - config()->set('larawebhook.retries.enabled', true); - config()->set('larawebhook.retries.async', false); - app()->instance(WebhookSecretResolver::class, new FakeWebhookSecretResolver([ 'github' => 'github_secret', ]));