From d34ef10acf65d26d48f17f601f0d08d615abae66 Mon Sep 17 00:00:00 2001 From: mustapayev Date: Fri, 6 Sep 2024 11:22:59 +0200 Subject: [PATCH] tests - cover event dispatch block --- src/Gateways/AbstractGateway.php | 21 ++- src/Gateways/AkbankPos.php | 3 +- src/Gateways/EstPos.php | 3 +- src/Gateways/GarantiPos.php | 3 +- src/Gateways/InterPos.php | 3 +- src/Gateways/KuveytPos.php | 6 +- src/Gateways/PayFlexCPV4Pos.php | 6 +- src/Gateways/PayFlexV4Pos.php | 6 +- src/Gateways/PayForPos.php | 3 +- src/Gateways/PosNet.php | 9 +- src/Gateways/PosNetV1Pos.php | 3 +- src/Gateways/ToslaPos.php | 3 +- src/Gateways/VakifKatilimPos.php | 6 +- tests/Unit/Gateways/AkbankPosTest.php | 31 +++-- tests/Unit/Gateways/EstPosTest.php | 29 ++++- tests/Unit/Gateways/GarantiPosTest.php | 29 ++++- tests/Unit/Gateways/InterPosTest.php | 29 ++++- tests/Unit/Gateways/KuveytPosTest.php | 61 ++++++--- tests/Unit/Gateways/PayFlexCPV4PosTest.php | 30 ++++- tests/Unit/Gateways/PayFlexV4PosTest.php | 30 ++++- tests/Unit/Gateways/PayForTest.php | 28 +++- tests/Unit/Gateways/PosNetTest.php | 142 +++++++++++++++------ tests/Unit/Gateways/PosNetV1PosTest.php | 29 ++++- tests/Unit/Gateways/ToslaPosTest.php | 30 ++++- tests/Unit/Gateways/VakifKatilimTest.php | 32 +++-- tests/Unit/HttpClientTestTrait.php | 7 + 26 files changed, 433 insertions(+), 149 deletions(-) diff --git a/src/Gateways/AbstractGateway.php b/src/Gateways/AbstractGateway.php index 32c44c0f..4fd2b332 100644 --- a/src/Gateways/AbstractGateway.php +++ b/src/Gateways/AbstractGateway.php @@ -238,7 +238,8 @@ public function makeRegularPayment(array $order, CreditCardInterface $creditCard $order, PosInterface::MODEL_NON_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), @@ -282,7 +283,8 @@ public function makeRegularPostPayment(array $order): PosInterface $order, PosInterface::MODEL_NON_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), @@ -325,7 +327,8 @@ public function refund(array $order): PosInterface $order, PosInterface::MODEL_NON_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), @@ -368,7 +371,8 @@ public function cancel(array $order): PosInterface $order, PosInterface::MODEL_NON_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), @@ -411,7 +415,8 @@ public function status(array $order): PosInterface $order, PosInterface::MODEL_NON_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), @@ -450,7 +455,8 @@ public function history(array $data): PosInterface $data, PosInterface::MODEL_NON_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), @@ -489,7 +495,8 @@ public function orderHistory(array $order): PosInterface $order, PosInterface::MODEL_NON_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), diff --git a/src/Gateways/AkbankPos.php b/src/Gateways/AkbankPos.php index 54d2b752..442faea3 100644 --- a/src/Gateways/AkbankPos.php +++ b/src/Gateways/AkbankPos.php @@ -93,7 +93,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $order, PosInterface::MODEL_3D_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), diff --git a/src/Gateways/EstPos.php b/src/Gateways/EstPos.php index 9fc39856..d70723f7 100644 --- a/src/Gateways/EstPos.php +++ b/src/Gateways/EstPos.php @@ -92,7 +92,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $order, PosInterface::MODEL_3D_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), diff --git a/src/Gateways/GarantiPos.php b/src/Gateways/GarantiPos.php index 4824c711..0ca3d684 100644 --- a/src/Gateways/GarantiPos.php +++ b/src/Gateways/GarantiPos.php @@ -85,7 +85,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $order, PosInterface::MODEL_3D_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), diff --git a/src/Gateways/InterPos.php b/src/Gateways/InterPos.php index b2b450d9..9277dfb8 100644 --- a/src/Gateways/InterPos.php +++ b/src/Gateways/InterPos.php @@ -92,7 +92,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $order, PosInterface::MODEL_3D_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), diff --git a/src/Gateways/KuveytPos.php b/src/Gateways/KuveytPos.php index fcab2e4a..0f6dc55d 100644 --- a/src/Gateways/KuveytPos.php +++ b/src/Gateways/KuveytPos.php @@ -178,7 +178,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $order, PosInterface::MODEL_3D_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), @@ -345,7 +346,8 @@ private function getCommon3DFormData(KuveytPosAccount $kuveytPosAccount, array $ $order, $paymentModel ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), diff --git a/src/Gateways/PayFlexCPV4Pos.php b/src/Gateways/PayFlexCPV4Pos.php index fe9bab34..74361962 100644 --- a/src/Gateways/PayFlexCPV4Pos.php +++ b/src/Gateways/PayFlexCPV4Pos.php @@ -97,7 +97,8 @@ public function make3DPayPayment(Request $request, array $order, string $txType) $order, PosInterface::MODEL_3D_PAY ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), @@ -254,7 +255,8 @@ private function registerPayment(array $order, string $txType, string $paymentMo $order, $paymentModel ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), diff --git a/src/Gateways/PayFlexV4Pos.php b/src/Gateways/PayFlexV4Pos.php index 13af344f..765582af 100644 --- a/src/Gateways/PayFlexV4Pos.php +++ b/src/Gateways/PayFlexV4Pos.php @@ -87,7 +87,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $order, PosInterface::MODEL_3D_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), @@ -231,7 +232,8 @@ private function sendEnrollmentRequest(array $order, CreditCardInterface $credit $order, $paymentModel ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), diff --git a/src/Gateways/PayForPos.php b/src/Gateways/PayForPos.php index 4b0a2a05..09ea8522 100644 --- a/src/Gateways/PayForPos.php +++ b/src/Gateways/PayForPos.php @@ -86,7 +86,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $order, PosInterface::MODEL_3D_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), diff --git a/src/Gateways/PosNet.php b/src/Gateways/PosNet.php index bf7317a6..b19d1e19 100644 --- a/src/Gateways/PosNet.php +++ b/src/Gateways/PosNet.php @@ -78,7 +78,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $order, PosInterface::MODEL_3D_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), @@ -118,7 +119,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $order, PosInterface::MODEL_3D_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), @@ -266,7 +268,8 @@ private function getOosTransactionData(array $order, string $txType, string $pay $order, $paymentModel ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), diff --git a/src/Gateways/PosNetV1Pos.php b/src/Gateways/PosNetV1Pos.php index adf17d20..fdc9ab4d 100644 --- a/src/Gateways/PosNetV1Pos.php +++ b/src/Gateways/PosNetV1Pos.php @@ -98,7 +98,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $order, PosInterface::MODEL_3D_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), diff --git a/src/Gateways/ToslaPos.php b/src/Gateways/ToslaPos.php index 7cb51ce1..d6fd572b 100644 --- a/src/Gateways/ToslaPos.php +++ b/src/Gateways/ToslaPos.php @@ -229,7 +229,8 @@ private function registerPayment(array $order, string $paymentModel, string $txT $order, $paymentModel ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), diff --git a/src/Gateways/VakifKatilimPos.php b/src/Gateways/VakifKatilimPos.php index 2ffa8b59..800effaf 100644 --- a/src/Gateways/VakifKatilimPos.php +++ b/src/Gateways/VakifKatilimPos.php @@ -136,7 +136,8 @@ public function make3DPayment(Request $request, array $order, string $txType, Cr $order, PosInterface::MODEL_3D_SECURE ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), @@ -209,7 +210,8 @@ private function sendEnrollmentRequest(KuveytPosAccount $kuveytPosAccount, array $order, $paymentModel ); - $this->eventDispatcher->dispatch($event); + /** @var RequestDataPreparedEvent $event */ + $event = $this->eventDispatcher->dispatch($event); if ($requestData !== $event->getRequestData()) { $this->logger->debug('Request data is changed via listeners', [ 'txType' => $event->getTxType(), diff --git a/tests/Unit/Gateways/AkbankPosTest.php b/tests/Unit/Gateways/AkbankPosTest.php index 179c2b39..c23c2b2e 100644 --- a/tests/Unit/Gateways/AkbankPosTest.php +++ b/tests/Unit/Gateways/AkbankPosTest.php @@ -3,7 +3,7 @@ * @license MIT */ -namespace Gateways; +namespace Mews\Pos\Tests\Unit\Gateways; use Mews\Pos\Client\HttpClient; use Mews\Pos\Crypt\CryptInterface; @@ -887,6 +887,8 @@ private function configureClientResponse( ?int $statusCode = null ): void { + $updatedRequestDataPreparedEvent = null; + $this->cryptMock->expects(self::once()) ->method('hashString') ->with($encodedRequestData, $this->account->getStoreKey()) @@ -894,7 +896,7 @@ private function configureClientResponse( $this->serializerMock->expects(self::once()) ->method('encode') - ->with($requestData, $txType) + ->with($this->logicalAnd($this->arrayHasKey('test-update-request-data-with-event')), $txType) ->willReturn($encodedRequestData); $this->serializerMock->expects(self::once()) @@ -918,11 +920,24 @@ private function configureClientResponse( $this->eventDispatcherMock->expects(self::once()) ->method('dispatch') - ->with($this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent - && get_class($this->pos) === $dispatchedEvent->getGatewayClass() - && $txType === $dispatchedEvent->getTxType() - && $requestData === $dispatchedEvent->getRequestData() - && $order === $dispatchedEvent->getOrder() - && $paymentModel === $dispatchedEvent->getPaymentModel())); + ->with($this->logicalAnd( + $this->isInstanceOf(RequestDataPreparedEvent::class), + $this->callback(function (RequestDataPreparedEvent $dispatchedEvent) use ($requestData, $txType, $order, $paymentModel, &$updatedRequestDataPreparedEvent) { + $updatedRequestDataPreparedEvent = $dispatchedEvent; + + return get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel(); + } + ))) + ->willReturnCallback(function () use (&$updatedRequestDataPreparedEvent) { + $updatedRequestData = $updatedRequestDataPreparedEvent->getRequestData(); + $updatedRequestData['test-update-request-data-with-event'] = true; + $updatedRequestDataPreparedEvent->setRequestData($updatedRequestData); + + return $updatedRequestDataPreparedEvent; + }); } } diff --git a/tests/Unit/Gateways/EstPosTest.php b/tests/Unit/Gateways/EstPosTest.php index d032f8dc..9e459ca0 100644 --- a/tests/Unit/Gateways/EstPosTest.php +++ b/tests/Unit/Gateways/EstPosTest.php @@ -701,9 +701,11 @@ private function configureClientResponse( string $paymentModel ): void { + $updatedRequestDataPreparedEvent = null; + $this->serializerMock->expects(self::once()) ->method('encode') - ->with($requestData, $txType) + ->with($this->logicalAnd($this->arrayHasKey('test-update-request-data-with-event')), $txType) ->willReturn($encodedRequestData); $this->serializerMock->expects(self::once()) @@ -722,11 +724,24 @@ private function configureClientResponse( $this->eventDispatcherMock->expects(self::once()) ->method('dispatch') - ->with($this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent - && get_class($this->pos) === $dispatchedEvent->getGatewayClass() - && $txType === $dispatchedEvent->getTxType() - && $requestData === $dispatchedEvent->getRequestData() - && $order === $dispatchedEvent->getOrder() - && $paymentModel === $dispatchedEvent->getPaymentModel())); + ->with($this->logicalAnd( + $this->isInstanceOf(RequestDataPreparedEvent::class), + $this->callback(function (RequestDataPreparedEvent $dispatchedEvent) use ($requestData, $txType, $order, $paymentModel, &$updatedRequestDataPreparedEvent) { + $updatedRequestDataPreparedEvent = $dispatchedEvent; + + return get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel(); + } + ))) + ->willReturnCallback(function () use (&$updatedRequestDataPreparedEvent) { + $updatedRequestData = $updatedRequestDataPreparedEvent->getRequestData(); + $updatedRequestData['test-update-request-data-with-event'] = true; + $updatedRequestDataPreparedEvent->setRequestData($updatedRequestData); + + return $updatedRequestDataPreparedEvent; + }); } } diff --git a/tests/Unit/Gateways/GarantiPosTest.php b/tests/Unit/Gateways/GarantiPosTest.php index 2d5532c8..9c5d4f7a 100644 --- a/tests/Unit/Gateways/GarantiPosTest.php +++ b/tests/Unit/Gateways/GarantiPosTest.php @@ -631,9 +631,11 @@ private function configureClientResponse( string $paymentModel ): void { + $updatedRequestDataPreparedEvent = null; + $this->serializerMock->expects(self::once()) ->method('encode') - ->with($requestData, $txType) + ->with($this->logicalAnd($this->arrayHasKey('test-update-request-data-with-event')), $txType) ->willReturn($encodedRequestData); $this->serializerMock->expects(self::once()) @@ -652,12 +654,25 @@ private function configureClientResponse( $this->eventDispatcherMock->expects(self::once()) ->method('dispatch') - ->with($this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent - && get_class($this->pos) === $dispatchedEvent->getGatewayClass() - && $txType === $dispatchedEvent->getTxType() - && $requestData === $dispatchedEvent->getRequestData() - && $order === $dispatchedEvent->getOrder() - && $paymentModel === $dispatchedEvent->getPaymentModel())); + ->with($this->logicalAnd( + $this->isInstanceOf(RequestDataPreparedEvent::class), + $this->callback(function (RequestDataPreparedEvent $dispatchedEvent) use ($requestData, $txType, $order, $paymentModel, &$updatedRequestDataPreparedEvent) { + $updatedRequestDataPreparedEvent = $dispatchedEvent; + + return get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel(); + } + ))) + ->willReturnCallback(function () use (&$updatedRequestDataPreparedEvent) { + $updatedRequestData = $updatedRequestDataPreparedEvent->getRequestData(); + $updatedRequestData['test-update-request-data-with-event'] = true; + $updatedRequestDataPreparedEvent->setRequestData($updatedRequestData); + + return $updatedRequestDataPreparedEvent; + }); } public static function historyRequestDataProvider(): array diff --git a/tests/Unit/Gateways/InterPosTest.php b/tests/Unit/Gateways/InterPosTest.php index 59226a72..45405d9b 100644 --- a/tests/Unit/Gateways/InterPosTest.php +++ b/tests/Unit/Gateways/InterPosTest.php @@ -621,9 +621,11 @@ private function configureClientResponse( string $paymentModel ): void { + $updatedRequestDataPreparedEvent = null; + $this->serializerMock->expects(self::once()) ->method('encode') - ->with($requestData, $txType) + ->with($this->logicalAnd($this->arrayHasKey('test-update-request-data-with-event')), $txType) ->willReturn($encodedRequestData); $this->serializerMock->expects(self::once()) @@ -642,11 +644,24 @@ private function configureClientResponse( $this->eventDispatcherMock->expects(self::once()) ->method('dispatch') - ->with($this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent - && get_class($this->pos) === $dispatchedEvent->getGatewayClass() - && $txType === $dispatchedEvent->getTxType() - && $requestData === $dispatchedEvent->getRequestData() - && $order === $dispatchedEvent->getOrder() - && $paymentModel === $dispatchedEvent->getPaymentModel())); + ->with($this->logicalAnd( + $this->isInstanceOf(RequestDataPreparedEvent::class), + $this->callback(function (RequestDataPreparedEvent $dispatchedEvent) use ($requestData, $txType, $order, $paymentModel, &$updatedRequestDataPreparedEvent) { + $updatedRequestDataPreparedEvent = $dispatchedEvent; + + return get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel(); + } + ))) + ->willReturnCallback(function () use (&$updatedRequestDataPreparedEvent) { + $updatedRequestData = $updatedRequestDataPreparedEvent->getRequestData(); + $updatedRequestData['test-update-request-data-with-event'] = true; + $updatedRequestDataPreparedEvent->setRequestData($updatedRequestData); + + return $updatedRequestDataPreparedEvent; + }); } } diff --git a/tests/Unit/Gateways/KuveytPosTest.php b/tests/Unit/Gateways/KuveytPosTest.php index 0d433790..3aa3c70e 100644 --- a/tests/Unit/Gateways/KuveytPosTest.php +++ b/tests/Unit/Gateways/KuveytPosTest.php @@ -265,6 +265,7 @@ public function testMake3DPayment( $create3DPaymentRequestData = [ 'create3DPaymentRequestData', ]; + $encodedRequestData = 'request-body'; if ($is3DSuccess) { @@ -277,7 +278,7 @@ public function testMake3DPayment( 'response-body', 'https://boatest.kuveytturk.com.tr/boa.virtualpos.services/Home/ThreeDModelProvisionGate', [ - 'body' => 'request-body', + 'body' => $encodedRequestData, 'headers' => [ 'Content-Type' => 'text/xml; charset=UTF-8', ], @@ -286,17 +287,30 @@ public function testMake3DPayment( $paymentModel = PosInterface::MODEL_3D_SECURE; $this->eventDispatcherMock->expects(self::once()) ->method('dispatch') - ->with($this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent - && get_class($this->pos) === $dispatchedEvent->getGatewayClass() - && $txType === $dispatchedEvent->getTxType() - && $create3DPaymentRequestData === $dispatchedEvent->getRequestData() - && $order === $dispatchedEvent->getOrder() - && $paymentModel === $dispatchedEvent->getPaymentModel())); + ->with($this->logicalAnd( + $this->isInstanceOf(RequestDataPreparedEvent::class), + $this->callback(function (RequestDataPreparedEvent $dispatchedEvent) use ($create3DPaymentRequestData, $txType, $order, $paymentModel, &$updatedRequestDataPreparedEvent) { + $updatedRequestDataPreparedEvent = $dispatchedEvent; + + return get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $create3DPaymentRequestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel(); + } + ))) + ->willReturnCallback(function() use (&$updatedRequestDataPreparedEvent) { + $updatedRequestData = $updatedRequestDataPreparedEvent->getRequestData(); + $updatedRequestData['test-update-request-data-with-event'] = true; + $updatedRequestDataPreparedEvent->setRequestData($updatedRequestData); + + return $updatedRequestDataPreparedEvent; + }); $this->serializerMock->expects(self::once()) ->method('encode') - ->with($create3DPaymentRequestData, $txType) - ->willReturn('request-body'); + ->with($this->logicalAnd($this->arrayHasKey('test-update-request-data-with-event')), $txType) + ->willReturn($encodedRequestData); $this->serializerMock->expects(self::exactly(2)) ->method('decode') @@ -533,9 +547,11 @@ private function configureClientResponse( ?int $statusCode = null ): void { + $updatedRequestDataPreparedEvent = null; + $this->serializerMock->expects(self::once()) ->method('encode') - ->with($requestData, $txType) + ->with($this->logicalAnd($this->arrayHasKey('test-update-request-data-with-event')), $txType) ->willReturn($encodedRequestData); $this->serializerMock->expects(self::once()) @@ -558,11 +574,24 @@ private function configureClientResponse( $this->eventDispatcherMock->expects(self::once()) ->method('dispatch') - ->with($this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent - && get_class($this->pos) === $dispatchedEvent->getGatewayClass() - && $txType === $dispatchedEvent->getTxType() - && $requestData === $dispatchedEvent->getRequestData() - && $order === $dispatchedEvent->getOrder() - && $paymentModel === $dispatchedEvent->getPaymentModel())); + ->with($this->logicalAnd( + $this->isInstanceOf(RequestDataPreparedEvent::class), + $this->callback(function (RequestDataPreparedEvent $dispatchedEvent) use ($requestData, $txType, $order, $paymentModel, &$updatedRequestDataPreparedEvent) { + $updatedRequestDataPreparedEvent = $dispatchedEvent; + + return get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel(); + } + ))) + ->willReturnCallback(function () use (&$updatedRequestDataPreparedEvent) { + $updatedRequestData = $updatedRequestDataPreparedEvent->getRequestData(); + $updatedRequestData['test-update-request-data-with-event'] = true; + $updatedRequestDataPreparedEvent->setRequestData($updatedRequestData); + + return $updatedRequestDataPreparedEvent; + }); } } diff --git a/tests/Unit/Gateways/PayFlexCPV4PosTest.php b/tests/Unit/Gateways/PayFlexCPV4PosTest.php index f4939630..b039bcb8 100644 --- a/tests/Unit/Gateways/PayFlexCPV4PosTest.php +++ b/tests/Unit/Gateways/PayFlexCPV4PosTest.php @@ -582,13 +582,16 @@ private function configureClientResponse( string $paymentModel ): void { + $updatedRequestDataPreparedEvent = null; + if ($requestData === $encodedRequestData) { $this->serializerMock->expects(self::never()) ->method('encode'); + $encodedRequestData['test-update-request-data-with-event'] = true; } else { $this->serializerMock->expects(self::once()) ->method('encode') - ->with($requestData, $txType) + ->with($this->logicalAnd($this->arrayHasKey('test-update-request-data-with-event')), $txType) ->willReturn($encodedRequestData); } @@ -606,11 +609,24 @@ private function configureClientResponse( $this->eventDispatcherMock->expects(self::once()) ->method('dispatch') - ->with($this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent - && get_class($this->pos) === $dispatchedEvent->getGatewayClass() - && $txType === $dispatchedEvent->getTxType() - && $requestData === $dispatchedEvent->getRequestData() - && $order === $dispatchedEvent->getOrder() - && $paymentModel === $dispatchedEvent->getPaymentModel())); + ->with($this->logicalAnd( + $this->isInstanceOf(RequestDataPreparedEvent::class), + $this->callback(function (RequestDataPreparedEvent $dispatchedEvent) use ($requestData, $txType, $order, $paymentModel, &$updatedRequestDataPreparedEvent) { + $updatedRequestDataPreparedEvent = $dispatchedEvent; + + return get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel(); + } + ))) + ->willReturnCallback(function () use (&$updatedRequestDataPreparedEvent) { + $updatedRequestData = $updatedRequestDataPreparedEvent->getRequestData(); + $updatedRequestData['test-update-request-data-with-event'] = true; + $updatedRequestDataPreparedEvent->setRequestData($updatedRequestData); + + return $updatedRequestDataPreparedEvent; + }); } } diff --git a/tests/Unit/Gateways/PayFlexV4PosTest.php b/tests/Unit/Gateways/PayFlexV4PosTest.php index 232ef759..d11c4844 100644 --- a/tests/Unit/Gateways/PayFlexV4PosTest.php +++ b/tests/Unit/Gateways/PayFlexV4PosTest.php @@ -632,13 +632,16 @@ private function configureClientResponse( string $paymentModel ): void { + $updatedRequestDataPreparedEvent = null; + if ($requestData === $encodedRequestData) { $this->serializerMock->expects(self::never()) ->method('encode'); + $encodedRequestData['test-update-request-data-with-event'] = true; } else { $this->serializerMock->expects(self::once()) ->method('encode') - ->with($requestData, $txType) + ->with($this->logicalAnd($this->arrayHasKey('test-update-request-data-with-event')), $txType) ->willReturn($encodedRequestData); } @@ -658,11 +661,24 @@ private function configureClientResponse( $this->eventDispatcherMock->expects(self::once()) ->method('dispatch') - ->with($this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent - && get_class($this->pos) === $dispatchedEvent->getGatewayClass() - && $txType === $dispatchedEvent->getTxType() - && $requestData === $dispatchedEvent->getRequestData() - && $order === $dispatchedEvent->getOrder() - && $paymentModel === $dispatchedEvent->getPaymentModel())); + ->with($this->logicalAnd( + $this->isInstanceOf(RequestDataPreparedEvent::class), + $this->callback(function (RequestDataPreparedEvent $dispatchedEvent) use ($requestData, $txType, $order, $paymentModel, &$updatedRequestDataPreparedEvent) { + $updatedRequestDataPreparedEvent = $dispatchedEvent; + + return get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel(); + } + ))) + ->willReturnCallback(function() use (&$updatedRequestDataPreparedEvent) { + $updatedRequestData = $updatedRequestDataPreparedEvent->getRequestData(); + $updatedRequestData['test-update-request-data-with-event'] = true; + $updatedRequestDataPreparedEvent->setRequestData($updatedRequestData); + + return $updatedRequestDataPreparedEvent; + }); } } diff --git a/tests/Unit/Gateways/PayForTest.php b/tests/Unit/Gateways/PayForTest.php index 04f71d13..ab1da4fe 100644 --- a/tests/Unit/Gateways/PayForTest.php +++ b/tests/Unit/Gateways/PayForTest.php @@ -686,9 +686,11 @@ private function configureClientResponse( string $paymentModel ): void { + $updatedRequestDataPreparedEvent = null; + $this->serializerMock->expects(self::once()) ->method('encode') - ->with($requestData, $txType) + ->with($this->logicalAnd($this->arrayHasKey('test-update-request-data-with-event')), $txType) ->willReturn($encodedRequestData); $this->serializerMock->expects(self::once()) @@ -710,11 +712,23 @@ private function configureClientResponse( $this->eventDispatcherMock->expects(self::once()) ->method('dispatch') - ->with($this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent - && get_class($this->pos) === $dispatchedEvent->getGatewayClass() - && $txType === $dispatchedEvent->getTxType() - && $requestData === $dispatchedEvent->getRequestData() - && $order === $dispatchedEvent->getOrder() - && $paymentModel === $dispatchedEvent->getPaymentModel())); + ->with($this->logicalAnd( + $this->isInstanceOf(RequestDataPreparedEvent::class), + $this->callback(function (RequestDataPreparedEvent $dispatchedEvent) use ($requestData, $txType, $order, $paymentModel, &$updatedRequestDataPreparedEvent) { + $updatedRequestDataPreparedEvent = $dispatchedEvent; + + return get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel(); + }))) + ->willReturnCallback(function () use (&$updatedRequestDataPreparedEvent) { + $updatedRequestData = $updatedRequestDataPreparedEvent->getRequestData(); + $updatedRequestData['test-update-request-data-with-event'] = true; + $updatedRequestDataPreparedEvent->setRequestData($updatedRequestData); + + return $updatedRequestDataPreparedEvent; + }); } } diff --git a/tests/Unit/Gateways/PosNetTest.php b/tests/Unit/Gateways/PosNetTest.php index 8989393b..4bcddf26 100644 --- a/tests/Unit/Gateways/PosNetTest.php +++ b/tests/Unit/Gateways/PosNetTest.php @@ -232,20 +232,36 @@ public function testMake3DPayment( ->with($this->account, $order, $txType, $request->request->all()) ->willReturn($create3DPaymentRequestData); - $this->serializerMock->expects(self::exactly(2)) + + $matcher = self::exactly(2); + $updatedRequestDataPreparedEvent1 = null; + $updatedRequestDataPreparedEvent2 = null; + + $this->serializerMock->expects($matcher) ->method('encode') - ->willReturnMap([ - [ - $resolveMerchantRequestData, - $txType, - 'resolveMerchantRequestData-body', - ], - [ - $create3DPaymentRequestData, - $txType, - 'payment-request-body', - ], - ]); + ->with($this->callback(function ($requestData) use ($matcher, &$updatedRequestDataPreparedEvent1, &$updatedRequestDataPreparedEvent2) { + if ($matcher->getInvocationCount() === 1) { + return $updatedRequestDataPreparedEvent1->getRequestData() === $requestData; + } + + if ($matcher->getInvocationCount() === 2) { + return $updatedRequestDataPreparedEvent2->getRequestData() === $requestData; + } + + return true; + }), $this->callback(function ($txT) use ($txType) { + return $txT === $txType; + })) + ->willReturnCallback(function () use ($matcher) { + if ($matcher->getInvocationCount() === 1) { + return 'resolveMerchantRequestData-body'; + } + if ($matcher->getInvocationCount() === 2) { + return 'payment-request-body'; + } + + return null; + }); $this->serializerMock->expects(self::exactly(2)) ->method('decode') @@ -289,23 +305,62 @@ public function testMake3DPayment( ); $paymentModel = PosInterface::MODEL_3D_SECURE; - $this->eventDispatcherMock->expects(self::exactly(2)) + + $matcher2 = self::exactly(2); + $this->eventDispatcherMock->expects($matcher2) ->method('dispatch') - // could not find another way expect using deprecated withConsecutive() function - ->withConsecutive( - [$this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent - && get_class($this->pos) === $dispatchedEvent->getGatewayClass() - && $txType === $dispatchedEvent->getTxType() - && $resolveMerchantRequestData === $dispatchedEvent->getRequestData() - && $order === $dispatchedEvent->getOrder() - && $paymentModel === $dispatchedEvent->getPaymentModel())], - [$this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent - && get_class($this->pos) === $dispatchedEvent->getGatewayClass() - && $txType === $dispatchedEvent->getTxType() - && $create3DPaymentRequestData === $dispatchedEvent->getRequestData() - && $order === $dispatchedEvent->getOrder() - && $paymentModel === $dispatchedEvent->getPaymentModel())] - ); + ->with($this->logicalAnd( + $this->isInstanceOf(RequestDataPreparedEvent::class), + $this->callback(function ($dispatchedEvent) use ( + $resolveMerchantRequestData, + $create3DPaymentRequestData, + $txType, + $order, + $paymentModel, + $matcher2, + &$updatedRequestDataPreparedEvent1, + &$updatedRequestDataPreparedEvent2 + ) { + if ($matcher2->getInvocationCount() === 1) { + $updatedRequestDataPreparedEvent1 = $dispatchedEvent; + + return get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $resolveMerchantRequestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel(); + } + + if ($matcher2->getInvocationCount() === 2) { + $updatedRequestDataPreparedEvent2 = $dispatchedEvent; + + return get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $create3DPaymentRequestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel(); + } + + return false; + }))) + ->willReturnCallback(function () use ($matcher2, &$updatedRequestDataPreparedEvent1, &$updatedRequestDataPreparedEvent2) { + if ($matcher2->getInvocationCount() === 1) { + $updatedRequestData = $updatedRequestDataPreparedEvent1->getRequestData(); + $updatedRequestData['test-update-request-data-with-event'] = true; + $updatedRequestDataPreparedEvent1->setRequestData($updatedRequestData); + + return $updatedRequestDataPreparedEvent1; + } + if ($matcher2->getInvocationCount() === 2) { + $updatedRequestData = $updatedRequestDataPreparedEvent2->getRequestData(); + $updatedRequestData['test-update-request-data-with-event'] = true; + $updatedRequestDataPreparedEvent2->setRequestData($updatedRequestData); + + return $updatedRequestDataPreparedEvent2; + } + + return false; + }); $this->responseMapperMock->expects(self::once()) ->method('map3DPaymentData') @@ -658,9 +713,11 @@ private function configureClientResponse( string $paymentModel ): void { + $updatedRequestDataPreparedEvent = null; + $this->serializerMock->expects(self::once()) ->method('encode') - ->with($requestData, $txType) + ->with($this->logicalAnd($this->arrayHasKey('test-update-request-data-with-event')), $txType) ->willReturn($encodedRequestData); $this->serializerMock->expects(self::once()) @@ -682,11 +739,24 @@ private function configureClientResponse( $this->eventDispatcherMock->expects(self::once()) ->method('dispatch') - ->with($this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent - && get_class($this->pos) === $dispatchedEvent->getGatewayClass() - && $txType === $dispatchedEvent->getTxType() - && $requestData === $dispatchedEvent->getRequestData() - && $order === $dispatchedEvent->getOrder() - && $paymentModel === $dispatchedEvent->getPaymentModel())); + ->with($this->logicalAnd( + $this->isInstanceOf(RequestDataPreparedEvent::class), + $this->callback(function (RequestDataPreparedEvent $dispatchedEvent) use ($requestData, $txType, $order, $paymentModel, &$updatedRequestDataPreparedEvent) { + $updatedRequestDataPreparedEvent = $dispatchedEvent; + + return get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel(); + } + ))) + ->willReturnCallback(function () use (&$updatedRequestDataPreparedEvent) { + $updatedRequestData = $updatedRequestDataPreparedEvent->getRequestData(); + $updatedRequestData['test-update-request-data-with-event'] = true; + $updatedRequestDataPreparedEvent->setRequestData($updatedRequestData); + + return $updatedRequestDataPreparedEvent; + }); } } diff --git a/tests/Unit/Gateways/PosNetV1PosTest.php b/tests/Unit/Gateways/PosNetV1PosTest.php index f656c860..19a0bcd8 100644 --- a/tests/Unit/Gateways/PosNetV1PosTest.php +++ b/tests/Unit/Gateways/PosNetV1PosTest.php @@ -614,9 +614,11 @@ private function configureClientResponse( string $paymentModel ): void { + $updatedRequestDataPreparedEvent = null; + $this->serializerMock->expects(self::once()) ->method('encode') - ->with($requestData, $txType) + ->with($this->logicalAnd($this->arrayHasKey('test-update-request-data-with-event')), $txType) ->willReturn($encodedRequestData); $this->serializerMock->expects(self::once()) @@ -638,11 +640,24 @@ private function configureClientResponse( $this->eventDispatcherMock->expects(self::once()) ->method('dispatch') - ->with($this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent - && get_class($this->pos) === $dispatchedEvent->getGatewayClass() - && $txType === $dispatchedEvent->getTxType() - && $requestData === $dispatchedEvent->getRequestData() - && $order === $dispatchedEvent->getOrder() - && $paymentModel === $dispatchedEvent->getPaymentModel())); + ->with($this->logicalAnd( + $this->isInstanceOf(RequestDataPreparedEvent::class), + $this->callback(function (RequestDataPreparedEvent $dispatchedEvent) use ($requestData, $txType, $order, $paymentModel, &$updatedRequestDataPreparedEvent) { + $updatedRequestDataPreparedEvent = $dispatchedEvent; + + return get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel(); + } + ))) + ->willReturnCallback(function() use (&$updatedRequestDataPreparedEvent) { + $updatedRequestData = $updatedRequestDataPreparedEvent->getRequestData(); + $updatedRequestData['test-update-request-data-with-event'] = true; + $updatedRequestDataPreparedEvent->setRequestData($updatedRequestData); + + return $updatedRequestDataPreparedEvent; + }); } } diff --git a/tests/Unit/Gateways/ToslaPosTest.php b/tests/Unit/Gateways/ToslaPosTest.php index 90cf1a80..847ee0b1 100644 --- a/tests/Unit/Gateways/ToslaPosTest.php +++ b/tests/Unit/Gateways/ToslaPosTest.php @@ -881,10 +881,13 @@ private function configureClientResponse( string $paymentModel ): void { + $updatedRequestDataPreparedEvent = null; + $this->serializerMock->expects(self::once()) ->method('encode') - ->with($requestData, $txType) + ->with($this->logicalAnd($this->arrayHasKey('test-update-request-data-with-event')), $txType) ->willReturn($encodedRequestData); + $this->serializerMock->expects(self::once()) ->method('decode') ->with($responseContent, $txType) @@ -904,11 +907,24 @@ private function configureClientResponse( $this->eventDispatcherMock->expects(self::once()) ->method('dispatch') - ->with($this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent - && get_class($this->pos) === $dispatchedEvent->getGatewayClass() - && $txType === $dispatchedEvent->getTxType() - && $requestData === $dispatchedEvent->getRequestData() - && $order === $dispatchedEvent->getOrder() - && $paymentModel === $dispatchedEvent->getPaymentModel())); + ->with($this->logicalAnd( + $this->isInstanceOf(RequestDataPreparedEvent::class), + $this->callback(function (RequestDataPreparedEvent $dispatchedEvent) use ($requestData, $txType, $order, $paymentModel, &$updatedRequestDataPreparedEvent) { + $updatedRequestDataPreparedEvent = $dispatchedEvent; + + return get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel(); + } + ))) + ->willReturnCallback(function () use (&$updatedRequestDataPreparedEvent) { + $updatedRequestData = $updatedRequestDataPreparedEvent->getRequestData(); + $updatedRequestData['test-update-request-data-with-event'] = true; + $updatedRequestDataPreparedEvent->setRequestData($updatedRequestData); + + return $updatedRequestDataPreparedEvent; + }); } } diff --git a/tests/Unit/Gateways/VakifKatilimTest.php b/tests/Unit/Gateways/VakifKatilimTest.php index 0c8f1461..adc16dad 100644 --- a/tests/Unit/Gateways/VakifKatilimTest.php +++ b/tests/Unit/Gateways/VakifKatilimTest.php @@ -3,7 +3,7 @@ * @license MIT */ -namespace Gateways; +namespace Mews\Pos\Tests\Unit\Gateways; use Mews\Pos\Client\HttpClient; use Mews\Pos\Crypt\CryptInterface; @@ -832,10 +832,13 @@ private function configureClientResponse( string $paymentModel ): void { + $updatedRequestDataPreparedEvent = null; + $this->serializerMock->expects(self::once()) ->method('encode') - ->with($requestData, $txType) + ->with($this->logicalAnd($this->arrayHasKey('test-update-request-data-with-event')), $txType) ->willReturn($encodedRequestData); + $this->serializerMock->expects(self::once()) ->method('decode') ->with($responseContent, $txType) @@ -855,11 +858,24 @@ private function configureClientResponse( $this->eventDispatcherMock->expects(self::once()) ->method('dispatch') - ->with($this->callback(fn($dispatchedEvent): bool => $dispatchedEvent instanceof RequestDataPreparedEvent - && get_class($this->pos) === $dispatchedEvent->getGatewayClass() - && $txType === $dispatchedEvent->getTxType() - && $requestData === $dispatchedEvent->getRequestData() - && $order === $dispatchedEvent->getOrder() - && $paymentModel === $dispatchedEvent->getPaymentModel())); + ->with($this->logicalAnd( + $this->isInstanceOf(RequestDataPreparedEvent::class), + $this->callback(function (RequestDataPreparedEvent $dispatchedEvent) use ($requestData, $txType, $order, $paymentModel, &$updatedRequestDataPreparedEvent) { + $updatedRequestDataPreparedEvent = $dispatchedEvent; + + return get_class($this->pos) === $dispatchedEvent->getGatewayClass() + && $txType === $dispatchedEvent->getTxType() + && $requestData === $dispatchedEvent->getRequestData() + && $order === $dispatchedEvent->getOrder() + && $paymentModel === $dispatchedEvent->getPaymentModel(); + } + ))) + ->willReturnCallback(function () use (&$updatedRequestDataPreparedEvent) { + $updatedRequestData = $updatedRequestDataPreparedEvent->getRequestData(); + $updatedRequestData['test-update-request-data-with-event'] = true; + $updatedRequestDataPreparedEvent->setRequestData($updatedRequestData); + + return $updatedRequestDataPreparedEvent; + }); } } diff --git a/tests/Unit/HttpClientTestTrait.php b/tests/Unit/HttpClientTestTrait.php index e50d0773..46c0845b 100644 --- a/tests/Unit/HttpClientTestTrait.php +++ b/tests/Unit/HttpClientTestTrait.php @@ -6,11 +6,15 @@ namespace Mews\Pos\Tests\Unit; use Mews\Pos\Client\HttpClient; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; trait HttpClientTestTrait { + /** + * @param HttpClient|MockObject $httpClient + */ private function prepareClient( HttpClient $httpClient, string $responseContent, @@ -26,6 +30,9 @@ private function prepareClient( ->willReturn($responseMock); } + /** + * @param HttpClient|MockObject $httpClient + */ private function prepareHttpClientRequestMulti( HttpClient $httpClient, array $responseContents,