Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues 31197 - Loading wrong order tax info #31198

Merged
merged 4 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function getFullTaxInfo()

$taxClassAmount = $this->_taxHelper->getCalculatedTaxes($source);
if (empty($taxClassAmount)) {
$rates = $this->_taxOrderFactory->create()->getCollection()->loadByOrder($source)->toArray();
$rates = $this->_taxOrderFactory->create()->getCollection()->loadByOrder($this->getOrder())->toArray();
$taxClassAmount = $this->_taxCalculation->reproduceProcess($rates['items']);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
*/
declare(strict_types=1);

/**
* Test class for \Magento\Sales\Block\Adminhtml\Order\Totals\TaxTest
*/
namespace Magento\Sales\Test\Unit\Block\Adminhtml\Order\Totals;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
Expand All @@ -16,46 +13,75 @@
use Magento\Sales\Model\Order\Creditmemo;
use Magento\Sales\Model\Order\Invoice;
use Magento\Tax\Helper\Data;
use Magento\Tax\Model\ResourceModel\Sales\Order\Tax\Collection;
use Magento\Tax\Model\Sales\Order\Tax as TaxModel;
use Magento\Tax\Model\Sales\Order\TaxFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Test for \Magento\Sales\Block\Adminhtml\Order\Totals\Tax
*/
class TaxTest extends TestCase
{
/** @var MockObject|Tax */
/**
* @var array
*/
private $calculatedData = [
'tax' => 'tax',
'shipping_tax' => 'shipping_tax',
];

/**
* @var MockObject|Tax
*/
private $taxMock;

/**
* @var Data|MockObject
*/
private $taxHelperMock;

/**
* @var TaxFactory|MockObject
*/
private $taxOrderFactory;

/**
* @inheridoc
*/
protected function setUp(): void
{
$getCalculatedTax = [
'tax' => 'tax',
'shipping_tax' => 'shipping_tax',
];
$taxHelperMock = $this->getMockBuilder(Data::class)
->setMethods(['getCalculatedTaxes'])
$this->taxHelperMock = $this->getMockBuilder(Data::class)
->onlyMethods(['getCalculatedTaxes'])
->disableOriginalConstructor()
->getMock();
$taxHelperMock->expects($this->any())
->method('getCalculatedTaxes')
->willReturn($getCalculatedTax);

$this->taxOrderFactory = $this->createMock(TaxFactory::class);

$arguments = $this->getModelArguments(
['taxHelper' => $this->taxHelperMock, 'taxOrderFactory' => $this->taxOrderFactory]
);
$this->taxMock = $this->getMockBuilder(Tax::class)
->setConstructorArgs($this->_getConstructArguments($taxHelperMock))
->setMethods(['getOrder', 'getSource'])
->setConstructorArgs($arguments)
->onlyMethods(['getOrder', 'getSource'])
->getMock();
}

/**
* Test method for getFullTaxInfo
*
* @param Order $source
* @param array $getCalculatedTax
* @param array $getShippingTax
* @param Order|null $source
* @param array $expectedResult
* @return void
*
* @dataProvider getFullTaxInfoDataProvider
*/
public function testGetFullTaxInfo($source, $expectedResult)
public function testGetFullTaxInfo(?Order $source, array $expectedResult): void
{
$this->taxHelperMock->expects($this->any())
->method('getCalculatedTaxes')
->willReturn($this->calculatedData);
$this->taxMock->expects($this->once())
->method('getOrder')
->willReturn($source);
Expand All @@ -69,13 +95,15 @@ public function testGetFullTaxInfo($source, $expectedResult)
*
* @param Invoice|Creditmemo $source
* @param array $expectedResult
* @return void
*
* @dataProvider getCreditAndInvoiceFullTaxInfoDataProvider
*/
public function testGetFullTaxInfoWithCreditAndInvoice(
$source,
$expectedResult
) {
public function testGetFullTaxInfoWithCreditAndInvoice($source, array $expectedResult): void
{
$this->taxHelperMock->expects($this->any())
->method('getCalculatedTaxes')
->willReturn($this->calculatedData);
$this->taxMock->expects($this->once())
->method('getSource')
->willReturn($source);
Expand All @@ -84,19 +112,57 @@ public function testGetFullTaxInfoWithCreditAndInvoice(
$this->assertSame($expectedResult, $actualResult);
}

/**
* Test method for getFullTaxInfo when order doesn't have tax
*
* @return void
*/
public function testGetFullTaxInfoOrderWithoutTax(): void
{
$this->taxHelperMock->expects($this->once())
->method('getCalculatedTaxes')
->willReturn(null);

$orderMock = $this->createMock(Order::class);
$taxCollection = $this->createMock(Collection::class);
$taxCollection->expects($this->once())
->method('loadByOrder')
->with($orderMock)
->willReturnSelf();
$taxCollection->expects($this->once())
->method('toArray')
->willReturn(['items' => []]);

$taxOrder = $this->createMock(TaxModel::class);
$taxOrder->expects($this->once())
->method('getCollection')
->willReturn($taxCollection);
$this->taxOrderFactory->expects($this->once())
->method('create')
->willReturn($taxOrder);

$invoiceMock = $this->createMock(Invoice::class);
$this->taxMock->expects($this->once())
->method('getSource')
->willReturn($invoiceMock);
$this->taxMock->expects($this->once())
->method('getOrder')
->willReturn($orderMock);

$this->assertNull($this->taxMock->getFullTaxInfo());
}

/**
* Provide the tax helper mock as a constructor argument
*
* @param $taxHelperMock
* @param array $arguments
* @return array
*/
protected function _getConstructArguments($taxHelperMock)
private function getModelArguments(array $arguments): array
{
$objectManagerHelper = new ObjectManager($this);
return $objectManagerHelper->getConstructArguments(
Tax::class,
['taxHelper' => $taxHelperMock]
);

return $objectManagerHelper->getConstructArguments(Tax::class, $arguments);
}

/**
Expand All @@ -106,19 +172,15 @@ protected function _getConstructArguments($taxHelperMock)
*
* @return array
*/
public function getFullTaxInfoDataProvider()
public function getFullTaxInfoDataProvider(): array
{
$salesModelOrderMock = $this->getMockBuilder(Order::class)
->disableOriginalConstructor()
->getMock();
$salesModelOrderMock = $this->createMock(Order::class);

return [
'source is not an instance of \Magento\Sales\Model\Order' => [null, []],
'source is an instance of \Magento\Sales\Model\Order and has reasonable data' => [
$salesModelOrderMock,
[
'tax' => 'tax',
'shipping_tax' => 'shipping_tax',
],
$this->calculatedData,
]
];
}
Expand All @@ -130,22 +192,14 @@ public function getFullTaxInfoDataProvider()
*
* @return array
*/
public function getCreditAndInvoiceFullTaxInfoDataProvider()
public function getCreditAndInvoiceFullTaxInfoDataProvider(): array
{
$invoiceMock = $this->getMockBuilder(Invoice::class)
->disableOriginalConstructor()
->getMock();
$creditMemoMock = $this->getMockBuilder(Creditmemo::class)
->disableOriginalConstructor()
->getMock();
$invoiceMock = $this->createMock(Invoice::class);
$creditMemoMock = $this->createMock(Creditmemo::class);

$expected = [
'tax' => 'tax',
'shipping_tax' => 'shipping_tax',
];
return [
'invoice' => [$invoiceMock, $expected],
'creditMemo' => [$creditMemoMock, $expected]
'invoice' => [$invoiceMock, $this->calculatedData],
'creditMemo' => [$creditMemoMock, $this->calculatedData]
];
}
}