From a50a0cd5cb7b6912e5d3fa03f876215c3c0c3590 Mon Sep 17 00:00:00 2001 From: Kirill Braslavsky Date: Wed, 17 Mar 2021 10:30:02 +0200 Subject: [PATCH] update tests --- .../WcGateway/Gateway/WcGatewayTest.php | 48 ++++++++++++++----- .../Processor/OrderProcessorTest.php | 48 ++++++++++--------- .../ApplicationContextRepositoryTest.php | 5 +- .../Repository/CartRepositoryTest.php | 2 +- 4 files changed, 63 insertions(+), 40 deletions(-) diff --git a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php index df70e8071..d0418d41e 100644 --- a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php +++ b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php @@ -12,10 +12,10 @@ use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; -use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsFields; use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsRenderer; use Mockery; use function Brain\Monkey\Functions\expect; +use function Brain\Monkey\Functions\when; class WcGatewayTest extends TestCase { @@ -31,7 +31,7 @@ class WcGatewayTest extends TestCase $orderProcessor ->expects('process') ->andReturnUsing( - function(\WC_Order $order, $woocommerce) use ($wcOrder) : bool { + function(\WC_Order $order) use ($wcOrder) : bool { return $order === $wcOrder; } ); @@ -64,11 +64,14 @@ class WcGatewayTest extends TestCase ->with($orderId) ->andReturn($wcOrder); - global $woocommerce; - $woocommerce = Mockery::mock(\WooCommerce::class); + + when('wc_get_checkout_url') + ->justReturn('test'); + $result = $testee->process_payment($orderId); - unset($woocommerce); + $this->assertIsArray($result); + $this->assertEquals('success', $result['result']); $this->assertEquals($result['redirect'], $wcOrder); } @@ -106,10 +109,21 @@ class WcGatewayTest extends TestCase ->with($orderId) ->andReturn(false); - global $woocommerce; - $woocommerce = Mockery::mock(\WooCommerce::class); - $this->assertNull($testee->process_payment($orderId)); - unset($woocommerce); + $redirectUrl = 'http://example.com/checkout'; + + when('wc_get_checkout_url') + ->justReturn($redirectUrl); + + expect('wc_add_notice') + ->with('Couldn\'t find order to process','error'); + + $this->assertEquals( + [ + 'result' => 'failure', + 'redirect' => $redirectUrl + ], + $testee->process_payment($orderId) + ); } @@ -156,11 +170,19 @@ class WcGatewayTest extends TestCase expect('wc_add_notice') ->with($lastError, 'error'); - global $woocommerce; - $woocommerce = Mockery::mock(\WooCommerce::class); + $redirectUrl = 'http://example.com/checkout'; + + when('wc_get_checkout_url') + ->justReturn($redirectUrl); + $result = $testee->process_payment($orderId); - unset($woocommerce); - $this->assertNull($result); + $this->assertEquals( + [ + 'result' => 'failure', + 'redirect' => $redirectUrl + ], + $result + ); } public function testCaptureAuthorizedPayment() { diff --git a/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php b/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php index 125e87b47..03121934a 100644 --- a/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php +++ b/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php @@ -6,20 +6,19 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Processor; use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; -use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint; use Woocommerce\PayPalCommerce\ApiClient\Entity\Capture; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\Payments; use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit; use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory; -use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository; use WooCommerce\PayPalCommerce\Button\Helper\ThreeDSecure; use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\TestCase; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; use Mockery; +use function Brain\Monkey\Functions\when; class OrderProcessorTest extends TestCase { @@ -78,8 +77,6 @@ class OrderProcessorTest extends TestCase $sessionHandler ->expects('destroy_session_data'); - $cartRepository = Mockery::mock(CartRepository::class); - $orderEndpoint = Mockery::mock(OrderEndpoint::class); $orderEndpoint ->expects('patch_order_with') @@ -90,8 +87,6 @@ class OrderProcessorTest extends TestCase ->with($currentOrder) ->andReturn($currentOrder); - $paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class); - $orderFactory = Mockery::mock(OrderFactory::class); $orderFactory ->expects('from_wc_order') @@ -111,9 +106,7 @@ class OrderProcessorTest extends TestCase $testee = new OrderProcessor( $sessionHandler, - $cartRepository, $orderEndpoint, - $paymentsEndpoint, $orderFactory, $threeDSecure, $authorizedPaymentProcessor, @@ -126,6 +119,9 @@ class OrderProcessorTest extends TestCase $cart ->expects('empty_cart'); $woocommerce = Mockery::mock(\WooCommerce::class); + when('WC') + ->justReturn($woocommerce); + $woocommerce->cart = $cart; $wcOrder @@ -149,7 +145,9 @@ class OrderProcessorTest extends TestCase $wcOrder ->expects('update_status') ->with('on-hold', 'Awaiting payment.'); - $this->assertTrue($testee->process($wcOrder, $woocommerce)); + + + $this->assertTrue($testee->process($wcOrder)); } public function testCapture() { @@ -198,7 +196,6 @@ class OrderProcessorTest extends TestCase ->andReturn($currentOrder); $sessionHandler ->expects('destroy_session_data'); - $cartRepository = Mockery::mock(CartRepository::class); $orderEndpoint = Mockery::mock(OrderEndpoint::class); $orderEndpoint ->expects('patch_order_with') @@ -208,7 +205,6 @@ class OrderProcessorTest extends TestCase ->expects('capture') ->with($currentOrder) ->andReturn($currentOrder); - $paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class); $orderFactory = Mockery::mock(OrderFactory::class); $orderFactory ->expects('from_wc_order') @@ -221,14 +217,24 @@ class OrderProcessorTest extends TestCase ->shouldReceive('has') ->andReturnFalse(); + $cart = Mockery::mock(\WC_Cart::class); + $cart + ->shouldReceive('empty_cart'); + + $woocommerce = Mockery::Mock(\Woocommerce::class); + $woocommerce + ->shouldReceive('__get') + ->with('cart') + ->set('cart', $cart); + when('WC') + ->justReturn($woocommerce); + $logger = Mockery::mock(LoggerInterface::class); $testee = new OrderProcessor( $sessionHandler, - $cartRepository, $orderEndpoint, - $paymentsEndpoint, $orderFactory, $threeDSecure, $authorizedPaymentProcessor, @@ -243,6 +249,9 @@ class OrderProcessorTest extends TestCase $woocommerce = Mockery::mock(\WooCommerce::class); $woocommerce->cart = $cart; + when('WC') + ->justReturn($woocommerce); + $wcOrder ->expects('update_meta_data') ->with( @@ -265,7 +274,7 @@ class OrderProcessorTest extends TestCase $wcOrder ->expects('update_status') ->with('processing', 'Payment received.'); - $this->assertTrue($testee->process($wcOrder, $woocommerce)); + $this->assertTrue($testee->process($wcOrder)); } public function testError() { @@ -316,9 +325,7 @@ class OrderProcessorTest extends TestCase $sessionHandler ->expects('order') ->andReturn($currentOrder); - $cartRepository = Mockery::mock(CartRepository::class); $orderEndpoint = Mockery::mock(OrderEndpoint::class); - $paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class); $orderFactory = Mockery::mock(OrderFactory::class); $threeDSecure = Mockery::mock(ThreeDSecure::class); $authorizedPaymentProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class); @@ -328,9 +335,7 @@ class OrderProcessorTest extends TestCase $testee = new OrderProcessor( $sessionHandler, - $cartRepository, $orderEndpoint, - $paymentsEndpoint, $orderFactory, $threeDSecure, $authorizedPaymentProcessor, @@ -339,10 +344,6 @@ class OrderProcessorTest extends TestCase false ); - $cart = Mockery::mock(\WC_Cart::class); - $woocommerce = Mockery::mock(\WooCommerce::class); - $woocommerce->cart = $cart; - $wcOrder ->expects('update_meta_data') ->with( @@ -355,7 +356,8 @@ class OrderProcessorTest extends TestCase PayPalGateway::INTENT_META_KEY, $orderIntent ); - $this->assertFalse($testee->process($wcOrder, $woocommerce)); + + $this->assertFalse($testee->process($wcOrder)); $this->assertNotEmpty($testee->last_error()); } diff --git a/tests/PHPUnit/WcGateway/Repository/ApplicationContextRepositoryTest.php b/tests/PHPUnit/WcGateway/Repository/ApplicationContextRepositoryTest.php index 8c346d13e..7b8d1800e 100644 --- a/tests/PHPUnit/WcGateway/Repository/ApplicationContextRepositoryTest.php +++ b/tests/PHPUnit/WcGateway/Repository/ApplicationContextRepositoryTest.php @@ -2,10 +2,11 @@ declare(strict_types=1); -namespace WooCommerce\PayPalCommerce\ApiClient\Repository; +namespace WooCommerce\PayPalCommerce\WcGateway\Repository; use Hamcrest\Matchers; use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext; +use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository; use WooCommerce\PayPalCommerce\TestCase; use Mockery\MockInterface; use Psr\Container\ContainerInterface; @@ -25,8 +26,6 @@ class ApplicationContextRepositoryTest extends TestCase array $expected ): void { - $brandName = 'Acme Corp.'; - // Config foreach ($container as $key => $value) { $this->buildTestee()[0]->shouldReceive('has') diff --git a/tests/PHPUnit/WcGateway/Repository/CartRepositoryTest.php b/tests/PHPUnit/WcGateway/Repository/CartRepositoryTest.php index 4a359b466..db8cc6322 100644 --- a/tests/PHPUnit/WcGateway/Repository/CartRepositoryTest.php +++ b/tests/PHPUnit/WcGateway/Repository/CartRepositoryTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace WooCommerce\PayPalCommerce\ApiClient\Repository; +namespace WooCommerce\PayPalCommerce\WcGateway\Repository; use Mockery\Adapter\Phpunit\MockeryTestCase;