From 80b129c92dec2ce1e19ec9dd47cd0003741ccebf Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 29 Apr 2020 08:58:16 +0300 Subject: [PATCH] add WcGateway tests --- .../ppcp-wc-gateway/src/Gateway/WcGateway.php | 13 +- .../WcGateway/Gateway/WcGatewayTest.php | 271 ++++++++++++++++++ tests/stubs/WC_Payment_Gateway.php | 15 + 3 files changed, 290 insertions(+), 9 deletions(-) create mode 100644 tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php diff --git a/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php b/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php index e3aa4b0b1..6fda7e7ef 100644 --- a/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php +++ b/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php @@ -74,7 +74,7 @@ class WcGateway extends WcGatewayBase public function process_payment($orderId): ?array { global $woocommerce; - $wcOrder = new \WC_Order($orderId); + $wcOrder = wc_get_order($orderId); if (! is_a($wcOrder, \WC_Order::class)) { return null; } @@ -97,10 +97,7 @@ class WcGateway extends WcGatewayBase if ($isProcessed) { $wcOrder->add_order_note( - __( - 'Payment successfully captured.', - 'woocommerce-paypal-gateway' - ) + __('Payment successfully captured.', 'woocommerce-paypal-gateway') ); $wcOrder->set_status('processing'); @@ -112,10 +109,7 @@ class WcGateway extends WcGatewayBase if ($this->authorizedPayments->lastStatus() === AuthorizedPaymentsProcessor::ALREADY_CAPTURED) { if ($wcOrder->get_status() === 'on-hold') { $wcOrder->add_order_note( - __( - 'Payment successfully captured.', - 'woocommerce-paypal-gateway' - ) + __('Payment successfully captured.','woocommerce-paypal-gateway') ); $wcOrder->set_status('processing'); } @@ -124,6 +118,7 @@ class WcGateway extends WcGatewayBase $wcOrder->save(); return true; } + return false; } private function renderAuthorizationMessageForStatus(string $status) { diff --git a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php new file mode 100644 index 000000000..a363bdec3 --- /dev/null +++ b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php @@ -0,0 +1,271 @@ + 'value']; + $settingsFields = Mockery::mock(SettingsFields::class); + $settingsFields + ->expects('fields') + ->andReturn($expectedFields); + $orderProcessor = Mockery::mock(OrderProcessor::class); + $authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class); + $authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class); + $testee = new WcGateway( + $settingsFields, + $orderProcessor, + $authorizedPaymentsProcessor, + $authorizedOrderActionNotice + ); + $this->assertEquals($testee->form_fields, $expectedFields); + } + + + public function testProcessPaymentSuccess() { + + $orderId = 1; + $wcOrder = Mockery::mock(\WC_Order::class); + $settingsFields = Mockery::mock(SettingsFields::class); + $settingsFields + ->expects('fields') + ->andReturn([]); + $orderProcessor = Mockery::mock(OrderProcessor::class); + $orderProcessor + ->expects('process') + ->andReturnUsing( + function(\WC_Order $order, $woocommerce) use ($wcOrder) : bool { + return $order === $wcOrder; + } + ); + $authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class); + $authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class); + $testee = new WcGateway( + $settingsFields, + $orderProcessor, + $authorizedPaymentsProcessor, + $authorizedOrderActionNotice + ); + + expect('wc_get_order') + ->with($orderId) + ->andReturn($wcOrder); + + $result = $testee->process_payment($orderId); + $this->assertIsArray($result); + $this->assertEquals('success', $result['result']); + $this->assertEquals($result['redirect'], $wcOrder); + } + + public function testProcessPaymentOrderNotFound() { + + $orderId = 1; + $settingsFields = Mockery::mock(SettingsFields::class); + $settingsFields + ->expects('fields') + ->andReturn([]); + $orderProcessor = Mockery::mock(OrderProcessor::class); + $authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class); + $authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class); + $testee = new WcGateway( + $settingsFields, + $orderProcessor, + $authorizedPaymentsProcessor, + $authorizedOrderActionNotice + ); + + expect('wc_get_order') + ->with($orderId) + ->andReturn(false); + + $this->assertNull($testee->process_payment($orderId)); + } + + + public function testProcessPaymentFails() { + + $orderId = 1; + $wcOrder = Mockery::mock(\WC_Order::class); + $lastError = 'some-error'; + $settingsFields = Mockery::mock(SettingsFields::class); + $settingsFields + ->expects('fields') + ->andReturn([]); + $orderProcessor = Mockery::mock(OrderProcessor::class); + $orderProcessor + ->expects('process') + ->andReturnFalse(); + $orderProcessor + ->expects('lastError') + ->andReturn($lastError); + $authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class); + $authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class); + $testee = new WcGateway( + $settingsFields, + $orderProcessor, + $authorizedPaymentsProcessor, + $authorizedOrderActionNotice + ); + + expect('wc_get_order') + ->with($orderId) + ->andReturn($wcOrder); + expect('wc_add_notice') + ->with($lastError); + + $result = $testee->process_payment($orderId); + $this->assertNull($result); + } + + public function testCaptureAuthorizedPayment() { + + $wcOrder = Mockery::mock(\WC_Order::class); + $wcOrder + ->expects('add_order_note'); + $wcOrder + ->expects('set_status') + ->with('processing'); + $wcOrder + ->expects('update_meta_data') + ->with(WcGateway::CAPTURED_META_KEY, 'true'); + $wcOrder + ->expects('save'); + $settingsFields = Mockery::mock(SettingsFields::class); + $settingsFields + ->expects('fields') + ->andReturn([]); + $orderProcessor = Mockery::mock(OrderProcessor::class); + $authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class); + $authorizedPaymentsProcessor + ->expects('process') + ->with($wcOrder) + ->andReturnTrue(); + $authorizedPaymentsProcessor + ->expects('lastStatus') + ->andReturn(AuthorizedPaymentsProcessor::SUCCESSFUL); + $authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class); + $authorizedOrderActionNotice + ->expects('displayMessage') + ->with(AuthorizeOrderActionNotice::SUCCESS); + $testee = new WcGateway( + $settingsFields, + $orderProcessor, + $authorizedPaymentsProcessor, + $authorizedOrderActionNotice + ); + + $this->assertTrue($testee->captureAuthorizedPayment($wcOrder)); + } + + public function testCaptureAuthorizedPaymentHasAlreadyBeenCaptured() { + + $wcOrder = Mockery::mock(\WC_Order::class); + $wcOrder + ->expects('get_status') + ->andReturn('on-hold'); + $wcOrder + ->expects('add_order_note'); + $wcOrder + ->expects('set_status') + ->with('processing'); + $wcOrder + ->expects('update_meta_data') + ->with(WcGateway::CAPTURED_META_KEY, 'true'); + $wcOrder + ->expects('save'); + $settingsFields = Mockery::mock(SettingsFields::class); + $settingsFields + ->expects('fields') + ->andReturn([]); + $orderProcessor = Mockery::mock(OrderProcessor::class); + $authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class); + $authorizedPaymentsProcessor + ->expects('process') + ->with($wcOrder) + ->andReturnFalse(); + $authorizedPaymentsProcessor + ->shouldReceive('lastStatus') + ->andReturn(AuthorizedPaymentsProcessor::ALREADY_CAPTURED); + $authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class); + $authorizedOrderActionNotice + ->expects('displayMessage') + ->with(AuthorizeOrderActionNotice::ALREADY_CAPTURED); + $testee = new WcGateway( + $settingsFields, + $orderProcessor, + $authorizedPaymentsProcessor, + $authorizedOrderActionNotice + ); + + $this->assertTrue($testee->captureAuthorizedPayment($wcOrder)); + } + + /** + * @dataProvider dataForTestCaptureAuthorizedPaymentNoActionableFailures + * + * @param string $lastStatus + * @param int $expectedMessage + */ + public function testCaptureAuthorizedPaymentNoActionableFailures($lastStatus, $expectedMessage) { + + $wcOrder = Mockery::mock(\WC_Order::class); + $settingsFields = Mockery::mock(SettingsFields::class); + $settingsFields + ->expects('fields') + ->andReturn([]); + $orderProcessor = Mockery::mock(OrderProcessor::class); + $authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class); + $authorizedPaymentsProcessor + ->expects('process') + ->with($wcOrder) + ->andReturnFalse(); + $authorizedPaymentsProcessor + ->shouldReceive('lastStatus') + ->andReturn($lastStatus); + $authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class); + $authorizedOrderActionNotice + ->expects('displayMessage') + ->with($expectedMessage); + $testee = new WcGateway( + $settingsFields, + $orderProcessor, + $authorizedPaymentsProcessor, + $authorizedOrderActionNotice + ); + + $this->assertFalse($testee->captureAuthorizedPayment($wcOrder)); + } + + public function dataForTestCaptureAuthorizedPaymentNoActionableFailures() : array + { + return [ + 'inaccessible' => [ + AuthorizedPaymentsProcessor::INACCESSIBLE, + AuthorizeOrderActionNotice::NO_INFO, + ], + 'not_found' => [ + AuthorizedPaymentsProcessor::NOT_FOUND, + AuthorizeOrderActionNotice::NOT_FOUND, + ], + 'not_mapped' => [ + 'some-other-failure', + AuthorizeOrderActionNotice::FAILED, + ], + ]; + } +} \ No newline at end of file diff --git a/tests/stubs/WC_Payment_Gateway.php b/tests/stubs/WC_Payment_Gateway.php index c94fb269a..6fbb823c8 100644 --- a/tests/stubs/WC_Payment_Gateway.php +++ b/tests/stubs/WC_Payment_Gateway.php @@ -4,4 +4,19 @@ declare(strict_types=1); class WC_Payment_Gateway { + protected function get_option(string $key) : string { + return $key; + } + + protected function init_settings() { + + } + + protected function get_return_url($wcOrder) { + return $wcOrder; + } + + public function process_admin_options() { + + } } \ No newline at end of file