mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Add PurchaseUnitSanitizer tests
Add subtotal mismatch admin options
This commit is contained in:
parent
c15b8ee463
commit
837bdb0392
6 changed files with 314 additions and 50 deletions
|
@ -3,6 +3,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\PurchaseUnitSanitizer;
|
||||
use WooCommerce\PayPalCommerce\TestCase;
|
||||
use Mockery;
|
||||
|
||||
|
@ -75,24 +76,57 @@ class PurchaseUnitTest extends TestCase
|
|||
$this->assertEquals($expected, $testee->to_array());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataForDitchTests
|
||||
* @param array $items
|
||||
* @param Amount $amount
|
||||
* @param bool $doDitch
|
||||
*/
|
||||
public function testDitchMethod(array $items, Amount $amount, bool $doDitch, string $message)
|
||||
/**
|
||||
* @dataProvider dataForDitchTests
|
||||
* @param array $items
|
||||
* @param Amount $amount
|
||||
* @param bool|array $doDitch
|
||||
* @param string $message
|
||||
*/
|
||||
public function testDitchMethod(array $items, Amount $amount, $doDitch, string $message)
|
||||
{
|
||||
if (is_array($doDitch)) {
|
||||
$doDitchItems = $doDitch['items'];
|
||||
$doDitchBreakdown = $doDitch['breakdown'];
|
||||
$doDitchTax = $doDitch['tax'];
|
||||
} else {
|
||||
$doDitchItems = $doDitch;
|
||||
$doDitchBreakdown = $doDitch;
|
||||
$doDitchTax = $doDitch;
|
||||
}
|
||||
|
||||
// $dataSetName = $this->dataName();
|
||||
// if ($dataSetName !== 'dont_ditch_with_discount') {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// print_r($amount->to_array());
|
||||
// foreach ($items as $item) {
|
||||
// print_r($item->to_array());
|
||||
// }
|
||||
|
||||
$testee = new PurchaseUnit(
|
||||
$amount,
|
||||
$items
|
||||
);
|
||||
|
||||
$testee->set_sanitizer(new PurchaseUnitSanitizer(PurchaseUnitSanitizer::MODE_DITCH));
|
||||
|
||||
$array = $testee->to_array();
|
||||
$resultItems = $doDitch === ! array_key_exists('items', $array);
|
||||
$resultBreakdown = $doDitch === ! array_key_exists('breakdown', $array['amount']);
|
||||
$resultItems = $doDitchItems === ! array_key_exists('items', $array);
|
||||
//
|
||||
// echo "------ RESULT ------\n";
|
||||
// print_r($array);
|
||||
// die('.');
|
||||
|
||||
$resultBreakdown = $doDitchBreakdown === ! array_key_exists('breakdown', $array['amount']);
|
||||
$this->assertTrue($resultItems, $message);
|
||||
$this->assertTrue($resultBreakdown, $message);
|
||||
|
||||
foreach ($array['items'] ?? [] as $item) {
|
||||
$resultTax = $doDitchTax === ! array_key_exists('tax', $item);
|
||||
$this->assertTrue($resultTax, $message);
|
||||
}
|
||||
}
|
||||
|
||||
public function dataForDitchTests() : array
|
||||
|
@ -406,6 +440,58 @@ class PurchaseUnitTest extends TestCase
|
|||
'insurance' => null,
|
||||
],
|
||||
],
|
||||
'ditch_items_total_but_not_breakdown' => [
|
||||
'message' => 'Items should be ditched because the item total does not add up. But not breakdown because it adds up.',
|
||||
'ditch' => [
|
||||
'items' => true,
|
||||
'breakdown' => false,
|
||||
'tax' => true,
|
||||
],
|
||||
'items' => [
|
||||
[
|
||||
'value' => 11,
|
||||
'quantity' => 2,
|
||||
'tax' => 3,
|
||||
'category' => Item::PHYSICAL_GOODS,
|
||||
],
|
||||
],
|
||||
'amount' => 26,
|
||||
'breakdown' => [
|
||||
'item_total' => 20,
|
||||
'tax_total' => 6,
|
||||
'shipping' => null,
|
||||
'discount' => null,
|
||||
'shipping_discount' => null,
|
||||
'handling' => null,
|
||||
'insurance' => null,
|
||||
],
|
||||
],
|
||||
'ditch_items_tax_with_incorrect_tax_total' => [
|
||||
'message' => 'Ditch tax from items. Items should not be ditched because the mismatch is on the tax.',
|
||||
'ditch' => [
|
||||
'items' => false,
|
||||
'breakdown' => false,
|
||||
'tax' => true,
|
||||
],
|
||||
'items' => [
|
||||
[
|
||||
'value' => 10,
|
||||
'quantity' => 2,
|
||||
'tax' => 4,
|
||||
'category' => Item::PHYSICAL_GOODS,
|
||||
],
|
||||
],
|
||||
'amount' => 26,
|
||||
'breakdown' => [
|
||||
'item_total' => 20,
|
||||
'tax_total' => 6,
|
||||
'shipping' => null,
|
||||
'discount' => null,
|
||||
'shipping_discount' => null,
|
||||
'handling' => null,
|
||||
'insurance' => null,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$values = [];
|
||||
|
@ -421,10 +507,16 @@ class PurchaseUnitTest extends TestCase
|
|||
'tax' => $tax,
|
||||
'quantity'=> $item['quantity'],
|
||||
'category' => $item['category'],
|
||||
'to_array' => [],
|
||||
'to_array' => [
|
||||
'unit_amount' => $unitAmount->to_array(),
|
||||
'tax' => $tax->to_array(),
|
||||
'quantity'=> $item['quantity'],
|
||||
'category' => $item['category'],
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
$breakdown = null;
|
||||
if ($test['breakdown']) {
|
||||
$breakdown = Mockery::mock(AmountBreakdown::class);
|
||||
|
@ -438,10 +530,29 @@ class PurchaseUnitTest extends TestCase
|
|||
return $money;
|
||||
});
|
||||
}
|
||||
|
||||
$breakdown
|
||||
->shouldReceive('to_array')
|
||||
->andReturn(
|
||||
array_map(
|
||||
function ($value) {
|
||||
return $value ? (new Money($value, 'EUR'))->to_array() : null;
|
||||
},
|
||||
$test['breakdown']
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$amountMoney = new Money($test['amount'], 'EUR');
|
||||
$amount = Mockery::mock(Amount::class);
|
||||
$amount->shouldReceive('to_array')->andReturn(['value' => number_format( $test['amount'], 2, '.', '' ), 'breakdown' => []]);
|
||||
$amount->shouldReceive('value_str')->andReturn(number_format( $test['amount'], 2, '.', '' ));
|
||||
$amount
|
||||
->shouldReceive('to_array')
|
||||
->andReturn([
|
||||
'value' => $amountMoney->value_str(),
|
||||
'currency_code' => $amountMoney->currency_code(),
|
||||
'breakdown' => $breakdown ? $breakdown->to_array() : [],
|
||||
]);
|
||||
$amount->shouldReceive('value_str')->andReturn($amountMoney->value_str());
|
||||
$amount->shouldReceive('currency_code')->andReturn('EUR');
|
||||
$amount->shouldReceive('breakdown')->andReturn($breakdown);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue