woocommerce-paypal-payments/tests/PHPUnit/ApiClient/Entity/PurchaseUnitTest.php

612 lines
20 KiB
PHP
Raw Normal View History

2020-08-31 13:38:54 +03:00
<?php
declare(strict_types=1);
2020-09-14 07:51:45 +03:00
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
2020-08-31 13:38:54 +03:00
use WooCommerce\PayPalCommerce\ApiClient\Helper\PurchaseUnitSanitizer;
2022-02-09 16:28:27 +02:00
use WooCommerce\PayPalCommerce\TestCase;
2020-08-31 13:38:54 +03:00
use Mockery;
class PurchaseUnitTest extends TestCase
{
public function test()
{
$amount = Mockery::mock(
Amount::class,
[
'breakdown' => null,
2020-09-01 09:47:36 +03:00
'to_array' => ['amount'],
2020-08-31 13:38:54 +03:00
]
);
$item1 = Mockery::mock(
Item::class,
[
2020-09-01 09:47:36 +03:00
'to_array' => ['item1'],
2020-08-31 13:38:54 +03:00
'category' => Item::DIGITAL_GOODS,
]
);
$item2 = Mockery::mock(
Item::class,
[
2020-09-01 09:47:36 +03:00
'to_array' => ['item2'],
2020-08-31 13:38:54 +03:00
'category' => Item::PHYSICAL_GOODS,
]
);
2020-09-01 09:47:36 +03:00
$shipping = Mockery::mock(Shipping::class, ['to_array' => ['shipping']]);
2020-08-31 13:38:54 +03:00
$testee = new PurchaseUnit(
$amount,
[$item1, $item2],
$shipping,
'referenceId',
'description',
null,
'customId',
'invoiceId',
'softDescriptor'
);
$this->assertEquals($amount, $testee->amount());
2020-09-01 09:47:36 +03:00
$this->assertEquals('referenceId', $testee->reference_id());
2020-08-31 13:38:54 +03:00
$this->assertEquals('description', $testee->description());
$this->assertNull($testee->payee());
2020-09-01 09:47:36 +03:00
$this->assertEquals('customId', $testee->custom_id());
$this->assertEquals('invoiceId', $testee->invoice_id());
$this->assertEquals('softDescriptor', $testee->soft_descriptor());
2020-08-31 13:38:54 +03:00
$this->assertEquals($shipping, $testee->shipping());
$this->assertEquals([$item1, $item2], $testee->items());
2020-09-01 09:47:36 +03:00
self::assertTrue($testee->contains_physical_goods());
2020-08-31 13:38:54 +03:00
$expected = [
'reference_id' => 'referenceId',
'amount' => ['amount'],
'description' => 'description',
'items' => [['item1'], ['item2']],
'shipping' => ['shipping'],
'custom_id' => 'customId',
'invoice_id' => 'invoiceId',
'soft_descriptor' => 'softDescriptor',
];
2020-09-01 09:47:36 +03:00
$this->assertEquals($expected, $testee->to_array());
2020-08-31 13:38:54 +03:00
}
/**
* @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)
2020-08-31 13:38:54 +03:00
{
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());
// }
2020-08-31 13:38:54 +03:00
$testee = new PurchaseUnit(
$amount,
$items
);
$testee->set_sanitizer(new PurchaseUnitSanitizer(PurchaseUnitSanitizer::MODE_DITCH));
2020-09-01 09:47:36 +03:00
$array = $testee->to_array();
$resultItems = $doDitchItems === ! array_key_exists('items', $array);
//
// echo "------ RESULT ------\n";
// print_r($array);
// die('.');
$resultBreakdown = $doDitchBreakdown === ! array_key_exists('breakdown', $array['amount']);
2020-08-31 13:38:54 +03:00
$this->assertTrue($resultItems, $message);
$this->assertTrue($resultBreakdown, $message);
foreach ($array['items'] ?? [] as $item) {
$resultTax = $doDitchTax === ! array_key_exists('tax', $item);
$this->assertTrue($resultTax, $message);
}
2020-08-31 13:38:54 +03:00
}
public function dataForDitchTests() : array
{
$data = [
'default' => [
'message' => 'Items should not be ditched.',
'ditch' => false,
'items' => [
[
'value' => 10,
'quantity' => 2,
'tax' => 3,
'category' => Item::PHYSICAL_GOODS,
],
],
'amount' => 26,
'breakdown' => [
2020-09-01 09:47:36 +03:00
'item_total' => 20,
'tax_total' => 6,
2020-08-31 13:38:54 +03:00
'shipping' => null,
'discount' => null,
2020-09-01 09:47:36 +03:00
'shipping_discount' => null,
2020-08-31 13:38:54 +03:00
'handling' => null,
'insurance' => null,
],
],
'dont_ditch_with_discount' => [
'message' => 'Items should not be ditched.',
'ditch' => false,
'items' => [
[
'value' => 10,
'quantity' => 2,
'tax' => 3,
'category' => Item::PHYSICAL_GOODS,
],
],
'amount' => 23,
'breakdown' => [
2020-09-01 09:47:36 +03:00
'item_total' => 20,
'tax_total' => 6,
2020-08-31 13:38:54 +03:00
'shipping' => null,
'discount' => 3,
2020-09-01 09:47:36 +03:00
'shipping_discount' => null,
2020-08-31 13:38:54 +03:00
'handling' => null,
'insurance' => null,
],
],
'ditch_with_discount' => [
'message' => 'Items should be ditched because of discount.',
'ditch' => true,
'items' => [
[
'value' => 10,
'quantity' => 2,
'tax' => 3,
'category' => Item::PHYSICAL_GOODS,
],
],
'amount' => 25,
'breakdown' => [
2020-09-01 09:47:36 +03:00
'item_total' => 20,
'tax_total' => 6,
2020-08-31 13:38:54 +03:00
'shipping' => null,
'discount' => 3,
2020-09-01 09:47:36 +03:00
'shipping_discount' => null,
2020-08-31 13:38:54 +03:00
'handling' => null,
'insurance' => null,
],
],
'dont_ditch_with_shipping_discount' => [
'message' => 'Items should not be ditched.',
'ditch' => false,
'items' => [
[
'value' => 10,
'quantity' => 2,
'tax' => 3,
'category' => Item::PHYSICAL_GOODS,
],
],
'amount' => 23,
'breakdown' => [
2020-09-01 09:47:36 +03:00
'item_total' => 20,
'tax_total' => 6,
2020-08-31 13:38:54 +03:00
'shipping' => null,
'discount' => null,
2020-09-01 09:47:36 +03:00
'shipping_discount' => 3,
2020-08-31 13:38:54 +03:00
'handling' => null,
'insurance' => null,
],
],
'ditch_with_handling' => [
'message' => 'Items should be ditched because of handling.',
'ditch' => true,
'items' => [
[
'value' => 10,
'quantity' => 2,
'tax' => 3,
'category' => Item::PHYSICAL_GOODS,
],
],
'amount' => 26,
'breakdown' => [
2020-09-01 09:47:36 +03:00
'item_total' => 20,
'tax_total' => 6,
2020-08-31 13:38:54 +03:00
'shipping' => null,
'discount' => null,
2020-09-01 09:47:36 +03:00
'shipping_discount' => null,
2020-08-31 13:38:54 +03:00
'handling' => 3,
'insurance' => null,
],
],
'dont_ditch_with_handling' => [
'message' => 'Items should not be ditched.',
'ditch' => false,
'items' => [
[
'value' => 10,
'quantity' => 2,
'tax' => 3,
'category' => Item::PHYSICAL_GOODS,
],
],
'amount' => 29,
'breakdown' => [
2020-09-01 09:47:36 +03:00
'item_total' => 20,
'tax_total' => 6,
2020-08-31 13:38:54 +03:00
'shipping' => null,
'discount' => null,
2020-09-01 09:47:36 +03:00
'shipping_discount' => null,
2020-08-31 13:38:54 +03:00
'handling' => 3,
'insurance' => null,
],
],
'ditch_with_insurance' => [
'message' => 'Items should be ditched because of insurance.',
'ditch' => true,
'items' => [
[
'value' => 10,
'quantity' => 2,
'tax' => 3,
'category' => Item::PHYSICAL_GOODS,
],
],
'amount' => 26,
'breakdown' => [
2020-09-01 09:47:36 +03:00
'item_total' => 20,
'tax_total' => 6,
2020-08-31 13:38:54 +03:00
'shipping' => null,
'discount' => null,
2020-09-01 09:47:36 +03:00
'shipping_discount' => null,
2020-08-31 13:38:54 +03:00
'handling' => null,
'insurance' => 3,
],
],
'dont_ditch_with_insurance' => [
'message' => 'Items should not be ditched.',
'ditch' => false,
'items' => [
[
'value' => 10,
'quantity' => 2,
'tax' => 3,
'category' => Item::PHYSICAL_GOODS,
],
],
'amount' => 29,
'breakdown' => [
2020-09-01 09:47:36 +03:00
'item_total' => 20,
'tax_total' => 6,
2020-08-31 13:38:54 +03:00
'shipping' => null,
'discount' => null,
2020-09-01 09:47:36 +03:00
'shipping_discount' => null,
2020-08-31 13:38:54 +03:00
'handling' => null,
'insurance' => 3,
],
],
'ditch_with_shipping_discount' => [
'message' => 'Items should be ditched because of shipping discount.',
'ditch' => true,
'items' => [
[
'value' => 10,
'quantity' => 2,
'tax' => 3,
'category' => Item::PHYSICAL_GOODS,
],
],
'amount' => 25,
'breakdown' => [
2020-09-01 09:47:36 +03:00
'item_total' => 20,
'tax_total' => 6,
2020-08-31 13:38:54 +03:00
'shipping' => null,
'discount' => null,
2020-09-01 09:47:36 +03:00
'shipping_discount' => 3,
2020-08-31 13:38:54 +03:00
'handling' => null,
'insurance' => null,
],
],
'dont_ditch_with_shipping' => [
'message' => 'Items should not be ditched.',
'ditch' => false,
'items' => [
[
'value' => 10,
'quantity' => 2,
'tax' => 3,
'category' => Item::PHYSICAL_GOODS,
],
],
'amount' => 29,
'breakdown' => [
2020-09-01 09:47:36 +03:00
'item_total' => 20,
'tax_total' => 6,
2020-08-31 13:38:54 +03:00
'shipping' => 3,
'discount' => null,
2020-09-01 09:47:36 +03:00
'shipping_discount' => null,
2020-08-31 13:38:54 +03:00
'handling' => null,
'insurance' => null,
],
],
'ditch_because_shipping' => [
'message' => 'Items should be ditched because of shipping.',
'ditch' => true,
'items' => [
[
'value' => 10,
'quantity' => 2,
'tax' => 3,
'category' => Item::PHYSICAL_GOODS,
],
],
'amount' => 28,
'breakdown' => [
2020-09-01 09:47:36 +03:00
'item_total' => 20,
'tax_total' => 6,
2020-08-31 13:38:54 +03:00
'shipping' => 3,
'discount' => null,
2020-09-01 09:47:36 +03:00
'shipping_discount' => null,
2020-08-31 13:38:54 +03:00
'handling' => null,
'insurance' => null,
],
],
'ditch_items_total' => [
'message' => 'Items should be ditched because the item total does not add up.',
'ditch' => true,
'items' => [
[
'value' => 10,
'quantity' => 2,
'tax' => 3,
'category' => Item::PHYSICAL_GOODS,
],
],
'amount' => 26,
'breakdown' => [
2020-09-01 09:47:36 +03:00
'item_total' => 11,
'tax_total' => 6,
2020-08-31 13:38:54 +03:00
'shipping' => null,
'discount' => null,
2020-09-01 09:47:36 +03:00
'shipping_discount' => null,
2020-08-31 13:38:54 +03:00
'handling' => null,
'insurance' => null,
],
],
'ditch_tax_total' => [
'message' => 'Items should be ditched because the tax total does not add up.',
'ditch' => true,
'items' => [
[
'value' => 10,
'quantity' => 2,
'tax' => 3,
'category' => Item::PHYSICAL_GOODS,
],
],
'amount' => 26,
'breakdown' => [
2020-09-01 09:47:36 +03:00
'item_total' => 20,
'tax_total' => 5,
2020-08-31 13:38:54 +03:00
'shipping' => null,
'discount' => null,
2020-09-01 09:47:36 +03:00
'shipping_discount' => null,
2020-08-31 13:38:54 +03:00
'handling' => null,
'insurance' => null,
],
],
'ditch_total_amount' => [
'message' => 'Items should be ditched because the total amount is way out of order.',
'ditch' => true,
'items' => [
[
'value' => 10,
'quantity' => 2,
'tax' => 3,
'category' => Item::PHYSICAL_GOODS,
],
],
'amount' => 260,
'breakdown' => [
2020-09-01 09:47:36 +03:00
'item_total' => 20,
'tax_total' => 6,
2020-08-31 13:38:54 +03:00
'shipping' => null,
'discount' => null,
2020-09-01 09:47:36 +03:00
'shipping_discount' => null,
2020-08-31 13:38:54 +03:00
'handling' => null,
'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,
],
],
2020-08-31 13:38:54 +03:00
];
$values = [];
foreach ($data as $testKey => $test) {
$items = [];
foreach ($test['items'] as $key => $item) {
$unitAmount = new Money($item['value'], 'EUR');
$tax = new Money($item['tax'], 'EUR');
2020-08-31 13:38:54 +03:00
$items[$key] = Mockery::mock(
Item::class,
[
2020-09-01 09:47:36 +03:00
'unit_amount' => $unitAmount,
2020-08-31 13:38:54 +03:00
'tax' => $tax,
'quantity'=> $item['quantity'],
'category' => $item['category'],
'to_array' => [
'unit_amount' => $unitAmount->to_array(),
'tax' => $tax->to_array(),
'quantity'=> $item['quantity'],
'category' => $item['category'],
],
2020-08-31 13:38:54 +03:00
]
);
}
2020-08-31 13:38:54 +03:00
$breakdown = null;
if ($test['breakdown']) {
$breakdown = Mockery::mock(AmountBreakdown::class);
foreach ($test['breakdown'] as $method => $value) {
$breakdown->shouldReceive($method)->andReturnUsing(function () use ($value) {
if (! is_numeric($value)) {
return null;
}
$money = new Money($value, 'EUR');
2020-08-31 13:38:54 +03:00
return $money;
});
}
$breakdown
->shouldReceive('to_array')
->andReturn(
array_map(
function ($value) {
return $value ? (new Money($value, 'EUR'))->to_array() : null;
},
$test['breakdown']
)
);
2020-08-31 13:38:54 +03:00
}
$amountMoney = new Money($test['amount'], 'EUR');
2020-08-31 13:38:54 +03:00
$amount = Mockery::mock(Amount::class);
$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');
2020-08-31 13:38:54 +03:00
$amount->shouldReceive('breakdown')->andReturn($breakdown);
$values[$testKey] = [
$items,
$amount,
$test['ditch'],
$test['message'],
];
}
return $values;
}
public function testPayee()
{
$amount = Mockery::mock(Amount::class);
$amount->shouldReceive('breakdown')->andReturnNull();
2020-09-01 09:47:36 +03:00
$amount->shouldReceive('to_array')->andReturn(['amount']);
2020-08-31 13:38:54 +03:00
$item1 = Mockery::mock(Item::class);
2020-09-01 09:47:36 +03:00
$item1->shouldReceive('to_array')->andReturn(['item1']);
2020-08-31 13:38:54 +03:00
$item2 = Mockery::mock(Item::class);
2020-09-01 09:47:36 +03:00
$item2->shouldReceive('to_array')->andReturn(['item2']);
2020-08-31 13:38:54 +03:00
$shipping = Mockery::mock(Shipping::class);
2020-09-01 09:47:36 +03:00
$shipping->shouldReceive('to_array')->andReturn(['shipping']);
2020-08-31 13:38:54 +03:00
$payee = Mockery::mock(Payee::class);
2020-09-01 09:47:36 +03:00
$payee->shouldReceive('to_array')->andReturn(['payee']);
2020-08-31 13:38:54 +03:00
$testee = new PurchaseUnit(
$amount,
[],
$shipping,
'referenceId',
'description',
$payee,
'customId',
'invoiceId',
'softDescriptor'
);
$this->assertEquals($payee, $testee->payee());
$expected = [
'reference_id' => 'referenceId',
'amount' => ['amount'],
'description' => 'description',
'items' => [],
'shipping' => ['shipping'],
'custom_id' => 'customId',
'invoice_id' => 'invoiceId',
'soft_descriptor' => 'softDescriptor',
'payee' => ['payee'],
];
2020-09-01 09:47:36 +03:00
$this->assertEquals($expected, $testee->to_array());
2020-08-31 13:38:54 +03:00
}
}