mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-04 08:47:23 +08:00
Merge trunk
This commit is contained in:
commit
933a80b536
27 changed files with 965 additions and 252 deletions
|
@ -443,8 +443,9 @@ class PurchaseUnitTest extends TestCase
|
|||
}
|
||||
}
|
||||
$amount = Mockery::mock(Amount::class);
|
||||
$amount->shouldReceive('to_array')->andReturn(['value' => [], 'breakdown' => []]);
|
||||
$amount->shouldReceive('to_array')->andReturn(['value' => number_format( $test['amount'], 2, '.', '' ), 'breakdown' => []]);
|
||||
$amount->shouldReceive('value')->andReturn($test['amount']);
|
||||
$amount->shouldReceive('currency_code')->andReturn('EUR');
|
||||
$amount->shouldReceive('breakdown')->andReturn($breakdown);
|
||||
|
||||
$values[$testKey] = [
|
||||
|
|
|
@ -35,28 +35,22 @@ class AmountFactoryTest extends TestCase
|
|||
$cart
|
||||
->shouldReceive('get_total')
|
||||
->withAnyArgs()
|
||||
->andReturn(1);
|
||||
->andReturn(13);
|
||||
$cart
|
||||
->shouldReceive('get_cart_contents_total')
|
||||
->andReturn(2);
|
||||
$cart
|
||||
->shouldReceive('get_discount_total')
|
||||
->andReturn(3);
|
||||
$cart
|
||||
->shouldReceive('get_shipping_total')
|
||||
->andReturn(4);
|
||||
$cart
|
||||
->shouldReceive('get_shipping_tax')
|
||||
->shouldReceive('get_subtotal')
|
||||
->andReturn(5);
|
||||
$cart
|
||||
->shouldReceive('get_cart_contents_tax')
|
||||
->andReturn(6);
|
||||
->shouldReceive('get_fee_total')
|
||||
->andReturn(3);
|
||||
$cart
|
||||
->shouldReceive('get_discount_tax')
|
||||
->andReturn(7);
|
||||
->shouldReceive('get_discount_total')
|
||||
->andReturn(1);
|
||||
$cart
|
||||
->shouldReceive('get_subtotal_tax')
|
||||
->andReturn(8);
|
||||
->shouldReceive('get_shipping_total')
|
||||
->andReturn(2);
|
||||
$cart
|
||||
->shouldReceive('get_total_tax')
|
||||
->andReturn(4);
|
||||
|
||||
$woocommerce = Mockery::mock(\WooCommerce::class);
|
||||
$session = Mockery::mock(\WC_Session::class);
|
||||
|
@ -66,46 +60,39 @@ class AmountFactoryTest extends TestCase
|
|||
|
||||
$result = $this->testee->from_wc_cart($cart);
|
||||
$this->assertEquals($this->currency, $result->currency_code());
|
||||
$this->assertEquals((float) 1, $result->value());
|
||||
$this->assertEquals((float) 10, $result->breakdown()->discount()->value());
|
||||
$this->assertEquals((float) 13, $result->value());
|
||||
$this->assertEquals((float) 1, $result->breakdown()->discount()->value());
|
||||
$this->assertEquals($this->currency, $result->breakdown()->discount()->currency_code());
|
||||
$this->assertEquals((float) 9, $result->breakdown()->shipping()->value());
|
||||
$this->assertEquals((float) 2, $result->breakdown()->shipping()->value());
|
||||
$this->assertEquals($this->currency, $result->breakdown()->shipping()->currency_code());
|
||||
$this->assertEquals((float) 5, $result->breakdown()->item_total()->value());
|
||||
$this->assertEquals((float) 8, $result->breakdown()->item_total()->value());
|
||||
$this->assertEquals($this->currency, $result->breakdown()->item_total()->currency_code());
|
||||
$this->assertEquals((float) 8, $result->breakdown()->tax_total()->value());
|
||||
$this->assertEquals((float) 4, $result->breakdown()->tax_total()->value());
|
||||
$this->assertEquals($this->currency, $result->breakdown()->tax_total()->currency_code());
|
||||
}
|
||||
|
||||
public function testFromWcCartNoDiscount()
|
||||
{
|
||||
$expectedTotal = 1;
|
||||
$cart = Mockery::mock(\WC_Cart::class);
|
||||
$cart
|
||||
->shouldReceive('get_total')
|
||||
->withAnyArgs()
|
||||
->andReturn($expectedTotal);
|
||||
$cart
|
||||
->shouldReceive('get_cart_contents_total')
|
||||
->andReturn(2);
|
||||
->andReturn(11);
|
||||
$cart
|
||||
->shouldReceive('get_subtotal')
|
||||
->andReturn(5);
|
||||
$cart
|
||||
->shouldReceive('get_fee_total')
|
||||
->andReturn(0);
|
||||
$cart
|
||||
->shouldReceive('get_discount_total')
|
||||
->andReturn(0);
|
||||
$cart
|
||||
->shouldReceive('get_shipping_total')
|
||||
->andReturn(4);
|
||||
$cart
|
||||
->shouldReceive('get_shipping_tax')
|
||||
->andReturn(5);
|
||||
$cart
|
||||
->shouldReceive('get_cart_contents_tax')
|
||||
->andReturn(6);
|
||||
$cart
|
||||
->shouldReceive('get_discount_tax')
|
||||
->andReturn(0);
|
||||
->andReturn(2);
|
||||
$cart
|
||||
->shouldReceive('get_subtotal_tax')
|
||||
->andReturn(11);
|
||||
->shouldReceive('get_total_tax')
|
||||
->andReturn(4);
|
||||
|
||||
$woocommerce = Mockery::mock(\WooCommerce::class);
|
||||
$session = Mockery::mock(\WC_Session::class);
|
||||
|
@ -114,6 +101,10 @@ class AmountFactoryTest extends TestCase
|
|||
$session->shouldReceive('get')->andReturn([]);
|
||||
$result = $this->testee->from_wc_cart($cart);
|
||||
$this->assertNull($result->breakdown()->discount());
|
||||
$this->assertEquals((float) 11, $result->value());
|
||||
$this->assertEquals((float) 2, $result->breakdown()->shipping()->value());
|
||||
$this->assertEquals((float) 5, $result->breakdown()->item_total()->value());
|
||||
$this->assertEquals((float) 4, $result->breakdown()->tax_total()->value());
|
||||
}
|
||||
|
||||
public function testFromWcOrderDefault()
|
||||
|
@ -123,10 +114,7 @@ class AmountFactoryTest extends TestCase
|
|||
$unitAmount
|
||||
->shouldReceive('value')
|
||||
->andReturn(3);
|
||||
$tax = Mockery::mock(Money::class);
|
||||
$tax
|
||||
->shouldReceive('value')
|
||||
->andReturn(1);
|
||||
|
||||
$item = Mockery::mock(Item::class);
|
||||
$item
|
||||
->shouldReceive('quantity')
|
||||
|
@ -134,9 +122,6 @@ class AmountFactoryTest extends TestCase
|
|||
$item
|
||||
->shouldReceive('unit_amount')
|
||||
->andReturn($unitAmount);
|
||||
$item
|
||||
->shouldReceive('tax')
|
||||
->andReturn($tax);
|
||||
$this->itemFactory
|
||||
->expects('from_wc_order')
|
||||
->with($order)
|
||||
|
@ -148,7 +133,13 @@ class AmountFactoryTest extends TestCase
|
|||
|
||||
$order
|
||||
->shouldReceive('get_total')
|
||||
->andReturn(100);
|
||||
->andReturn(10);
|
||||
$order
|
||||
->shouldReceive('get_subtotal')
|
||||
->andReturn(6);
|
||||
$order
|
||||
->shouldReceive('get_total_fees')
|
||||
->andReturn(4);
|
||||
$order
|
||||
->shouldReceive('get_currency')
|
||||
->andReturn($this->currency);
|
||||
|
@ -156,11 +147,10 @@ class AmountFactoryTest extends TestCase
|
|||
->shouldReceive('get_shipping_total')
|
||||
->andReturn(1);
|
||||
$order
|
||||
->shouldReceive('get_shipping_tax')
|
||||
->andReturn(.5);
|
||||
->shouldReceive('get_total_tax')
|
||||
->andReturn(2);
|
||||
$order
|
||||
->shouldReceive('get_total_discount')
|
||||
->with(false)
|
||||
->andReturn(3);
|
||||
$order
|
||||
->shouldReceive('get_meta')
|
||||
|
@ -168,9 +158,9 @@ class AmountFactoryTest extends TestCase
|
|||
|
||||
$result = $this->testee->from_wc_order($order);
|
||||
$this->assertEquals((float) 3, $result->breakdown()->discount()->value());
|
||||
$this->assertEquals((float) 6, $result->breakdown()->item_total()->value());
|
||||
$this->assertEquals((float) 1.5, $result->breakdown()->shipping()->value());
|
||||
$this->assertEquals((float) 100, $result->value());
|
||||
$this->assertEquals((float) 10, $result->breakdown()->item_total()->value());
|
||||
$this->assertEquals((float) 1, $result->breakdown()->shipping()->value());
|
||||
$this->assertEquals((float) 10, $result->value());
|
||||
$this->assertEquals((float) 2, $result->breakdown()->tax_total()->value());
|
||||
$this->assertEquals($this->currency, $result->breakdown()->discount()->currency_code());
|
||||
$this->assertEquals($this->currency, $result->breakdown()->item_total()->currency_code());
|
||||
|
@ -186,10 +176,6 @@ class AmountFactoryTest extends TestCase
|
|||
$unitAmount
|
||||
->shouldReceive('value')
|
||||
->andReturn(3);
|
||||
$tax = Mockery::mock(Money::class);
|
||||
$tax
|
||||
->shouldReceive('value')
|
||||
->andReturn(1);
|
||||
$item = Mockery::mock(Item::class);
|
||||
$item
|
||||
->shouldReceive('quantity')
|
||||
|
@ -197,9 +183,6 @@ class AmountFactoryTest extends TestCase
|
|||
$item
|
||||
->shouldReceive('unit_amount')
|
||||
->andReturn($unitAmount);
|
||||
$item
|
||||
->shouldReceive('tax')
|
||||
->andReturn($tax);
|
||||
$this->itemFactory
|
||||
->expects('from_wc_order')
|
||||
->with($order)
|
||||
|
@ -212,18 +195,23 @@ class AmountFactoryTest extends TestCase
|
|||
$order
|
||||
->shouldReceive('get_total')
|
||||
->andReturn(100);
|
||||
$order
|
||||
->shouldReceive('get_subtotal')
|
||||
->andReturn(6);
|
||||
$order
|
||||
->shouldReceive('get_total_fees')
|
||||
->andReturn(0);
|
||||
$order
|
||||
->shouldReceive('get_currency')
|
||||
->andReturn($this->currency);
|
||||
$order
|
||||
->shouldReceive('get_shipping_total')
|
||||
->andReturn(1);
|
||||
$order
|
||||
->shouldReceive('get_shipping_tax')
|
||||
->andReturn(.5);
|
||||
$order
|
||||
->shouldReceive('get_total_tax')
|
||||
->andReturn(2);
|
||||
$order
|
||||
->shouldReceive('get_total_discount')
|
||||
->with(false)
|
||||
->andReturn(0);
|
||||
$order
|
||||
->shouldReceive('get_meta')
|
||||
|
|
|
@ -37,7 +37,8 @@ class ItemFactoryTest extends TestCase
|
|||
$items = [
|
||||
[
|
||||
'data' => $product,
|
||||
'quantity' => 1,
|
||||
'quantity' => 2,
|
||||
'line_subtotal' => 84,
|
||||
],
|
||||
];
|
||||
$cart = Mockery::mock(\WC_Cart::class);
|
||||
|
@ -45,12 +46,6 @@ class ItemFactoryTest extends TestCase
|
|||
->expects('get_cart_contents')
|
||||
->andReturn($items);
|
||||
|
||||
expect('wc_get_price_including_tax')
|
||||
->with($product)
|
||||
->andReturn(2.995);
|
||||
expect('wc_get_price_excluding_tax')
|
||||
->with($product)
|
||||
->andReturn(1);
|
||||
expect('wp_strip_all_tags')
|
||||
->with('description')
|
||||
->andReturn('description');
|
||||
|
@ -74,11 +69,10 @@ class ItemFactoryTest extends TestCase
|
|||
*/
|
||||
$this->assertEquals(Item::PHYSICAL_GOODS, $item->category());
|
||||
$this->assertEquals('description', $item->description());
|
||||
$this->assertEquals(1, $item->quantity());
|
||||
$this->assertEquals(2, $item->quantity());
|
||||
$this->assertEquals('name', $item->name());
|
||||
$this->assertEquals('sku', $item->sku());
|
||||
$this->assertEquals(1, $item->unit_amount()->value());
|
||||
$this->assertEquals(2, $item->tax()->value());
|
||||
$this->assertEquals(42, $item->unit_amount()->value());
|
||||
}
|
||||
|
||||
public function testFromCartDigitalGood()
|
||||
|
@ -106,6 +100,7 @@ class ItemFactoryTest extends TestCase
|
|||
[
|
||||
'data' => $product,
|
||||
'quantity' => 1,
|
||||
'line_subtotal' => 42,
|
||||
],
|
||||
];
|
||||
$cart = Mockery::mock(\WC_Cart::class);
|
||||
|
@ -113,12 +108,6 @@ class ItemFactoryTest extends TestCase
|
|||
->expects('get_cart_contents')
|
||||
->andReturn($items);
|
||||
|
||||
expect('wc_get_price_including_tax')
|
||||
->with($product)
|
||||
->andReturn(2.995);
|
||||
expect('wc_get_price_excluding_tax')
|
||||
->with($product)
|
||||
->andReturn(1);
|
||||
expect('wp_strip_all_tags')
|
||||
->with('description')
|
||||
->andReturn('description');
|
||||
|
@ -143,9 +132,6 @@ class ItemFactoryTest extends TestCase
|
|||
$testee = new ItemFactory($this->currency);
|
||||
|
||||
$product = Mockery::mock(\WC_Product::class);
|
||||
$product
|
||||
->expects('get_name')
|
||||
->andReturn('name');
|
||||
$product
|
||||
->expects('get_description')
|
||||
->andReturn('description');
|
||||
|
@ -166,6 +152,9 @@ class ItemFactoryTest extends TestCase
|
|||
$item
|
||||
->expects('get_product')
|
||||
->andReturn($product);
|
||||
$item
|
||||
->expects('get_name')
|
||||
->andReturn('name');
|
||||
$item
|
||||
->expects('get_quantity')
|
||||
->andReturn(1);
|
||||
|
@ -177,10 +166,6 @@ class ItemFactoryTest extends TestCase
|
|||
$order
|
||||
->expects('get_items')
|
||||
->andReturn([$item]);
|
||||
$order
|
||||
->expects('get_item_subtotal')
|
||||
->with($item, true)
|
||||
->andReturn(3);
|
||||
$order
|
||||
->expects('get_item_subtotal')
|
||||
->with($item, false)
|
||||
|
@ -204,7 +189,6 @@ class ItemFactoryTest extends TestCase
|
|||
$this->assertEquals(1, $item->quantity());
|
||||
$this->assertEquals(Item::PHYSICAL_GOODS, $item->category());
|
||||
$this->assertEquals(1, $item->unit_amount()->value());
|
||||
$this->assertEquals(2, $item->tax()->value());
|
||||
}
|
||||
|
||||
public function testFromWcOrderDigitalGood()
|
||||
|
@ -212,9 +196,6 @@ class ItemFactoryTest extends TestCase
|
|||
$testee = new ItemFactory($this->currency);
|
||||
|
||||
$product = Mockery::mock(\WC_Product::class);
|
||||
$product
|
||||
->expects('get_name')
|
||||
->andReturn('name');
|
||||
$product
|
||||
->expects('get_description')
|
||||
->andReturn('description');
|
||||
|
@ -236,6 +217,9 @@ class ItemFactoryTest extends TestCase
|
|||
$item
|
||||
->expects('get_product')
|
||||
->andReturn($product);
|
||||
$item
|
||||
->expects('get_name')
|
||||
->andReturn('name');
|
||||
$item
|
||||
->expects('get_quantity')
|
||||
->andReturn(1);
|
||||
|
@ -247,10 +231,6 @@ class ItemFactoryTest extends TestCase
|
|||
$order
|
||||
->expects('get_items')
|
||||
->andReturn([$item]);
|
||||
$order
|
||||
->expects('get_item_subtotal')
|
||||
->with($item, true)
|
||||
->andReturn(3);
|
||||
$order
|
||||
->expects('get_item_subtotal')
|
||||
->with($item, false)
|
||||
|
@ -277,9 +257,6 @@ class ItemFactoryTest extends TestCase
|
|||
$testee = new ItemFactory($this->currency);
|
||||
|
||||
$product = Mockery::mock(\WC_Product::class);
|
||||
$product
|
||||
->expects('get_name')
|
||||
->andReturn($name);
|
||||
$product
|
||||
->expects('get_description')
|
||||
->andReturn($description);
|
||||
|
@ -301,6 +278,9 @@ class ItemFactoryTest extends TestCase
|
|||
$item
|
||||
->expects('get_product')
|
||||
->andReturn($product);
|
||||
$item
|
||||
->expects('get_name')
|
||||
->andReturn($name);
|
||||
$item
|
||||
->expects('get_quantity')
|
||||
->andReturn(1);
|
||||
|
@ -312,10 +292,6 @@ class ItemFactoryTest extends TestCase
|
|||
$order
|
||||
->expects('get_items')
|
||||
->andReturn([$item]);
|
||||
$order
|
||||
->expects('get_item_subtotal')
|
||||
->with($item, true)
|
||||
->andReturn(3);
|
||||
$order
|
||||
->expects('get_item_subtotal')
|
||||
->with($item, false)
|
||||
|
|
412
tests/e2e/PHPUnit/Order/PurchaseUnitTest.php
Normal file
412
tests/e2e/PHPUnit/Order/PurchaseUnitTest.php
Normal file
|
@ -0,0 +1,412 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Tests\E2e\Order;
|
||||
|
||||
use Exception;
|
||||
use WC_Cart;
|
||||
use WC_Coupon;
|
||||
use WC_Customer;
|
||||
use WC_Order;
|
||||
use WC_Order_Item_Fee;
|
||||
use WC_Order_Item_Product;
|
||||
use WC_Order_Item_Shipping;
|
||||
use WC_Product;
|
||||
use WC_Product_Simple;
|
||||
use WC_Session;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
|
||||
use WooCommerce\PayPalCommerce\Tests\E2e\TestCase;
|
||||
|
||||
class PurchaseUnitTest extends TestCase
|
||||
{
|
||||
protected $postIds = [];
|
||||
|
||||
protected $container;
|
||||
|
||||
protected $cart;
|
||||
protected $customer;
|
||||
protected $session;
|
||||
|
||||
/**
|
||||
* @var PurchaseUnitFactory
|
||||
*/
|
||||
private $puFactory;
|
||||
|
||||
const CURRENCY = 'EUR';
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->container = $this->getContainer();
|
||||
$this->cart = $this->cart();
|
||||
$this->customer = $this->customer();
|
||||
$this->session = $this->session();
|
||||
|
||||
$this->puFactory = $this->container->get( 'api.factory.purchase-unit' );
|
||||
assert($this->puFactory instanceof PurchaseUnitFactory);
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
foreach ($this->postIds as $id) {
|
||||
wp_delete_post($id);
|
||||
}
|
||||
|
||||
$this->cart->empty_cart();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider orderData
|
||||
*/
|
||||
public function testOrder(array $orderData, array $expectedAmount)
|
||||
{
|
||||
$wcOrder = $this->createWcOrder($orderData);
|
||||
|
||||
$pu = $this->puFactory->from_wc_order($wcOrder);
|
||||
$puData = $pu->to_array();
|
||||
|
||||
self::assertTrue(isset($puData['amount']['breakdown']));
|
||||
|
||||
self::assertEquals($expectedAmount, $puData['amount']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider cartData
|
||||
*/
|
||||
public function testCart(array $cartData, array $expectedAmount)
|
||||
{
|
||||
$this->fillWcCart($this->cart, $this->customer, $this->session, $cartData);
|
||||
|
||||
$pu = $this->puFactory->from_wc_cart($this->cart);
|
||||
$puData = $pu->to_array();
|
||||
|
||||
self::assertTrue(isset($puData['amount']['breakdown']));
|
||||
|
||||
self::assertEquals($expectedAmount, $puData['amount']);
|
||||
}
|
||||
|
||||
protected function createWcOrder(array $data): WC_Order {
|
||||
$wcOrder = new WC_Order();
|
||||
$wcOrder->set_currency( $data['currency'] ?? self::CURRENCY);
|
||||
$wcOrder->set_prices_include_tax($data['prices_include_tax'] ?? true);
|
||||
|
||||
foreach ($data['items'] as $itemData) {
|
||||
$item = new WC_Order_Item_Product();
|
||||
$item->set_name($itemData['name'] ?? 'Test product');
|
||||
$item->set_quantity($itemData['quantity'] ?? 1);
|
||||
$item->set_total((string) ($itemData['price'] * $itemData['quantity'] ?? 1));
|
||||
if (isset($itemData['product'])) {
|
||||
$product = $this->createWcProduct($itemData['product']);
|
||||
$item->set_product($product);
|
||||
}
|
||||
$wcOrder->add_item($item);
|
||||
}
|
||||
|
||||
$wcOrder->set_address(array_merge([
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'company' => '',
|
||||
'email' => 'jd@example.com',
|
||||
'phone' => '1234567890',
|
||||
'address_1' => '123 st',
|
||||
'address_2' => '',
|
||||
'city' => 'city0',
|
||||
'state' => 'state0',
|
||||
'country' => 'AQ',
|
||||
'postcode' => '12345',
|
||||
], $data['billing'] ?? []));
|
||||
|
||||
if (isset($data['shipping'])) {
|
||||
$shipping = new WC_Order_Item_Shipping();
|
||||
$shipping->set_total((string) $data['shipping']['total']);
|
||||
$wcOrder->add_item($shipping);
|
||||
}
|
||||
|
||||
foreach ($data['fees'] ?? [] as $ind => $feeData) {
|
||||
$fee = new WC_Order_Item_Fee();
|
||||
$fee->set_name("Test fee $ind");
|
||||
$fee->set_amount((string) $feeData['amount']);
|
||||
$fee->set_total((string) $feeData['amount']);
|
||||
$fee->set_tax_class('');
|
||||
$fee->set_tax_status('taxable');
|
||||
|
||||
$wcOrder->add_item($fee);
|
||||
}
|
||||
|
||||
$wcOrder->calculate_totals();
|
||||
$wcOrder->save();
|
||||
|
||||
$this->postIds[] = $wcOrder->get_id();
|
||||
|
||||
foreach ($data['coupons'] ?? [] as $couponData) {
|
||||
$coupon = $this->createWcCoupon($couponData);
|
||||
|
||||
$ret = $wcOrder->apply_coupon($coupon);
|
||||
if (is_wp_error($ret)) {
|
||||
throw new Exception('Incorrect coupon. ' . $ret->get_error_message());
|
||||
}
|
||||
}
|
||||
|
||||
$wcOrder->calculate_totals();
|
||||
$wcOrder->save();
|
||||
|
||||
return $wcOrder;
|
||||
}
|
||||
|
||||
protected function fillWcCart(WC_Cart $cart, WC_Customer $customer, WC_Session $session, array $data): void {
|
||||
$cart->empty_cart();
|
||||
|
||||
foreach ($data['products'] as $productData) {
|
||||
$product = $this->createWcProduct($productData);
|
||||
|
||||
$cart->add_to_cart($product->get_id(), $productData['quantity'] ?? 1);
|
||||
}
|
||||
|
||||
foreach ($data['coupons'] ?? [] as $couponData) {
|
||||
$coupon = $this->createWcCoupon($couponData);
|
||||
|
||||
$cart->apply_coupon($coupon->get_code());
|
||||
}
|
||||
|
||||
$customer->set_billing_country($data['billing']['country'] ?? 'AQ');
|
||||
if (isset($data['billing']['city'])) {
|
||||
$customer->set_billing_city($data['billing']['city']);
|
||||
}
|
||||
|
||||
$cart->calculate_totals();
|
||||
}
|
||||
|
||||
protected function createWcCoupon(array $data): WC_Coupon {
|
||||
$coupon = new WC_Coupon();
|
||||
$coupon->set_amount($data['amount']);
|
||||
$coupon->set_discount_type($data['type']);
|
||||
$coupon->set_code(uniqid());
|
||||
$coupon->set_virtual(true);
|
||||
$coupon->save();
|
||||
|
||||
$this->postIds[] = $coupon->get_id();
|
||||
|
||||
return $coupon;
|
||||
}
|
||||
|
||||
protected function createWcProduct(array $data): WC_Product {
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name('Test product ' . rand());
|
||||
$product->set_status( 'publish');
|
||||
$product->set_regular_price((string) $data['price']);
|
||||
$product->set_tax_status('taxable');
|
||||
$product->set_tax_class('');
|
||||
|
||||
$product->save();
|
||||
|
||||
$this->postIds[] = $product->get_id();
|
||||
|
||||
return $product;
|
||||
}
|
||||
|
||||
public function orderData() {
|
||||
yield [
|
||||
[
|
||||
'items' => [
|
||||
['price' => 11.99, 'quantity' => 1],
|
||||
],
|
||||
'shipping' => ['total' => 4.99],
|
||||
'billing' => ['city' => 'city1'],
|
||||
],
|
||||
self::adaptAmountFormat([
|
||||
'value' => 18.44,
|
||||
'breakdown' => [
|
||||
'item_total' => 11.99,
|
||||
'tax_total' => 1.46,
|
||||
'shipping' => 4.99,
|
||||
],
|
||||
]),
|
||||
];
|
||||
yield [
|
||||
[
|
||||
'items' => [
|
||||
['price' => 11.99, 'quantity' => 3],
|
||||
],
|
||||
'shipping' => ['total' => 4.99],
|
||||
'billing' => ['city' => 'city1'],
|
||||
],
|
||||
self::adaptAmountFormat([
|
||||
'value' => 44.49,
|
||||
'breakdown' => [
|
||||
'item_total' => 35.97,
|
||||
'tax_total' => 3.53,
|
||||
'shipping' => 4.99,
|
||||
],
|
||||
]),
|
||||
];
|
||||
yield [
|
||||
[
|
||||
'items' => [
|
||||
['price' => 18.0, 'quantity' => 1],
|
||||
],
|
||||
'shipping' => ['total' => 4.99],
|
||||
'billing' => ['city' => 'city1'],
|
||||
],
|
||||
self::adaptAmountFormat([
|
||||
'value' => 24.97,
|
||||
'breakdown' => [
|
||||
'item_total' => 18.0,
|
||||
'tax_total' => 1.98,
|
||||
'shipping' => 4.99,
|
||||
],
|
||||
]),
|
||||
];
|
||||
yield [
|
||||
[
|
||||
'items' => [
|
||||
['price' => 18.0, 'quantity' => 3],
|
||||
],
|
||||
'shipping' => ['total' => 4.99],
|
||||
'billing' => ['city' => 'city1'],
|
||||
],
|
||||
self::adaptAmountFormat([
|
||||
'value' => 64.08,
|
||||
'breakdown' => [
|
||||
'item_total' => 54.0,
|
||||
'tax_total' => 5.09,
|
||||
'shipping' => 4.99,
|
||||
],
|
||||
]),
|
||||
];
|
||||
yield [
|
||||
[
|
||||
'items' => [
|
||||
['price' => 11.25, 'quantity' => 3],
|
||||
],
|
||||
'billing' => ['city' => 'city2'],
|
||||
],
|
||||
self::adaptAmountFormat([
|
||||
'value' => 53.99,
|
||||
'breakdown' => [
|
||||
'item_total' => 33.75,
|
||||
'tax_total' => 20.24,
|
||||
'shipping' => 0.0,
|
||||
],
|
||||
]),
|
||||
];
|
||||
yield [
|
||||
[
|
||||
'items' => [
|
||||
['price' => 11.99, 'quantity' => 3, 'product' => ['price' => 11.99]],
|
||||
],
|
||||
'shipping' => ['total' => 4.99],
|
||||
'billing' => ['city' => 'city1'],
|
||||
'coupons' => [
|
||||
['amount' => 2.39, 'type' => 'fixed_cart'],
|
||||
['amount' => 7.33, 'type' => 'percent'],
|
||||
],
|
||||
],
|
||||
self::adaptAmountFormat([
|
||||
'value' => 39.25,
|
||||
'breakdown' => [
|
||||
'item_total' => 35.97,
|
||||
'tax_total' => 3.12,
|
||||
'shipping' => 4.99,
|
||||
'discount' => 4.83,
|
||||
],
|
||||
]),
|
||||
];
|
||||
yield [
|
||||
[
|
||||
'items' => [
|
||||
['price' => 5.99, 'quantity' => 1],
|
||||
],
|
||||
'billing' => ['city' => 'city1'],
|
||||
'fees' => [
|
||||
['amount' => 2.89],
|
||||
['amount' => 7.13],
|
||||
]
|
||||
],
|
||||
self::adaptAmountFormat([
|
||||
'value' => 17.39,
|
||||
'breakdown' => [
|
||||
'item_total' => 16.01,
|
||||
'tax_total' => 1.38,
|
||||
'shipping' => 0.0,
|
||||
],
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public function cartData() {
|
||||
yield [
|
||||
[
|
||||
'products' => [
|
||||
['price' => 11.99, 'quantity' => 3],
|
||||
],
|
||||
'billing' => ['city' => 'city1'],
|
||||
],
|
||||
self::adaptAmountFormat([
|
||||
'value' => 39.07,
|
||||
'breakdown' => [
|
||||
'item_total' => 35.97,
|
||||
'tax_total' => 3.10,
|
||||
'shipping' => 0.00,
|
||||
],
|
||||
], get_woocommerce_currency()),
|
||||
];
|
||||
yield [
|
||||
[
|
||||
'products' => [
|
||||
['price' => 11.25, 'quantity' => 3],
|
||||
],
|
||||
'billing' => ['city' => 'city2'],
|
||||
],
|
||||
self::adaptAmountFormat([
|
||||
'value' => 53.99,
|
||||
'breakdown' => [
|
||||
'item_total' => 33.75,
|
||||
'tax_total' => 20.24,
|
||||
'shipping' => 0.0,
|
||||
],
|
||||
], get_woocommerce_currency()),
|
||||
];
|
||||
yield [
|
||||
[
|
||||
'products' => [
|
||||
['price' => 11.99, 'quantity' => 3],
|
||||
],
|
||||
'billing' => ['city' => 'city1'],
|
||||
'coupons' => [
|
||||
['amount' => 2.39, 'type' => 'fixed_cart'],
|
||||
['amount' => 7.33, 'type' => 'percent'],
|
||||
],
|
||||
],
|
||||
self::adaptAmountFormat([
|
||||
'value' => 33.83,
|
||||
'breakdown' => [
|
||||
'item_total' => 35.97,
|
||||
'tax_total' => 2.69,
|
||||
'shipping' => 0.00,
|
||||
'discount' => 4.83,
|
||||
],
|
||||
], get_woocommerce_currency()),
|
||||
];
|
||||
}
|
||||
|
||||
private static function adaptAmountFormat(array $data, string $currency = null): array {
|
||||
if (!$currency) {
|
||||
$currency = self::CURRENCY;
|
||||
}
|
||||
|
||||
$data['currency_code'] = $currency;
|
||||
if (isset($data['breakdown'])) {
|
||||
foreach ($data['breakdown'] as $key => $value) {
|
||||
$data['breakdown'][$key] = [
|
||||
'currency_code' => $currency,
|
||||
'value' => $value,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
31
tests/e2e/PHPUnit/TestCase.php
Normal file
31
tests/e2e/PHPUnit/TestCase.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Tests\E2e;
|
||||
|
||||
use PPCP_E2E;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use WC_Cart;
|
||||
use WC_Customer;
|
||||
use WC_Session;
|
||||
|
||||
class TestCase extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
protected $container;
|
||||
|
||||
protected function getContainer(): ContainerInterface {
|
||||
return PPCP_E2E::$container;
|
||||
}
|
||||
|
||||
protected function cart(): WC_Cart {
|
||||
return WC()->cart;
|
||||
}
|
||||
|
||||
protected function customer(): WC_Customer {
|
||||
return WC()->customer;
|
||||
}
|
||||
|
||||
protected function session(): WC_Session {
|
||||
return WC()->session;
|
||||
}
|
||||
}
|
23
tests/e2e/PHPUnit/bootstrap.php
Normal file
23
tests/e2e/PHPUnit/bootstrap.php
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
define('E2E_TESTS_ROOT_DIR', dirname(__DIR__));
|
||||
define('ROOT_DIR', dirname(dirname(E2E_TESTS_ROOT_DIR)));
|
||||
|
||||
require_once ROOT_DIR . '/vendor/autoload.php';
|
||||
|
||||
if (file_exists(ROOT_DIR . '/.env.e2e')) {
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(ROOT_DIR, '.env.e2e');
|
||||
$dotenv->load();
|
||||
}
|
||||
|
||||
if (!isset($_ENV['PPCP_E2E_WP_DIR'])) {
|
||||
exit('Copy .env.e2e.example to .env.e2e or define the environment variables.' . PHP_EOL);
|
||||
}
|
||||
$wpRootDir = str_replace('${ROOT_DIR}', ROOT_DIR, $_ENV['PPCP_E2E_WP_DIR']);
|
||||
|
||||
define('WP_ROOT_DIR', $wpRootDir);
|
||||
|
||||
$_SERVER['HTTP_HOST'] = ''; // just to avoid a warning
|
||||
|
||||
require_once WP_ROOT_DIR . '/wp-load.php';
|
19
tests/e2e/PHPUnit/ppcp-e2e-plugin.php
Normal file
19
tests/e2e/PHPUnit/ppcp-e2e-plugin.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
/**
|
||||
* Plugin Name: WooCommerce PayPal Payments e2e
|
||||
* Description: PPCP e2e
|
||||
* Version: 1.0.0
|
||||
* Author: Inpsyde
|
||||
* License: GPL-2.0
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
class PPCP_E2E
|
||||
{
|
||||
public static $container;
|
||||
}
|
||||
|
||||
add_filter('woocommerce_paypal_payments_built_container', function($app_container): void {
|
||||
PPCP_E2E::$container = $app_container;
|
||||
});
|
49
tests/e2e/PHPUnit/setup.php
Normal file
49
tests/e2e/PHPUnit/setup.php
Normal file
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
$options = [
|
||||
'woocommerce_calc_taxes' => 'yes',
|
||||
'woocommerce_prices_include_tax' => 'yes',
|
||||
'woocommerce_tax_based_on' => 'billing',
|
||||
'woocommerce_shipping_tax_class' => 'inherit',
|
||||
'woocommerce_tax_round_at_subtotal' => 'no',
|
||||
];
|
||||
|
||||
foreach ($options as $key => $value) {
|
||||
echo "Setting $key to $value." . PHP_EOL;
|
||||
update_option($key, $value);
|
||||
}
|
||||
|
||||
echo 'Adding ppcp-e2e-plugin.' . PHP_EOL;
|
||||
|
||||
$pluginDir = WP_ROOT_DIR . '/wp-content/plugins/ppcp-e2e-plugin';
|
||||
if (!is_dir($pluginDir)) {
|
||||
mkdir($pluginDir);
|
||||
}
|
||||
if (!copy(E2E_TESTS_ROOT_DIR . '/PHPUnit/ppcp-e2e-plugin.php', $pluginDir . '/ppcp-e2e-plugin.php')) {
|
||||
echo 'Failed to copy ppcp-e2e-plugin.' . PHP_EOL;
|
||||
}
|
||||
|
||||
activate_plugin('ppcp-e2e-plugin/ppcp-e2e-plugin.php', '', true);
|
||||
|
||||
echo 'Deleting test taxes.' . PHP_EOL;
|
||||
|
||||
$taxRates = WC_Tax::get_rates_for_tax_class('');
|
||||
$testTaxRates = array_filter($taxRates, function ($taxRate): bool {
|
||||
return str_contains($taxRate->tax_rate_name, '[PPCP TEST]');
|
||||
});
|
||||
foreach ($testTaxRates as $rate) {
|
||||
WC_Tax::_delete_tax_rate($rate->tax_rate_id);
|
||||
}
|
||||
|
||||
echo 'Importing test taxes.' . PHP_EOL;
|
||||
|
||||
require WP_ROOT_DIR . '/wp-admin/includes/class-wp-importer.php';
|
||||
require WP_ROOT_DIR . '/wp-content/plugins/woocommerce/includes/admin/importers/class-wc-tax-rate-importer.php';
|
||||
|
||||
$taxImporter = new WC_Tax_Rate_Importer();
|
||||
$taxImporter->import(E2E_TESTS_ROOT_DIR . '/data/tax_rates.csv');
|
||||
|
||||
echo PHP_EOL;
|
4
tests/e2e/data/tax_rates.csv
Normal file
4
tests/e2e/data/tax_rates.csv
Normal file
|
@ -0,0 +1,4 @@
|
|||
Country code,State code,Postcode / ZIP,City,Rate %,Tax name,Priority,Compound,Shipping,Tax class
|
||||
AQ,,,CITY1,8.625,[PPCP TEST] Tax 1,1,0,1,
|
||||
AQ,,,CITY2,10,[PPCP TEST] Tax 2.1,1,0,1,
|
||||
AQ,,,CITY2,50,[PPCP TEST] Tax 2.2,2,0,1,
|
|
15
tests/e2e/phpunit.xml.dist
Normal file
15
tests/e2e/phpunit.xml.dist
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
bootstrap="PHPUnit/bootstrap.php"
|
||||
backupGlobals="false"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="unit">
|
||||
<directory suffix="Test.php">./PHPUnit</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
Loading…
Add table
Add a link
Reference in a new issue