Merge pull request #719 from woocommerce/pcp-443-fix-vaulted-card-button

Fix vaulted card button on order page
This commit is contained in:
Emili Castells 2022-07-08 10:26:53 +02:00 committed by GitHub
commit 4c6b7b9659
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 374 additions and 264 deletions

View file

@ -952,7 +952,7 @@ class OrderEndpointTest extends TestCase
->expects('email_address')
->andReturn('');
$result = $testee->create([$purchaseUnit], $payer);
$result = $testee->create([$purchaseUnit], ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING, $payer);
$this->assertEquals($expectedOrder, $result);
}
@ -1049,7 +1049,7 @@ class OrderEndpointTest extends TestCase
$payerName = Mockery::mock(PayerName::class);
$payer->expects('name')->andReturn($payerName);
$payer->expects('to_array')->andReturn(['payer']);
$result = $testee->create([$purchaseUnit], $payer);
$result = $testee->create([$purchaseUnit], ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE, $payer);
$this->assertEquals($expectedOrder, $result);
}
@ -1141,7 +1141,7 @@ class OrderEndpointTest extends TestCase
$payerName = Mockery::mock(PayerName::class);
$payer->expects('name')->andReturn($payerName);
$payer->expects('to_array')->andReturn(['payer']);
$testee->create([$purchaseUnit], $payer);
$testee->create([$purchaseUnit], ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING, $payer);
}
public function testCreateForPurchaseUnitsIsNot201()
@ -1232,6 +1232,6 @@ class OrderEndpointTest extends TestCase
$payerName = Mockery::mock(PayerName::class);
$payer->expects('name')->andReturn($payerName);
$payer->expects('to_array')->andReturn(['payer']);
$testee->create([$purchaseUnit], $payer);
$testee->create([$purchaseUnit], ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE, $payer);
}
}

View file

@ -0,0 +1,118 @@
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use Mockery;
use WC_Cart;
use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Shipping;
use WooCommerce\PayPalCommerce\TestCase;
class ShippingPreferenceFactoryTest extends TestCase
{
private $testee;
public function setUp(): void
{
parent::setUp();
$this->testee = new ShippingPreferenceFactory();
}
/**
* @dataProvider forStateData
*/
public function testFromState(
PurchaseUnit $purchase_unit,
string $context,
?WC_Cart $cart,
string $funding_source,
string $expected_result
) {
$result = $this->testee->from_state($purchase_unit, $context, $cart, $funding_source);
self::assertEquals($expected_result, $result);
}
public function forStateData()
{
yield [
$this->createPurchaseUnit(true, Mockery::mock(Shipping::class)),
'checkout',
$this->createCart(true),
'',
ApplicationContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS,
];
yield [
$this->createPurchaseUnit(false, Mockery::mock(Shipping::class)),
'checkout',
$this->createCart(false),
'',
ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING,
];
yield [
$this->createPurchaseUnit(true, null),
'checkout',
$this->createCart(true),
'',
ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING,
];
yield [
$this->createPurchaseUnit(true, Mockery::mock(Shipping::class)),
'checkout',
$this->createCart(true),
'card',
ApplicationContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS,
];
yield [
$this->createPurchaseUnit(true, null),
'product',
null,
'',
ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE,
];
yield [
$this->createPurchaseUnit(true, null),
'pay-now',
null,
'venmo',
ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE,
];
yield [
$this->createPurchaseUnit(true, Mockery::mock(Shipping::class)),
'pay-now',
null,
'venmo',
ApplicationContext::SHIPPING_PREFERENCE_GET_FROM_FILE,
];
yield [
$this->createPurchaseUnit(true, Mockery::mock(Shipping::class)),
'pay-now',
null,
'card',
ApplicationContext::SHIPPING_PREFERENCE_SET_PROVIDED_ADDRESS,
];
yield [
$this->createPurchaseUnit(true, null),
'pay-now',
null,
'card',
ApplicationContext::SHIPPING_PREFERENCE_NO_SHIPPING,
];
}
private function createPurchaseUnit(bool $containsPhysicalGoods, ?Shipping $shipping): PurchaseUnit {
$pu = Mockery::mock(PurchaseUnit::class);
$pu->shouldReceive('contains_physical_goods')->andReturn($containsPhysicalGoods);
$pu->shouldReceive('shipping')->andReturn($shipping);
return $pu;
}
private function createCart(bool $needsShipping): WC_Cart {
$cart = Mockery::mock(WC_Cart::class);
$cart->shouldReceive('needs_shipping')->andReturn($needsShipping);
return $cart;
}
}

View file

