subscriptionHelper = Mockery::mock(SubscriptionHelper::class); $this->paymentTokenRepository = Mockery::mock(PaymentTokenRepository::class); $this->purchaseUnitFactory = Mockery::mock(PurchaseUnitFactory::class); $this->payerFactory = Mockery::mock(PayerFactory::class); $this->shippingPreferenceFactory = Mockery::mock(ShippingPreferenceFactory::class); $this->orderEndpoint = Mockery::mock(OrderEndpoint::class); $this->environment = Mockery::mock(Environment::class); $this->authorizedPaymentProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class); $this->config = Mockery::mock(ContainerInterface::class); $this->testee = new VaultedCreditCardHandler( $this->subscriptionHelper, $this->paymentTokenRepository, $this->purchaseUnitFactory, $this->payerFactory, $this->shippingPreferenceFactory, $this->orderEndpoint, $this->environment, $this->authorizedPaymentProcessor, $this->config ); } public function testHandlePaymentChangingPayment() { $_POST['woocommerce_change_payment'] = 1; $wcOrder = Mockery::mock(\WC_Order::class); $wcOrder->shouldReceive('get_id')->andReturn(1); $wcOrder->shouldReceive('update_meta_data') ->with('payment_token_id', 'abc123') ->andReturn(1); $wcOrder->shouldReceive('save')->andReturn(1); $this->subscriptionHelper->shouldReceive('has_subscription')->andReturn(true); $this->subscriptionHelper->shouldReceive('is_subscription_change_payment')->andReturn(true); $customer = Mockery::mock(WC_Customer::class); $result = $this->testee->handle_payment('abc123', $wcOrder, $customer); $this->assertInstanceOf(\WC_Order::class, $result); } public function testHandlePayment() { $_POST['woocommerce_change_payment'] = null; $wcOrder = Mockery::mock(\WC_Order::class); $wcOrder->shouldReceive('get_id')->andReturn(1); $wcOrder->shouldReceive('get_customer_id')->andReturn(1); $wcOrder->shouldReceive('update_meta_data')->andReturn(1); $wcOrder->shouldReceive('save')->once(); $wcOrder->shouldReceive('payment_complete')->andReturn(true); $token = Mockery::mock(PaymentToken::class); $tokenId = 'abc123'; $token->shouldReceive('id')->andReturn($tokenId); $this->paymentTokenRepository->shouldReceive('all_for_user_id') ->andReturn([$token]); $purchaseUnit = Mockery::mock(PurchaseUnit::class); $this->purchaseUnitFactory->shouldReceive('from_wc_order') ->andReturn($purchaseUnit); $customer = Mockery::mock(WC_Customer::class); $payer = Mockery::mock(Payer::class); $this->payerFactory->shouldReceive('from_wc_order') ->andReturn($payer); $this->shippingPreferenceFactory->shouldReceive('from_state') ->andReturn('some_preference'); $order = Mockery::mock(Order::class); $order->shouldReceive('id')->andReturn('1'); $order->shouldReceive('intent')->andReturn('CAPTURE'); $paymentSource = Mockery::mock(PaymentSource::class); $paymentSourceCard = Mockery::mock(PaymentSourceCard::class); $paymentSource->shouldReceive('card')->andReturn($paymentSourceCard); $order->shouldReceive('payment_source')->andReturn($paymentSource); $orderStatus = Mockery::mock(OrderStatus::class); $orderStatus->shouldReceive('is')->andReturn(true); $order->shouldReceive('status')->andReturn($orderStatus); $order->shouldReceive('purchase_units')->andReturn([$purchaseUnit]); $payments = Mockery::mock(Payments::class); $capture = Mockery::mock(Capture::class); $capture->shouldReceive('id')->andReturn('1'); $captureStatus = Mockery::mock(CaptureStatus::class); $captureStatus->shouldReceive('details')->andReturn(null); $captureStatus->shouldReceive('name')->andReturn(CaptureStatus::COMPLETED); $capture->shouldReceive('status')->andReturn($captureStatus); $payments->shouldReceive('captures')->andReturn([$capture]); $purchaseUnit->shouldReceive('payments')->andReturn($payments); $this->orderEndpoint->shouldReceive('create') ->with([$purchaseUnit], 'some_preference', $payer, $token) ->andReturn($order); $this->environment->shouldReceive('current_environment_is')->andReturn(true); $this->config->shouldReceive('has')->andReturn(false); $result = $this->testee->handle_payment($tokenId, $wcOrder, $customer); $this->assertInstanceOf(\WC_Order::class, $result); } }