From b2ba72c06c8907eab77d5327d14ba6f4a301faed Mon Sep 17 00:00:00 2001 From: Alex P Date: Tue, 14 Nov 2023 10:56:15 +0200 Subject: [PATCH] Add ppcp_create_paypal_order_for_wc_order --- api/order-functions.php | 14 +++++ .../PHPUnit/Api/CreateOrderForWcOrderTest.php | 52 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/PHPUnit/Api/CreateOrderForWcOrderTest.php diff --git a/api/order-functions.php b/api/order-functions.php index 701b064fb..340be8585 100644 --- a/api/order-functions.php +++ b/api/order-functions.php @@ -21,6 +21,7 @@ use WooCommerce\PayPalCommerce\PPCP; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use WooCommerce\PayPalCommerce\WcGateway\Helper\RefundFeesUpdater; use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor; +use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor; /** @@ -47,6 +48,19 @@ function ppcp_get_paypal_order( $paypal_id_or_wc_order ): Order { return $order_endpoint->order( $paypal_id_or_wc_order ); } +/** + * Creates a PayPal order for the given WC order. + * + * @param WC_Order $wc_order The WC order. + * @throws Exception When the operation fails. + */ +function ppcp_create_paypal_order_for_wc_order( WC_Order $wc_order ): Order { + $order_processor = PPCP::container()->get( 'wcgateway.order-processor' ); + assert( $order_processor instanceof OrderProcessor ); + + return $order_processor->create_order( $wc_order ); +} + /** * Captures the PayPal order. * diff --git a/tests/PHPUnit/Api/CreateOrderForWcOrderTest.php b/tests/PHPUnit/Api/CreateOrderForWcOrderTest.php new file mode 100644 index 000000000..b2d16caf7 --- /dev/null +++ b/tests/PHPUnit/Api/CreateOrderForWcOrderTest.php @@ -0,0 +1,52 @@ +orderProcesor = Mockery::mock(OrderProcessor::class); + + $this->bootstrapModule([ + 'wcgateway.order-processor' => function () { + return $this->orderProcesor; + }, + ]); + } + + public function testSuccess(): void { + $wcOrder = Mockery::mock(WC_Order::class); + $ret = Mockery::mock(Order::class); + $this->orderProcesor + ->expects('create_order') + ->with($wcOrder) + ->andReturn($ret) + ->once(); + + self::assertEquals($ret, ppcp_create_paypal_order_for_wc_order($wcOrder)); + } + + public function testFailure(): void { + $wcOrder = Mockery::mock(WC_Order::class); + $this->orderProcesor + ->expects('create_order') + ->with($wcOrder) + ->andThrow(new RuntimeException()) + ->once(); + + $this->expectException(RuntimeException::class); + + ppcp_create_paypal_order_for_wc_order($wcOrder); + } +}