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 18, 2024
1 parent 85f37c3 commit ec5f014
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions Test/Unit/Helper/PaymentResponseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
namespace Adyen\Payment\Test\Unit\Helper;

use Adyen\Client;
use Adyen\Payment\Helper\PaymentResponseHandler;
use Adyen\Payment\Logger\AdyenLogger;
use Adyen\Payment\Helper\Vault;
Expand Down Expand Up @@ -69,18 +70,9 @@ protected function setUp(): void

$this->paymentResponseCollectionFactoryMock = $this->createGeneratedMock(CollectionFactory::class, ['create']);
$this->paymentResponseCollectionFactoryMock->method('create')->willReturn($this->paymentResponseMockForFactory);
// Mock addFieldToFilter behavior
$this->paymentResponseMockForFactory->method('addFieldToFilter')
->willReturnSelf();

// Mock getSize to return a desired value
$this->paymentResponseMockForFactory->method('getSize')
->willReturn(1); // Adjust based on your test case logic

// Mock getData to return some dummy data
$this->paymentResponseMockForFactory->method('getData')
->willReturn([['field' => 'value']]);

$this->paymentResponseMockForFactory->expects($this->any())
->method('addFieldToFilter')
->willReturn($this->paymentResponseMockForFactory);
$orderHistory = $this->createMock(History::class);
$orderHistory->method('setStatus')->willReturnSelf();
$orderHistory->method('setComment')->willReturnSelf();
Expand Down Expand Up @@ -548,4 +540,48 @@ public function testOrderStatusUpdateWhenResponseIsValid()

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

public function testHasActiveGiftCardPayments()
{

// Mock the addFieldToFilter method to return the collection itself for chaining


// Mock getSize to simulate an authorized gift card payment
$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);
}
}

0 comments on commit ec5f014

Please sign in to comment.