@ -5,7 +5,7 @@ namespace WooCommerce\PayPalCommerce\Button\Endpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\TestCase;
use Mockery;
use WooCommerce\WooCommerce\Logging\Logger\NullLogger;
@ -17,7 +17,7 @@ class ChangeCartEndpointTest extends TestCase
/**
* @dataProvider dataForTestProducts
*/
public function testProducts($data, $products, $lineItems, $responseExpectation) {
public function testProducts($data, $products, $responseExpectation) {
$dataStore = Mockery::mock(\WC_Data_Store::class);
$cart = Mockery::mock(\WC_Cart::class);
@ -59,16 +59,21 @@ class ChangeCartEndpointTest extends TestCase
->expects('read_request')
->with(ChangeCartEndpoint::nonce())
->andReturn($data);
$cartRepository = Mockery::mock(CartRepository::class);
$cartRepository
->expects('all')
->andReturn($lineItems);
$pu = Mockery::mock(PurchaseUnit::class);
$pu
->shouldReceive('to_array')
->andReturn($responseExpectation[0]);
$purchase_unit_factory = Mockery::mock(PurchaseUnitFactory::class);
$purchase_unit_factory
->expects('from_wc_cart')
->andReturn($pu);
$testee = new ChangeCartEndpoint(
$cart,
$shipping,
$requestData,
$cartRepository,
$purchase_unit_factory,
$dataStore,
new NullLogger()
);
@ -97,15 +102,6 @@ class ChangeCartEndpointTest extends TestCase
->with('variable')
->andReturn(true);
$defaultLineItem = Mockery::mock(PurchaseUnit::class);
$defaultLineItem
->shouldReceive('to_array')
->andReturn([1]);
$variationLineItem = Mockery::mock(PurchaseUnit::class);
$variationLineItem
->shouldReceive('to_array')
->andReturn([2]);
$testData = [
'default' => [
[
@ -120,9 +116,6 @@ class ChangeCartEndpointTest extends TestCase
[
$defaultProduct,
],
[
$defaultLineItem,
],
[
[1],
]
@ -162,11 +155,7 @@ class ChangeCartEndpointTest extends TestCase
$variationProduct,
],
[
$defaultLineItem,
$variationLineItem,
],
[
[1],[2]
[1, 2]
]
]
];

View file

@ -10,7 +10,7 @@ use ReflectionClass;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory;
use WooCommerce\PayPalCommerce\Button\Helper\EarlyOrderHandler;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\TestCase;
@ -145,7 +145,7 @@ class CreateOrderEndpointTest extends TestCase
protected function mockTestee()
{
$request_data = Mockery::mock(RequestData::class);
$cart_repository = Mockery::mock(CartRepository::class);
$shippingPreferenceFactory = Mockery::mock(ShippingPreferenceFactory::class);
$purchase_unit_factory = Mockery::mock(PurchaseUnitFactory::class);
$order_endpoint = Mockery::mock(OrderEndpoint::class);
$payer_factory = Mockery::mock(PayerFactory::class);
@ -155,8 +155,8 @@ class CreateOrderEndpointTest extends TestCase
$testee = new CreateOrderEndpoint(
$request_data,
$cart_repository,
$purchase_unit_factory,
$shippingPreferenceFactory,
$order_endpoint,
$payer_factory,
$session_handler,

View file

@ -16,6 +16,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\TestCase;
use Mockery;
@ -31,6 +32,7 @@ class RenewalHandlerTest extends TestCase
private $repository;
private $orderEndpoint;
private $purchaseUnitFactory;
private $shippingPreferenceFactory;
private $payerFactory;
private $environment;
private $sut;
@ -43,6 +45,7 @@ class RenewalHandlerTest extends TestCase
$this->repository = Mockery::mock(PaymentTokenRepository::class);
$this->orderEndpoint = Mockery::mock(OrderEndpoint::class);
$this->purchaseUnitFactory = Mockery::mock(PurchaseUnitFactory::class);
$this->shippingPreferenceFactory = Mockery::mock(ShippingPreferenceFactory::class);
$this->payerFactory = Mockery::mock(PayerFactory::class);
$this->environment = new Environment(new Dictionary([]));
@ -56,6 +59,7 @@ class RenewalHandlerTest extends TestCase
$this->repository,
$this->orderEndpoint,
$this->purchaseUnitFactory,
$this->shippingPreferenceFactory,
$this->payerFactory,
$this->environment
);
@ -133,8 +137,12 @@ class RenewalHandlerTest extends TestCase
$this->payerFactory->shouldReceive('from_customer')
->andReturn($payer);
$this->shippingPreferenceFactory->shouldReceive('from_state')
->with($purchaseUnit, 'renewal')
->andReturn('no_shipping');
$this->orderEndpoint->shouldReceive('create')
->with([$purchaseUnit], $payer, $token)
->with([$purchaseUnit], 'no_shipping', $payer, $token)
->andReturn($order);
$wcOrder->shouldReceive('update_status');

View file

@ -10,6 +10,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use Psr\Log\NullLogger;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture;
use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
@ -44,6 +45,7 @@ class WcGatewayTest extends TestCase
private $subscriptionHelper;
private $environment;
private $paymentTokenRepository;
private $shipping_preference_factory;
private $logger;
private $paymentsEndpoint;
private $orderEndpoint;
@ -67,6 +69,7 @@ class WcGatewayTest extends TestCase
$this->subscriptionHelper = Mockery::mock(SubscriptionHelper::class);
$this->environment = Mockery::mock(Environment::class);
$this->paymentTokenRepository = Mockery::mock(PaymentTokenRepository::class);
$this->shipping_preference_factory = Mockery::mock(ShippingPreferenceFactory::class);
$this->logger = Mockery::mock(LoggerInterface::class);
$this->paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
$this->orderEndpoint = Mockery::mock(OrderEndpoint::class);
@ -102,6 +105,7 @@ class WcGatewayTest extends TestCase
PayPalGateway::ID,
$this->environment,
$this->paymentTokenRepository,
$this->shipping_preference_factory,
$this->logger,
$this->paymentsEndpoint,
$this->orderEndpoint,