From 2f9f281dfffe2356b2a1e5a70b7e6957465d8dcd Mon Sep 17 00:00:00 2001 From: David Funke Date: Mon, 22 Jul 2024 19:17:44 +0200 Subject: [PATCH] Added unit tests for RequestConfig --- .../Tests/Request/CommonRequestTest.php | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/SaferpayJson/Tests/Request/CommonRequestTest.php b/tests/SaferpayJson/Tests/Request/CommonRequestTest.php index 1937577..27750fb 100644 --- a/tests/SaferpayJson/Tests/Request/CommonRequestTest.php +++ b/tests/SaferpayJson/Tests/Request/CommonRequestTest.php @@ -6,6 +6,7 @@ use GuzzleHttp\Client; use GuzzleHttp\Psr7\Response as GuzzleResponse; use GuzzleHttp\Psr7\Utils as GuzzleUtils; +use InvalidArgumentException; use JMS\Serializer\SerializerBuilder; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -30,6 +31,36 @@ public function testErrorResponse(): void $this->executeRequest(); } + public function getRequestConfigValidationParams(): array + { + return [ + 'first try' => [null, RequestConfig::MIN_RETRY_INDICATOR], + 'second try' => [uniqid(), RequestConfig::MIN_RETRY_INDICATOR + 1], + 'last try' => [uniqid(), RequestConfig::MAX_RETRY_INDICATOR], + 'try after all retries exceeded' => [uniqid(), RequestConfig::MAX_RETRY_INDICATOR + 1, InvalidArgumentException::class], + 'retry without previous request id' => [null, RequestConfig::MAX_RETRY_INDICATOR, InvalidArgumentException::class], + ]; + } + + public function testRequestConfigValidation( + ?string $requestId, + int $retryIndicator, + ?string $expectedException = null): void + { + if ($expectedException !== null) { + $this->expectException($expectedException, $requestId, $retryIndicator); + } + + new RequestConfig( + 'apiKey', + 'apiSecret', + 'customerId', + false, + $requestId, + $retryIndicator + ); + } + public function doTestSuccessfulResponse(string $responseClass): void { $this->successful = true; @@ -87,7 +118,7 @@ private function getResponseMock(): MockObject $response->expects($this->any()) ->method('getStatusCode') - ->will($this->returnValue($this->successful ? 200: 404)); + ->will($this->returnValue($this->successful ? 200 : 404)); if ($this->successful) { $content = $this->getFakedApiResponse($this->successfulResponseClass);