Skip to content

Commit

Permalink
#36: Unit tests: Reduce CreditCardTest.php code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarjan-onestic committed May 15, 2024
1 parent 5fd234c commit 5aa7a72
Showing 1 changed file with 65 additions and 70 deletions.
135 changes: 65 additions & 70 deletions Test/Unit/Magewire/Payment/Method/CreditCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

class CreditCardTest extends \PHPUnit\Framework\TestCase
{
private Quote $quote;
private MockObject $payment;
private MockObject $quote;
private MockObject $order;
private MockObject $checkoutStateDataValidator;
private MockObject $configuration;
private MockObject $session;
Expand All @@ -34,9 +36,16 @@ class CreditCardTest extends \PHPUnit\Framework\TestCase

public function setUp(): void
{
$this->payment = $this->getMockBuilder(Quote\Payment::class)
->disableOriginalConstructor()
->getMock();
$this->quote = $this->getMockBuilder(Quote::class)
->disableOriginalConstructor()
->getMock();
$this->order = $this->getMockBuilder(Order::class)
->disableOriginalConstructor()
->getMock();

$this->checkoutStateDataValidator = $this->getMockBuilder(CheckoutStateDataValidator::class)
->disableOriginalConstructor()
->getMock();
Expand Down Expand Up @@ -90,10 +99,10 @@ public function setUp(): void
->getMock();

$this->creditCard = new \Adyen\Hyva\Magewire\Payment\Method\CreditCard(
$this->context,
$this->brandsManager,
$this->installmentsManager
);
$this->context,
$this->brandsManager,
$this->installmentsManager
);
}

public function testGetMethodCode()
Expand Down Expand Up @@ -131,23 +140,12 @@ public function testPlaceOrder()
$paymentStatus = 'success';
$quoteId = '111';
$orderId = '123';
$payment = $this->getMockBuilder(Quote\Payment::class)->disableOriginalConstructor()->getMock();

$this->session->expects($this->once())
->method('getQuoteId')
->willReturn($quoteId);

$this->session->expects($this->once())
->method('getQuote')
->willReturn($this->quote);

$this->quote->expects($this->once())
->method('getPayment')
->willReturn($payment);
$this->setPlaceOrderCommonExpectations($quoteId);

$this->paymentInformationManagement->expects($this->once())
->method('savePaymentInformationAndPlaceOrder')
->with($quoteId, $payment)
->with($quoteId, $this->payment)
->willReturn($orderId);

$this->adyenOrderPaymentStatus->expects($this->once())
Expand All @@ -164,23 +162,12 @@ public function testPlaceOrderThrowsException()
{
$data = ['stateData' => []];
$quoteId = '111';
$payment = $this->getMockBuilder(Quote\Payment::class)->disableOriginalConstructor()->getMock();

$this->session->expects($this->once())
->method('getQuoteId')
->willReturn($quoteId);

$this->session->expects($this->once())
->method('getQuote')
->willReturn($this->quote);

$this->quote->expects($this->once())
->method('getPayment')
->willReturn($payment);
$this->setPlaceOrderCommonExpectations($quoteId);

$this->paymentInformationManagement->expects($this->once())
->method('savePaymentInformationAndPlaceOrder')
->with($quoteId, $payment)
->with($quoteId, $this->payment)
->willThrowException(new \Exception('Some error occurred while placing Order'));

$this->logger->expects($this->once())
Expand All @@ -192,6 +179,21 @@ public function testPlaceOrderThrowsException()
$this->assertEquals('{"isRefused":true}', $this->creditCard->paymentStatus);
}

private function setPlaceOrderCommonExpectations($quoteId)
{
$this->session->expects($this->once())
->method('getQuoteId')
->willReturn($quoteId);

$this->session->expects($this->once())
->method('getQuote')
->willReturn($this->quote);

$this->quote->expects($this->once())
->method('getPayment')
->willReturn($this->payment);
}

public function testEvaluateCompletion()
{
$valuationResultFactory = $this->getMockBuilder(\Hyva\Checkout\Model\Magewire\Component\EvaluationResultFactory::class)
Expand All @@ -208,19 +210,7 @@ public function testRefreshProperties()
$quoteId = '111';
$paymentResponse = '{"some-data-structure":["some-values"]}';

$this->session->expects($this->exactly(2))
->method('getQuote')
->willReturn($this->quote);

$this->quote->expects($this->once())
->method('isVirtual');

$this->quote->expects($this->once())
->method('getShippingAddress');

$this->session->expects($this->once())
->method('getQuoteId')
->willReturn($quoteId);
$this->setRefreshPropertiesCommonExpectations($quoteId);

$this->paymentMethods->expects($this->once())
->method('getData')
Expand All @@ -232,22 +222,15 @@ public function testRefreshProperties()
$this->assertEquals($paymentResponse, $this->creditCard->paymentResponse);
}


public function testRefreshPropertiesThrowsException()
{
$quoteId = '111';
$quoteIdException = '111';

$this->session->expects($this->exactly(2))
->method('getQuote')
->willReturn($this->quote);

$this->session->expects($this->once())
->method('getQuoteId')
->willReturn($quoteId);
$this->setRefreshPropertiesCommonExpectations($quoteIdException);

$this->paymentMethods->expects($this->once())
->method('getData')
->with($quoteId)
->with($quoteIdException)
->willThrowException(new \Exception('Some error occurred while refreshing properties'));

$this->logger->expects($this->once())
Expand All @@ -259,24 +242,32 @@ public function testRefreshPropertiesThrowsException()
$this->assertEquals('{}', $this->creditCard->paymentResponse);
}

private function setRefreshPropertiesCommonExpectations($quoteId)
{
$this->session->expects($this->exactly(2))
->method('getQuote')
->willReturn($this->quote);

$this->quote->expects($this->once())
->method('getShippingAddress');

$this->session->expects($this->once())
->method('getQuoteId')
->willReturn($quoteId);
}

public function testCollectPaymentDetails()
{
$data = [
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
];
$order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();

$orderId = '123';
$paymentDetails = '{"some-data-structure":[{"some-key":"some-value"}]}';

$this->session->expects($this->once())
->method('getLastRealOrder')
->willReturn($order);

$order->expects($this->once())
->method('getId')
->willReturn($orderId);
$this->setCollectPaymentDetailsCommonExpectations($orderId);

$this->adyenPaymentDetails->expects($this->once())
->method('initiate')
Expand All @@ -296,16 +287,9 @@ public function testCollectPaymentDetailsThrowsException()
'key2' => 'value2',
'key3' => 'value3',
];
$order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
$orderId = '123';

$this->session->expects($this->once())
->method('getLastRealOrder')
->willReturn($order);

$order->expects($this->once())
->method('getId')
->willReturn($orderId);
$this->setCollectPaymentDetailsCommonExpectations($orderId);

$this->adyenPaymentDetails->expects($this->once())
->method('initiate')
Expand All @@ -320,4 +304,15 @@ public function testCollectPaymentDetailsThrowsException()

$this->assertEquals('{"isRefused":true}', $this->creditCard->paymentDetails);
}

private function setCollectPaymentDetailsCommonExpectations($orderId)
{
$this->session->expects($this->once())
->method('getLastRealOrder')
->willReturn($this->order);

$this->order->expects($this->once())
->method('getId')
->willReturn($orderId);
}
}

0 comments on commit 5aa7a72

Please sign in to comment.