* Fix tests

This commit is contained in:
Pedro Silva 2023-08-04 14:18:27 +01:00
parent ef5fc4b3d4
commit 4027f4a77e
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
5 changed files with 61 additions and 6 deletions

View file

@ -42,7 +42,7 @@ class Payments {
* @param array $captures The Captures.
* @param array $refunds The Refunds.
*/
public function __construct( array $authorizations, array $captures, array $refunds ) {
public function __construct( array $authorizations, array $captures, array $refunds = array() ) {
foreach ( $authorizations as $key => $authorization ) {
if ( is_a( $authorization, Authorization::class ) ) {
continue;

View file

@ -34,7 +34,7 @@ class FeesRenderer {
$refund_fee = $refund_breakdown['paypal_fee'] ?? array();
$refund_amount = $refund_breakdown['net_amount'] ?? array();
$refund_total = ( $refund_fee['value'] ?? 0 ) + ( $refund_amount['value'] ?? 0 );
$refund_currency = ( $refund_amount['currency_code'] === $refund_fee['currency_code'] ) ? $refund_amount['currency_code'] : '';
$refund_currency = ( ( $refund_amount['currency_code'] ?? '' ) === ( $refund_fee['currency_code'] ?? '' ) ) ? ( $refund_amount['currency_code'] ?? '' ) : '';
$html = '';

View file

@ -43,10 +43,19 @@ class PaymentsTest extends TestCase
'status' => 'CREATED',
]
);
$captures = [$capture];
$authorizations = [$authorization];
$refund = \Mockery::mock(Refund::class);
$refund->shouldReceive('to_array')->andReturn(
[
'id' => 'refund',
'status' => 'CREATED',
]
);
$testee = new Payments($authorizations, $captures);
$authorizations = [$authorization];
$captures = [$capture];
$refunds = [$refund];
$testee = new Payments($authorizations, $captures, $refunds);
$this->assertEquals(
[
@ -62,6 +71,12 @@ class PaymentsTest extends TestCase
'status' => 'CREATED',
],
],
'refunds' => [
[
'id' => 'refund',
'status' => 'CREATED',
],
],
],
$testee->to_array()
);

View file

@ -7,6 +7,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payments;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Refund;
use WooCommerce\PayPalCommerce\TestCase;
use Mockery;
@ -18,11 +19,15 @@ class PaymentsFactoryTest extends TestCase
$authorization->shouldReceive('to_array')->andReturn(['id' => 'foo', 'status' => 'CREATED']);
$capture = Mockery::mock(Capture::class);
$capture->shouldReceive('to_array')->andReturn(['id' => 'capture', 'status' => 'CREATED']);
$refund = Mockery::mock(Refund::class);
$refund->shouldReceive('to_array')->andReturn(['id' => 'refund', 'status' => 'CREATED']);
$authorizationsFactory = Mockery::mock(AuthorizationFactory::class);
$authorizationsFactory->shouldReceive('from_paypal_response')->andReturn($authorization);
$captureFactory = Mockery::mock(CaptureFactory::class);
$captureFactory->shouldReceive('from_paypal_response')->andReturn($capture);
$refundFactory = Mockery::mock(RefundFactory::class);
$refundFactory->shouldReceive('from_paypal_response')->andReturn($refund);
$response = (object)[
'authorizations' => [
(object)['id' => 'foo', 'status' => 'CREATED'],
@ -30,9 +35,12 @@ class PaymentsFactoryTest extends TestCase
'captures' => [
(object)['id' => 'capture', 'status' => 'CREATED'],
],
'refunds' => [
(object)['id' => 'refund', 'status' => 'CREATED'],
],
];
$testee = new PaymentsFactory($authorizationsFactory, $captureFactory);
$testee = new PaymentsFactory($authorizationsFactory, $captureFactory, $refundFactory);
$result = $testee->from_paypal_response($response);
$this->assertInstanceOf(Payments::class, $result);
@ -44,6 +52,9 @@ class PaymentsFactoryTest extends TestCase
'captures' => [
['id' => 'capture', 'status' => 'CREATED'],
],
'refunds' => [
['id' => 'refund', 'status' => 'CREATED'],
],
];
$this->assertEquals($expectedToArray, $result->to_array());
}

View file

@ -43,11 +43,32 @@ class FeesRendererTest extends TestCase
],
]);
$wcOrder->expects('get_meta')
->with(PayPalGateway::REFUND_FEES_META_KEY)
->andReturn([
'gross_amount' => [
'currency_code' => 'USD',
'value' => '20.52',
],
'paypal_fee' => [
'currency_code' => 'USD',
'value' => '0.51',
],
'net_amount' => [
'currency_code' => 'USD',
'value' => '50.01',
],
]);
$result = $this->renderer->render($wcOrder);
$this->assertStringContainsString('Fee', $result);
$this->assertStringContainsString('0.41', $result);
$this->assertStringContainsString('Payout', $result);
$this->assertStringContainsString('10.01', $result);
$this->assertStringContainsString('PayPal Refund Fee', $result);
$this->assertStringContainsString('0.51', $result);
$this->assertStringContainsString('PayPal Refund', $result);
$this->assertStringContainsString('50.01', $result);
}
public function testRenderWithoutNet() {
@ -62,6 +83,10 @@ class FeesRendererTest extends TestCase
],
]);
$wcOrder->expects('get_meta')
->with(PayPalGateway::REFUND_FEES_META_KEY)
->andReturn([]);
$result = $this->renderer->render($wcOrder);
$this->assertStringContainsString('Fee', $result);
$this->assertStringContainsString('0.41', $result);
@ -78,6 +103,10 @@ class FeesRendererTest extends TestCase
->with(PayPalGateway::FEES_META_KEY)
->andReturn($meta);
$wcOrder->expects('get_meta')
->with(PayPalGateway::REFUND_FEES_META_KEY)
->andReturn([]);
$this->assertSame('', $this->renderer->render($wcOrder));
}