From c7cf9bc3e9aaf52b3286c99c3428fd1106d5b077 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Fri, 22 Apr 2022 11:49:30 +0200 Subject: [PATCH] Add unit tests --- .../Endpoint/PayUponInvoiceOrderEndpoint.php} | 2 +- .../ppcp-compat/src/PPEC/DeactivateNote.php | 2 +- modules/ppcp-wc-gateway/services.php | 6 +- .../Gateway/PayUponInvoice/PayUponInvoice.php | 6 +- .../PayUponInvoice/PayUponInvoiceGateway.php | 6 +- .../PayUponInvoiceGatewayTest.php | 122 ++++++++++++++++++ 6 files changed, 133 insertions(+), 11 deletions(-) rename modules/{ppcp-wc-gateway/src/Gateway/PayUponInvoice/OrderEndpoint.php => ppcp-api-client/src/Endpoint/PayUponInvoiceOrderEndpoint.php} (99%) create mode 100644 tests/PHPUnit/WcGateway/Gateway/PayUponInvoice/PayUponInvoiceGatewayTest.php diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/OrderEndpoint.php b/modules/ppcp-api-client/src/Endpoint/PayUponInvoiceOrderEndpoint.php similarity index 99% rename from modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/OrderEndpoint.php rename to modules/ppcp-api-client/src/Endpoint/PayUponInvoiceOrderEndpoint.php index 36f91615c..c6784530a 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/OrderEndpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/PayUponInvoiceOrderEndpoint.php @@ -22,7 +22,7 @@ use WP_Error; /** * Class OrderEndpoint. */ -class OrderEndpoint { +class PayUponInvoiceOrderEndpoint { use RequestTrait; diff --git a/modules/ppcp-compat/src/PPEC/DeactivateNote.php b/modules/ppcp-compat/src/PPEC/DeactivateNote.php index 07e0dbe1b..9ced83d22 100644 --- a/modules/ppcp-compat/src/PPEC/DeactivateNote.php +++ b/modules/ppcp-compat/src/PPEC/DeactivateNote.php @@ -18,7 +18,7 @@ use Automattic\WooCommerce\Admin\Notes\NoteTraits; */ class DeactivateNote { - use NoteTraits; + //use NoteTraits; /** * Name of the note for use in the database. diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index f201d8964..df6c13ac9 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -32,7 +32,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\FraudNet; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\FraudNetSessionId; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\FraudNetSourceWebsiteId; -use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\OrderEndpoint; +use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceOrderEndpoint; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PaymentSourceFactory; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoice; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceGateway; @@ -2151,8 +2151,8 @@ return array( $container->get( 'wcgateway.settings' ) ); }, - 'wcgateway.pay-upon-invoice-order-endpoint' => static function ( ContainerInterface $container ): OrderEndpoint { - return new OrderEndpoint( + 'wcgateway.pay-upon-invoice-order-endpoint' => static function ( ContainerInterface $container ): PayUponInvoiceOrderEndpoint { + return new PayUponInvoiceOrderEndpoint( $container->get( 'api.host' ), $container->get( 'api.bearer' ), $container->get( 'api.factory.order' ), diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php index 673cbc095..7f5275827 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php @@ -39,7 +39,7 @@ class PayUponInvoice { /** * The order endpoint. * - * @var OrderEndpoint + * @var PayUponInvoiceOrderEndpoint */ protected $order_endpoint; @@ -76,7 +76,7 @@ class PayUponInvoice { * * @param string $module_url The module URL. * @param FraudNet $fraud_net The FraudNet entity. - * @param OrderEndpoint $order_endpoint The order endpoint. + * @param PayUponInvoiceOrderEndpoint $order_endpoint The order endpoint. * @param LoggerInterface $logger The logger. * @param Settings $settings The settings. * @param Environment $environment The environment. @@ -85,7 +85,7 @@ class PayUponInvoice { public function __construct( string $module_url, FraudNet $fraud_net, - OrderEndpoint $order_endpoint, + PayUponInvoiceOrderEndpoint $order_endpoint, LoggerInterface $logger, Settings $settings, Environment $environment, diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php index 0f69765b7..926e45dcc 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php @@ -29,7 +29,7 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway { /** * The order endpoint. * - * @var OrderEndpoint + * @var PayUponInvoiceOrderEndpoint */ protected $order_endpoint; @@ -64,14 +64,14 @@ class PayUponInvoiceGateway extends WC_Payment_Gateway { /** * PayUponInvoiceGateway constructor. * - * @param OrderEndpoint $order_endpoint The order endpoint. + * @param PayUponInvoiceOrderEndpoint $order_endpoint The order endpoint. * @param PurchaseUnitFactory $purchase_unit_factory The purchase unit factory. * @param PaymentSourceFactory $payment_source_factory The payment source factory. * @param Environment $environment The environment. * @param LoggerInterface $logger The logger. */ public function __construct( - OrderEndpoint $order_endpoint, + PayUponInvoiceOrderEndpoint $order_endpoint, PurchaseUnitFactory $purchase_unit_factory, PaymentSourceFactory $payment_source_factory, Environment $environment, diff --git a/tests/PHPUnit/WcGateway/Gateway/PayUponInvoice/PayUponInvoiceGatewayTest.php b/tests/PHPUnit/WcGateway/Gateway/PayUponInvoice/PayUponInvoiceGatewayTest.php new file mode 100644 index 000000000..00ad76c5e --- /dev/null +++ b/tests/PHPUnit/WcGateway/Gateway/PayUponInvoice/PayUponInvoiceGatewayTest.php @@ -0,0 +1,122 @@ +order_endpoint = Mockery::mock(PayUponInvoiceOrderEndpoint::class); + $this->purchase_unit_factory = Mockery::mock(PurchaseUnitFactory::class); + $this->payment_source_factory = Mockery::mock(PaymentSourceFactory::class); + $this->environment = Mockery::mock(Environment::class); + $this->logger = Mockery::mock(LoggerInterface::class); + + $this->setInitStubs(); + + $this->testee = new PayUponInvoiceGateway( + $this->order_endpoint, + $this->purchase_unit_factory, + $this->payment_source_factory, + $this->environment, + $this->logger + ); + } + + public function testProcessPayment() + { + list($order, $purchase_unit, $payment_source) = $this->setTestStubs(); + + $this->order_endpoint->shouldReceive('create')->with( + [$purchase_unit], + $payment_source, + '' + )->andReturn($order); + + $result = $this->testee->process_payment(1); + $this->assertEquals('success', $result['result']); + } + + public function testProcessPaymentError() + { + list($order, $purchase_unit, $payment_source) = $this->setTestStubs(); + + $this->logger->shouldReceive('error'); + when('wc_add_notice')->justReturn(); + when('wc_get_checkout_url')->justReturn(); + + $this->order_endpoint->shouldReceive('create')->with( + [$purchase_unit], + $payment_source, + '' + )->andThrows(\RuntimeException::class); + + $result = $this->testee->process_payment(1); + $this->assertEquals('failure', $result['result']); + } + + private function setInitStubs(): void + { + when('get_option')->justReturn([ + 'title' => 'foo', + 'description' => 'bar', + ]); + when('get_bloginfo')->justReturn('Foo'); + + $woocommerce = Mockery::mock(\WooCommerce::class); + $cart = Mockery::mock(\WC_Cart::class); + when('WC')->justReturn($woocommerce); + $woocommerce->cart = $cart; + $cart->shouldReceive('empty_cart'); + } + + /** + * @return array + */ + private function setTestStubs(): array + { + $wcOrder = Mockery::mock(WC_Order::class); + $wcOrder->shouldReceive('update_meta_data'); + $wcOrder->shouldReceive('update_status'); + when('wc_get_order')->justReturn($wcOrder); + + $order = Mockery::mock(Order::class); + $order->shouldReceive('id')->andReturn('1'); + $order->shouldReceive('intent')->andReturn('CAPTURE'); + + $purchase_unit = Mockery::mock(PurchaseUnit::class); + $payment_source = Mockery::mock(PaymentSource::class); + + $this->payment_source_factory->shouldReceive('from_wc_order') + ->with($wcOrder, '') + ->andReturn($payment_source); + + $this->purchase_unit_factory->shouldReceive('from_wc_order') + ->with($wcOrder) + ->andReturn($purchase_unit); + + $this->environment->shouldReceive('current_environment_is')->andReturn(true); + + return array($order, $purchase_unit, $payment_source); + } +}