From f28a408fd0c31af7b779c0d5e31f38357fdf76ec Mon Sep 17 00:00:00 2001 From: khushboos Date: Mon, 21 Oct 2024 10:54:09 +0200 Subject: [PATCH] Updating unit test for asserting return from private methods --- Helper/PaymentResponseHandler.php | 2 +- .../Helper/PaymentResponseHandlerTest.php | 155 +++++++++++++----- 2 files changed, 111 insertions(+), 46 deletions(-) diff --git a/Helper/PaymentResponseHandler.php b/Helper/PaymentResponseHandler.php index 81963ab05..51ec1bbc4 100644 --- a/Helper/PaymentResponseHandler.php +++ b/Helper/PaymentResponseHandler.php @@ -409,7 +409,7 @@ private function isValidMerchantReference(array $paymentsDetailsResponse, OrderI } // Method to check for existing Gift Card payments - private function hasActiveGiftCardPayments($merchantReference) + public function hasActiveGiftCardPayments($merchantReference): array|string|null { $paymentResponseCollection = $this->paymentResponseCollectionFactory->create() ->addFieldToFilter('merchant_reference', $merchantReference) diff --git a/Test/Unit/Helper/PaymentResponseHandlerTest.php b/Test/Unit/Helper/PaymentResponseHandlerTest.php index d66293cce..01a6b8eb2 100644 --- a/Test/Unit/Helper/PaymentResponseHandlerTest.php +++ b/Test/Unit/Helper/PaymentResponseHandlerTest.php @@ -387,8 +387,7 @@ private static function handlePaymentsDetailsActionCancelledOrRefusedProvider(): { return [ ['resultCode' => PaymentResponseHandler::REFUSED], - ['resultCode' => PaymentResponseHandler::CANCELLED], - ['resultCode' => PaymentResponseHandler::CANCELLED, 'hasGiftCard' => true] + ['resultCode' => PaymentResponseHandler::CANCELLED] ]; } @@ -399,7 +398,7 @@ private static function handlePaymentsDetailsActionCancelledOrRefusedProvider(): * @throws NoSuchEntityException * @dataProvider handlePaymentsDetailsActionCancelledOrRefusedProvider */ - public function testHandlePaymentsDetailsResponseCancelOrRefused($resultCode, $hasGiftCard = false) + public function testHandlePaymentsDetailsResponseCancelOrRefused($resultCode) { $paymentsDetailsResponse = [ 'resultCode' => $resultCode, @@ -412,17 +411,11 @@ public function testHandlePaymentsDetailsResponseCancelOrRefused($resultCode, $h 'actionData' => 'actionValue' ] ]; + $giftcardData = "{['merchant_reference' => '12345', 'result_code' => 'Authorised']"; + $this->paymentResponseHandler->method('hasActiveGiftCardPayments') + ->with('00123456') + ->willReturn($giftcardData); - if ($hasGiftCard) { - $giftcardData = $this->testHasActiveGiftCardPayments(); - // Mock the dataHelper and service to simulate cancellation call - $this->dataHelperMock->expects($this->once())->method('initializeAdyenClient'); - $this->dataHelperMock->expects($this->once())->method('initializeOrdersApi'); - $this->adyenLoggerMock->expects($this->once())->method('error')->with( - 'Error canceling partial payments', - $this->anything() - ); - } $this->adyenLoggerMock->expects($this->atLeastOnce())->method('addAdyenResult'); $result = $this->paymentResponseHandler->handlePaymentsDetailsResponse( @@ -552,43 +545,115 @@ public function testOrderStatusUpdateWhenResponseIsValid() $this->paymentResponseHandler->handlePaymentsDetailsResponse($paymentsDetailsResponse, $this->orderMock); } - public function testHasActiveGiftCardPayments() +// public function testHasActiveGiftCardPayments() +// { +// $this->paymentResponseMockForFactory->expects($this->any()) +// ->method('getSize') +// ->willReturn(1); // Simulate there is at least one record +// +// // Mock getData to return the desired array of data from the database +// $this->paymentResponseMockForFactory->expects($this->any()) +// ->method('getData') +// ->willReturn([ +// ['merchant_reference' => '12345', 'result_code' => 'Authorised'] +// ]); +// +// $this->paymentResponseCollectionFactoryMock->expects($this->any()) +// ->method('create') +// ->willReturn($this->paymentResponseMockForFactory); +// +// // Create an instance of the class that has the private method +// $class = new \ReflectionClass(PaymentResponseHandler::class); +// $instance = $class->newInstanceWithoutConstructor(); +// +// // Inject the mocked factory into the instance if necessary +// $property = $class->getProperty('paymentResponseCollectionFactory'); +// $property->setAccessible(true); +// $property->setValue($instance, $this->paymentResponseCollectionFactoryMock); +// +// // Use Reflection to access the private method +// $method = $class->getMethod('hasActiveGiftCardPayments'); +// $method->setAccessible(true); +// +// // Call the private method with the required parameters +// $result = $method->invoke($instance, '12345'); +// +// // Assert the expected result +// $this->assertEquals([ +// ['merchant_reference' => '12345', 'result_code' => 'Authorised'] +// ], $result); +// return $result; +// } + + public function testCancelledScenarioWithActiveGiftCardPayments() { - $this->paymentResponseMockForFactory->expects($this->any()) - ->method('getSize') - ->willReturn(1); // Simulate there is at least one record - - // Mock getData to return the desired array of data from the database - $this->paymentResponseMockForFactory->expects($this->any()) - ->method('getData') - ->willReturn([ - ['merchant_reference' => '12345', 'result_code' => 'Authorised'] - ]); + $merchantReference = 'test_merchant_reference'; + $storeId = 1; - $this->paymentResponseCollectionFactoryMock->expects($this->any()) - ->method('create') - ->willReturn($this->paymentResponseMockForFactory); - - // Create an instance of the class that has the private method - $class = new \ReflectionClass(PaymentResponseHandler::class); - $instance = $class->newInstanceWithoutConstructor(); + // Mock the order object + $orderMock = $this->createMock(\Magento\Sales\Model\Order::class); + $orderMock->expects($this->once())->method('getStoreId')->willReturn($storeId); + $orderMock->expects($this->once())->method('canCancel')->willReturn(true); // Order can be canceled + $orderMock->expects($this->once())->method('setActionFlag')->with(\Magento\Sales\Model\Order::ACTION_FLAG_CANCEL, true); + + // Mock the payment response collection to simulate existing gift card payments + $giftCardDetails = [ + [ + 'response' => json_encode([ + 'order' => [ + 'orderData' => 'test_order_data', + 'pspReference' => 'test_psp_reference' + ] + ]) + ] + ]; - // Inject the mocked factory into the instance if necessary - $property = $class->getProperty('paymentResponseCollectionFactory'); - $property->setAccessible(true); - $property->setValue($instance, $this->paymentResponseCollectionFactoryMock); + $giftcardData = $this->paymentResponseHandler->hasActiveGiftCardPayments(''); + if(empty($giftcardData)) + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid giftcard response data'); + } - // Use Reflection to access the private method - $method = $class->getMethod('hasActiveGiftCardPayments'); - $method->setAccessible(true); + // Mock the Adyen client and Orders API + $clientMock = $this->createMock(\Adyen\Client::class); + $ordersApiMock = $this->createMock(\Adyen\Service\Orders::class); + + $this->dataHelperMock->expects($this->once()) + ->method('initializeAdyenClient') + ->with($storeId) + ->willReturn($clientMock); + + $this->dataHelperMock->expects($this->once()) + ->method('initializeOrdersApi') + ->with($clientMock) + ->willReturn($ordersApiMock); + + // Mock the cancelOrder API call and its response + $cancelOrderRequestMock = $this->createMock(\Adyen\Service\ResourceModel\Order\Cancel::class); + $ordersApiMock->expects($this->once()) + ->method('cancelOrder') + ->with($this->isInstanceOf(\Adyen\Service\ResourceModel\Order\CancelOrderRequest::class)) + ->willReturn($cancelOrderRequestMock); + + $cancelOrderRequestMock->expects($this->once()) + ->method('toArray') + ->willReturn(['resultCode' => 'Cancelled']); + + // Mock the dataHelper's cancelOrder method + $this->dataHelperMock->expects($this->once()) + ->method('cancelOrder') + ->with($orderMock); + + // Run the actual method under test + $paymentsDetailsResponse = [ + 'merchantReference' => $merchantReference, + ]; - // Call the private method with the required parameters - $result = $method->invoke($instance, '12345'); + $result = $this->paymentResponseHandler->handlePaymentsDetailsResponse($paymentsDetailsResponse, $orderMock); - // Assert the expected result - $this->assertEquals([ - ['merchant_reference' => '12345', 'result_code' => 'Authorised'] - ], $result); - return $result; + // Assert the result is false after canceling + $this->assertFalse($result); } + }