diff --git a/tests/PHPUnit/WcGateway/Gateway/OXXO/OXXOGatewayTest.php b/tests/PHPUnit/WcGateway/Gateway/OXXO/OXXOGatewayTest.php new file mode 100644 index 000000000..849353b86 --- /dev/null +++ b/tests/PHPUnit/WcGateway/Gateway/OXXO/OXXOGatewayTest.php @@ -0,0 +1,140 @@ +orderEndpoint = Mockery::mock(OrderEndpoint::class); + $this->purchaseUnitFactory = Mockery::mock(PurchaseUnitFactory::class); + $this->shippingPreferenceFactory = Mockery::mock(ShippingPreferenceFactory::class); + $this->logger = Mockery::mock(LoggerInterface::class); + + $this->wcOrder = Mockery::mock(WC_Order::class); + when('wc_get_order')->justReturn($this->wcOrder); + when('get_option')->justReturn([ + 'title' => 'foo', + 'description' => 'bar', + ]); + + $this->testee = new OXXOGateway( + $this->orderEndpoint, + $this->purchaseUnitFactory, + $this->shippingPreferenceFactory, + $this->logger + ); + } + + public function testProcessPaymentSuccess() + { + $this->wcOrder->shouldReceive('get_billing_first_name')->andReturn('John'); + $this->wcOrder->shouldReceive('get_billing_last_name')->andReturn('Doe'); + $this->wcOrder->shouldReceive('get_billing_email')->andReturn('foo@bar.com'); + $this->wcOrder->shouldReceive('get_billing_country')->andReturn('MX'); + + list($purchaseUnit, $shippingPreference) = $this->setStubs(); + + $linkHref = 'https://sandbox.paypal.com/payment/oxxo?token=ABC123'; + $this->orderEndpoint + ->shouldReceive('confirm_payment_source') + ->with('1', [ + 'oxxo' => [ + 'name' => 'John Doe', + 'email' => 'foo@bar.com', + 'country_code' => 'MX', + ] + ] + )->andReturn((object)[ + 'links' => [ + (object)[ + 'rel' => 'payer-action', + 'href' => $linkHref, + ], + ] + ]); + + $order = Mockery::mock(Order::class); + $order->shouldReceive('id')->andReturn('1'); + + $this->orderEndpoint + ->shouldReceive('create') + ->with([$purchaseUnit], $shippingPreference) + ->andReturn($order); + + $this->wcOrder + ->shouldReceive('add_meta_data') + ->with('ppcp_oxxo_payer_action', $linkHref) + ->andReturn(true); + $this->wcOrder->shouldReceive('save_meta_data'); + + $woocommerce = Mockery::mock(\WooCommerce::class); + $cart = Mockery::mock(\WC_Cart::class); + when('WC')->justReturn($woocommerce); + $woocommerce->cart = $cart; + $cart->shouldReceive('empty_cart'); + + $result = $this->testee->process_payment(1); + $this->assertEquals('success', $result['result']); + } + + public function testProcessPaymentFailure() + { + list($purchaseUnit, $shippingPreference) = $this->setStubs(); + + $this->orderEndpoint + ->shouldReceive('create') + ->with([$purchaseUnit], $shippingPreference) + ->andThrows(RuntimeException::class); + + $this->logger->shouldReceive('error'); + when('wc_add_notice')->justReturn(); + when('wc_get_checkout_url')->justReturn(); + $this->wcOrder->shouldReceive('update_status'); + + $result = $this->testee->process_payment(1); + $this->assertEquals('failure', $result['result']); + + } + + /** + * @return array + */ + private function setStubs(): array + { + $purchaseUnit = Mockery::mock(PurchaseUnit::class); + $this->purchaseUnitFactory + ->shouldReceive('from_wc_order') + ->with($this->wcOrder) + ->andReturn($purchaseUnit); + + $shippingPreference = 'SOME_SHIPPING_PREFERENCE'; + $this->shippingPreferenceFactory + ->shouldReceive('from_state') + ->with($purchaseUnit, 'checkout') + ->andReturn($shippingPreference); + return array($purchaseUnit, $shippingPreference); + } +}