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 22, 2024
1 parent f28a408 commit 7214dd2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 119 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
public function hasActiveGiftCardPayments($merchantReference): array|string|null
private function hasActiveGiftCardPayments($merchantReference): array|string|null
{
$paymentResponseCollection = $this->paymentResponseCollectionFactory->create()
->addFieldToFilter('merchant_reference', $merchantReference)
Expand Down
146 changes: 28 additions & 118 deletions Test/Unit/Helper/PaymentResponseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ protected function setUp(): void
$this->paymentResponseMockForFactory = $this->createMock(Collection::class);
// Mock PaymentResponseCollectionFactory to return the mocked collection

$this->paymentResponseCollectionFactoryMock = $this->createGeneratedMock(CollectionFactory::class, ['create']);
$this->paymentResponseCollectionFactoryMock->method('create')->willReturn($this->paymentResponseMockForFactory);
$this->paymentResponseCollectionFactoryMock = $this->createMock(CollectionFactory::class);
$this->paymentResponseMockForFactory->expects($this->any())
->method('addFieldToFilter')
->willReturn($this->paymentResponseMockForFactory);
Expand Down Expand Up @@ -411,10 +410,33 @@ public function testHandlePaymentsDetailsResponseCancelOrRefused($resultCode)
'actionData' => 'actionValue'
]
];
$giftcardData = "{['merchant_reference' => '12345', 'result_code' => 'Authorised']";
$this->paymentResponseHandler->method('hasActiveGiftCardPayments')
->with('00123456')
->willReturn($giftcardData);
$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);

$this->adyenLoggerMock->expects($this->atLeastOnce())->method('addAdyenResult');

Expand Down Expand Up @@ -544,116 +566,4 @@ public function testOrderStatusUpdateWhenResponseIsValid()

$this->paymentResponseHandler->handlePaymentsDetailsResponse($paymentsDetailsResponse, $this->orderMock);
}

// 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()
{
$merchantReference = 'test_merchant_reference';
$storeId = 1;

// 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'
]
])
]
];

$giftcardData = $this->paymentResponseHandler->hasActiveGiftCardPayments('');
if(empty($giftcardData))
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid giftcard response data');
}

// 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,
];

$result = $this->paymentResponseHandler->handlePaymentsDetailsResponse($paymentsDetailsResponse, $orderMock);

// Assert the result is false after canceling
$this->assertFalse($result);
}

}

0 comments on commit 7214dd2

Please sign in to comment.