Skip to content

Commit

Permalink
Updating unit test for asserting return from private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
khushboo-singhvi committed Oct 21, 2024
1 parent ad67f20 commit f28a408
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Helper/PaymentResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
155 changes: 110 additions & 45 deletions Test/Unit/Helper/PaymentResponseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,7 @@ private static function handlePaymentsDetailsActionCancelledOrRefusedProvider():
{
return [
['resultCode' => PaymentResponseHandler::REFUSED],
['resultCode' => PaymentResponseHandler::CANCELLED],
['resultCode' => PaymentResponseHandler::CANCELLED, 'hasGiftCard' => true]
['resultCode' => PaymentResponseHandler::CANCELLED]
];
}

Expand All @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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);
}

}

0 comments on commit f28a408

Please sign in to comment.