mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
add tests for ChangeCartEndpoint
This commit is contained in:
parent
8f35569c81
commit
1225ad83c8
3 changed files with 185 additions and 6 deletions
|
@ -45,7 +45,8 @@ return [
|
|||
$shipping = WC()->shipping();
|
||||
$requestData = $container->get('button.request-data');
|
||||
$repository = $container->get('api.repository.cart');
|
||||
return new ChangeCartEndpoint($cart, $shipping, $requestData, $repository);
|
||||
$dataStore = \WC_Data_Store::load('product');
|
||||
return new ChangeCartEndpoint($cart, $shipping, $requestData, $repository, $dataStore);
|
||||
},
|
||||
'button.endpoint.create-order' => static function (ContainerInterface $container): CreateOrderEndpoint {
|
||||
$requestData = $container->get('button.request-data');
|
||||
|
|
|
@ -18,17 +18,20 @@ class ChangeCartEndpoint implements EndpointInterface
|
|||
private $shipping;
|
||||
private $requestData;
|
||||
private $repository;
|
||||
private $productDataStore;
|
||||
public function __construct(
|
||||
\WC_Cart $cart,
|
||||
\WC_Shipping $shipping,
|
||||
RequestData $requestData,
|
||||
CartRepository $repository
|
||||
CartRepository $repository,
|
||||
\WC_Data_Store $productDataStore
|
||||
) {
|
||||
|
||||
$this->cart = $cart;
|
||||
$this->shipping = $shipping;
|
||||
$this->requestData = $requestData;
|
||||
$this->repository = $repository;
|
||||
$this->productDataStore = $productDataStore;
|
||||
}
|
||||
|
||||
public static function nonce(): string
|
||||
|
@ -65,7 +68,7 @@ class ChangeCartEndpoint implements EndpointInterface
|
|||
$success = true;
|
||||
foreach ($products as $product) {
|
||||
$success = $success && (! $product['product']->is_type('variable')) ?
|
||||
$success = $this->addProduct($product['product'], $product['quantity'])
|
||||
$this->addProduct($product['product'], $product['quantity'])
|
||||
: $this->addVariableProduct($product['product'], $product['quantity'], $product['variations']);
|
||||
}
|
||||
if (! $success) {
|
||||
|
@ -113,6 +116,7 @@ class ChangeCartEndpoint implements EndpointInterface
|
|||
}
|
||||
|
||||
$wcProduct = wc_get_product((int) $product['id']);
|
||||
|
||||
if (! $wcProduct) {
|
||||
return null;
|
||||
}
|
||||
|
@ -136,15 +140,15 @@ class ChangeCartEndpoint implements EndpointInterface
|
|||
array $postVariations
|
||||
): bool {
|
||||
|
||||
$variations = [];
|
||||
foreach ($postVariations as $key => $value) {
|
||||
$variations[$value['name']] = $value['value'];
|
||||
}
|
||||
|
||||
$dataStore = \WC_Data_Store::load('product');
|
||||
$variationId = $dataStore->find_matching_product_variation($product, $variations);
|
||||
$variationId = $this->productDataStore->find_matching_product_variation($product, $variations);
|
||||
|
||||
//ToDo: Check stock status for variation.
|
||||
return false !== WC()->cart->add_to_cart($product->get_id(), $quantity, $variationId, $variations);
|
||||
return false !== $this->cart->add_to_cart($product->get_id(), $quantity, $variationId, $variations);
|
||||
}
|
||||
|
||||
private function generatePurchaseUnits(): array
|
||||
|
|
174
tests/PHPUnit/Button/Endpoint/ChangeCartEndpointTest.php
Normal file
174
tests/PHPUnit/Button/Endpoint/ChangeCartEndpointTest.php
Normal file
|
@ -0,0 +1,174 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Inpsyde\PayPalCommerce\Button\Endpoint;
|
||||
|
||||
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
|
||||
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
|
||||
use Inpsyde\PayPalCommerce\TestCase;
|
||||
use Mockery;
|
||||
use function Brain\Monkey\Functions\expect;
|
||||
|
||||
class ChangeCartEndpointTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @dataProvider dataForTestProducts
|
||||
*/
|
||||
public function testProducts($data, $products, $lineItems, $responseExpectation) {
|
||||
|
||||
$dataStore = Mockery::mock(\WC_Data_Store::class);
|
||||
$cart = Mockery::mock(\WC_Cart::class);
|
||||
foreach ($data['products'] as $productKey => $singleProductArray) {
|
||||
expect('wc_get_product')
|
||||
->once()
|
||||
->with($singleProductArray['id'])
|
||||
->andReturn($products[$productKey]);
|
||||
if (! $singleProductArray['__test_data_is_variation']) {
|
||||
$cart
|
||||
->expects('add_to_cart')
|
||||
->with($singleProductArray['id'], $singleProductArray['quantity'])
|
||||
->andReturnTrue();
|
||||
}
|
||||
if ($singleProductArray['__test_data_is_variation']) {
|
||||
$dataStore
|
||||
->expects('find_matching_product_variation')
|
||||
->with($products[$productKey], $singleProductArray['__test_data_variation_map'])
|
||||
->andReturn($singleProductArray['__test_data_variation_id']);
|
||||
$cart
|
||||
->expects('add_to_cart')
|
||||
->with(
|
||||
$singleProductArray['id'],
|
||||
$singleProductArray['quantity'],
|
||||
$singleProductArray['__test_data_variation_id'],
|
||||
$singleProductArray['__test_data_variation_map']
|
||||
)
|
||||
->andReturnTrue();
|
||||
}
|
||||
}
|
||||
$cart
|
||||
->expects('empty_cart')
|
||||
->with(false);
|
||||
$shipping = Mockery::mock(\WC_Shipping::class);
|
||||
$shipping
|
||||
->expects('reset_shipping');
|
||||
$requestData = Mockery::mock(RequestData::class);
|
||||
$requestData
|
||||
->expects('readRequest')
|
||||
->with(ChangeCartEndpoint::nonce())
|
||||
->andReturn($data);
|
||||
$cartRepository = Mockery::mock(CartRepository::class);
|
||||
$cartRepository
|
||||
->expects('all')
|
||||
->andReturn($lineItems);
|
||||
|
||||
$testee = new ChangeCartEndpoint(
|
||||
$cart,
|
||||
$shipping,
|
||||
$requestData,
|
||||
$cartRepository,
|
||||
$dataStore
|
||||
);
|
||||
|
||||
expect('wp_send_json_success')
|
||||
->with($responseExpectation);
|
||||
$this->assertTrue($testee->handleRequest());
|
||||
}
|
||||
|
||||
public function dataForTestProducts() : array {
|
||||
$defaultProduct = Mockery::mock(\WC_Product::class);
|
||||
$defaultProduct
|
||||
->shouldReceive('get_id')
|
||||
->andReturn(1);
|
||||
$defaultProduct
|
||||
->shouldReceive('is_type')
|
||||
->with('variable')
|
||||
->andReturn(false);
|
||||
|
||||
$variationProduct = Mockery::mock(\WC_Product::class);
|
||||
$variationProduct
|
||||
->shouldReceive('get_id')
|
||||
->andReturn(2);
|
||||
$variationProduct
|
||||
->shouldReceive('is_type')
|
||||
->with('variable')
|
||||
->andReturn(true);
|
||||
|
||||
$defaultLineItem = Mockery::mock(PurchaseUnit::class);
|
||||
$defaultLineItem
|
||||
->shouldReceive('toArray')
|
||||
->andReturn([1]);
|
||||
$variationLineItem = Mockery::mock(PurchaseUnit::class);
|
||||
$variationLineItem
|
||||
->shouldReceive('toArray')
|
||||
->andReturn([2]);
|
||||
|
||||
$testData = [
|
||||
'default' => [
|
||||
[
|
||||
'products' => [
|
||||
[
|
||||
'quantity' => 2,
|
||||
'id' => 1,
|
||||
'__test_data_is_variation' => false,
|
||||
],
|
||||
]
|
||||
],
|
||||
[
|
||||
$defaultProduct,
|
||||
],
|
||||
[
|
||||
$defaultLineItem,
|
||||
],
|
||||
[
|
||||
[1],
|
||||
]
|
||||
],
|
||||
'variation' => [
|
||||
[
|
||||
'products' => [
|
||||
[
|
||||
'quantity' => 2,
|
||||
'id' => 1,
|
||||
'__test_data_is_variation' => false,
|
||||
],
|
||||
[
|
||||
'quantity' => 2,
|
||||
'id' => 2,
|
||||
'variations' => [
|
||||
[
|
||||
'name' => 'variation-1',
|
||||
'value' => 'abc',
|
||||
],
|
||||
[
|
||||
'name' => 'variation-2',
|
||||
'value' => 'def',
|
||||
],
|
||||
],
|
||||
'__test_data_is_variation' => true,
|
||||
'__test_data_variation_id' => 123,
|
||||
'__test_data_variation_map' => [
|
||||
'variation-1' => 'abc',
|
||||
'variation-2' => 'def',
|
||||
]
|
||||
],
|
||||
]
|
||||
],
|
||||
[
|
||||
$defaultProduct,
|
||||
$variationProduct,
|
||||
],
|
||||
[
|
||||
$defaultLineItem,
|
||||
$variationLineItem,
|
||||
],
|
||||
[
|
||||
[1],[2]
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
return $testData;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue