diff --git a/Test/Unit/Gateway/Http/Client/TransactionCaptureTest.php b/Test/Unit/Gateway/Http/Client/TransactionCaptureTest.php new file mode 100644 index 000000000..4c161dda7 --- /dev/null +++ b/Test/Unit/Gateway/Http/Client/TransactionCaptureTest.php @@ -0,0 +1,125 @@ +adyenHelper = $this->createMock(Data::class); + $adyenLogger = $this->createMock(AdyenLogger::class); + $this->idempotencyHelper = $this->createMock(Idempotency::class); + + $this->transactionCapture = new TransactionCapture( + $this->adyenHelper, + $adyenLogger, + $this->idempotencyHelper + ); + + $this->request = [ + 'amount' => ['value' => 100, 'currency' => 'USD'], + 'paymentPspReference' => 'testPspReference' + ]; + + $this->transferObject = $this->createConfiguredMock(TransferInterface::class, [ + 'getBody' => $this->request, + 'getHeaders' => ['idempotencyExtraData' => ['someData']], + 'getClientConfig' => [] + ]); + } + + private function configureAdyenMocks(array $response = null, \Exception $exception = null) + { + $adyenClient = $this->createMock(\Adyen\Client::class); + $adyenService = $this->createMock(Checkout::class); + $expectedIdempotencyKey = 'generated_idempotency_key'; + + $this->adyenHelper->method('initializeAdyenClientWithClientConfig')->willReturn($adyenClient); + $this->adyenHelper->method('createAdyenCheckoutService')->willReturn($adyenService); + $this->adyenHelper->method('buildRequestHeaders')->willReturn([]); + $this->adyenHelper->expects($this->once())->method('logRequest'); + $this->adyenHelper->expects($this->once())->method('logResponse'); + + $this->idempotencyHelper->expects($this->once()) + ->method('generateIdempotencyKey') + ->with( + $this->request, + $this->equalTo(['someData']) + ) + ->willReturn($expectedIdempotencyKey); + + if ($response) { + $adyenService->expects($this->once()) + ->method('captures') + ->with( + $this->equalTo($this->request), + $this->callback(function ($requestOptions) use ($expectedIdempotencyKey) { + return isset($requestOptions['idempotencyKey']) && + $requestOptions['idempotencyKey'] === $expectedIdempotencyKey; + }) + ) + ->willReturn($response); + } + + if ($exception) { + $adyenService->expects($this->once()) + ->method('captures') + ->with( + $this->equalTo($this->request), + $this->callback(function ($requestOptions) use ($expectedIdempotencyKey) { + return isset($requestOptions['idempotencyKey']) && + $requestOptions['idempotencyKey'] === $expectedIdempotencyKey; + }) + ) + ->willThrowException($exception); + } + + return $adyenService; + } + + public function testPlaceRequest() + { + $expectedResponse = [ + 'capture_amount' => $this->request['amount']['value'], + 'paymentPspReference' => $this->request['paymentPspReference'] + ]; + + $this->configureAdyenMocks($expectedResponse); + + // Call the method under test + $response = $this->transactionCapture->placeRequest($this->transferObject); + + // Assert that the response is as expected + $this->assertEquals($expectedResponse, $response); + } + + public function testPlaceRequestWithException() + { + $expectedException = new AdyenException('Test exception'); + + $this->configureAdyenMocks(null, $expectedException); + + // Call the method under test + $response = $this->transactionCapture->placeRequest($this->transferObject); + + // Assert that the response contains the error message + $this->assertArrayHasKey('error', $response); + $this->assertEquals('Test exception', $response['error']); + } +}