123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Tax\Test\Unit\Block\Item\Price;
- use Magento\Framework\Pricing\Render;
- class RendererTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Tax\Block\Item\Price\Renderer
- */
- protected $renderer;
- /**
- * @var \Magento\Tax\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $taxHelper;
- /**
- * @var \Magento\Framework\Pricing\PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $priceCurrency;
- protected function setUp()
- {
- $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->priceCurrency = $this->getMockBuilder(
- \Magento\Framework\Pricing\PriceCurrencyInterface::class
- )->getMock();
- $this->taxHelper = $this->getMockBuilder(\Magento\Tax\Helper\Data::class)
- ->disableOriginalConstructor()
- ->setMethods([
- 'displayCartPriceExclTax',
- 'displayCartBothPrices',
- 'displayCartPriceInclTax',
- 'displaySalesPriceExclTax',
- 'displaySalesBothPrices',
- 'displaySalesPriceInclTax',
- ])
- ->getMock();
- $this->renderer = $objectManager->getObject(
- \Magento\Tax\Block\Item\Price\Renderer::class,
- [
- 'taxHelper' => $this->taxHelper,
- 'priceCurrency' => $this->priceCurrency,
- 'data' => [
- 'zone' => Render::ZONE_CART,
- ]
- ]
- );
- }
- /**
- * @param $storeId
- * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Sales\Model\Order\Item
- */
- protected function getItemMockWithStoreId($storeId)
- {
- $itemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(['getStoreId', '__wakeup'])
- ->getMock();
- $itemMock->expects($this->once())
- ->method('getStoreId')
- ->will($this->returnValue($storeId));
- return $itemMock;
- }
- /**
- * Test displayPriceInclTax
- *
- * @param string $zone
- * @param string $methodName
- * @dataProvider displayPriceInclTaxDataProvider
- */
- public function testDisplayPriceInclTax($zone, $methodName)
- {
- $storeId = 1;
- $flag = true;
- $itemMock = $this->getItemMockWithStoreId($storeId);
- $this->renderer->setItem($itemMock);
- $this->renderer->setZone($zone);
- $this->taxHelper->expects($this->once())
- ->method($methodName)
- ->with($storeId)
- ->will($this->returnValue($flag));
- $this->assertEquals($flag, $this->renderer->displayPriceInclTax());
- }
- /**
- * @return array
- */
- public function displayPriceInclTaxDataProvider()
- {
- $data = [
- 'cart' => [
- 'zone' => Render::ZONE_CART,
- 'method_name' => 'displayCartPriceInclTax',
- ],
- 'anythingelse' => [
- 'zone' => 'anythingelse',
- 'method_name' => 'displayCartPriceInclTax',
- ],
- 'sale' => [
- 'zone' => Render::ZONE_SALES,
- 'method_name' => 'displaySalesPriceInclTax',
- ],
- 'email' => [
- 'zone' => Render::ZONE_EMAIL,
- 'method_name' => 'displaySalesPriceInclTax',
- ],
- ];
- return $data;
- }
- /**
- * Test displayPriceExclTax
- *
- * @param string $zone
- * @param string $methodName
- * @dataProvider displayPriceExclTaxDataProvider
- */
- public function testDisplayPriceExclTax($zone, $methodName)
- {
- $storeId = 1;
- $flag = true;
- $itemMock = $this->getItemMockWithStoreId($storeId);
- $this->renderer->setItem($itemMock);
- $this->renderer->setZone($zone);
- $this->taxHelper->expects($this->once())
- ->method($methodName)
- ->with($storeId)
- ->will($this->returnValue($flag));
- $this->assertEquals($flag, $this->renderer->displayPriceExclTax());
- }
- /**
- * @return array
- */
- public function displayPriceExclTaxDataProvider()
- {
- $data = [
- 'cart' => [
- 'zone' => Render::ZONE_CART,
- 'method_name' => 'displayCartPriceExclTax',
- ],
- 'anythingelse' => [
- 'zone' => 'anythingelse',
- 'method_name' => 'displayCartPriceExclTax',
- ],
- 'sale' => [
- 'zone' => Render::ZONE_SALES,
- 'method_name' => 'displaySalesPriceExclTax',
- ],
- 'email' => [
- 'zone' => Render::ZONE_EMAIL,
- 'method_name' => 'displaySalesPriceExclTax',
- ],
- ];
- return $data;
- }
- /**
- * Test displayBothPrices
- *
- * @param string $zone
- * @param string $methodName
- * @dataProvider displayBothPricesDataProvider
- */
- public function testDisplayBothPrices($zone, $methodName)
- {
- $storeId = 1;
- $flag = true;
- $itemMock = $this->getItemMockWithStoreId($storeId);
- $this->renderer->setItem($itemMock);
- $this->renderer->setZone($zone);
- $this->taxHelper->expects($this->once())
- ->method($methodName)
- ->with($storeId)
- ->will($this->returnValue($flag));
- $this->assertEquals($flag, $this->renderer->displayBothPrices());
- }
- /**
- * @return array
- */
- public function displayBothPricesDataProvider()
- {
- $data = [
- 'cart' => [
- 'zone' => Render::ZONE_CART,
- 'method_name' => 'displayCartBothPrices',
- ],
- 'anythingelse' => [
- 'zone' => 'anythingelse',
- 'method_name' => 'displayCartBothPrices',
- ],
- 'sale' => [
- 'zone' => Render::ZONE_SALES,
- 'method_name' => 'displaySalesBothPrices',
- ],
- 'email' => [
- 'zone' => Render::ZONE_EMAIL,
- 'method_name' => 'displaySalesBothPrices',
- ],
- ];
- return $data;
- }
- public function testFormatPriceQuoteItem()
- {
- $price = 3.554;
- $formattedPrice = "$3.55";
- $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->setMethods(['formatPrice', '__wakeup'])
- ->getMock();
- $this->priceCurrency->expects($this->once())
- ->method('format')
- ->with($price, true)
- ->will($this->returnValue($formattedPrice));
- $itemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(['getStore', '__wakeup'])
- ->getMock();
- $itemMock->expects($this->once())
- ->method('getStore')
- ->will($this->returnValue($storeMock));
- $this->renderer->setItem($itemMock);
- $this->assertEquals($formattedPrice, $this->renderer->formatPrice($price));
- }
- public function testFormatPriceOrderItem()
- {
- $price = 3.554;
- $formattedPrice = "$3.55";
- $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
- ->disableOriginalConstructor()
- ->getMock();
- $orderMock->expects($this->once())
- ->method('formatPrice')
- ->with($price, false)
- ->will($this->returnValue($formattedPrice));
- $itemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(['getOrder', '__wakeup'])
- ->getMock();
- $itemMock->expects($this->once())
- ->method('getOrder')
- ->will($this->returnValue($orderMock));
- $this->renderer->setItem($itemMock);
- $this->assertEquals($formattedPrice, $this->renderer->formatPrice($price));
- }
- public function testFormatPriceInvoiceItem()
- {
- $price = 3.554;
- $formattedPrice = "$3.55";
- $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
- ->disableOriginalConstructor()
- ->setMethods(['formatPrice', '__wakeup'])
- ->getMock();
- $orderMock->expects($this->once())
- ->method('formatPrice')
- ->with($price, false)
- ->will($this->returnValue($formattedPrice));
- $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(['getOrder', '__wakeup'])
- ->getMock();
- $orderItemMock->expects($this->once())
- ->method('getOrder')
- ->will($this->returnValue($orderMock));
- $invoiceItemMock = $this->getMockBuilder(\Magento\Sales\Model\Invoice\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(['getOrderItem', '__wakeup', 'getStoreId'])
- ->getMock();
- $invoiceItemMock->expects($this->once())
- ->method('getOrderItem')
- ->will($this->returnValue($orderItemMock));
- $this->renderer->setItem($invoiceItemMock);
- $this->assertEquals($formattedPrice, $this->renderer->formatPrice($price));
- }
- public function testGetZone()
- {
- $this->assertEquals(Render::ZONE_CART, $this->renderer->getZone());
- }
- public function testGetStoreId()
- {
- $storeId = 'default';
- $itemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(['getStoreId', '__wakeup'])
- ->getMock();
- $itemMock->expects($this->once())
- ->method('getStoreId')
- ->will($this->returnValue($storeId));
- $this->renderer->setItem($itemMock);
- $this->assertEquals($storeId, $this->renderer->getStoreId());
- }
- public function testGetItemDisplayPriceExclTaxQuoteItem()
- {
- $price = 10;
- /** @var \Magento\Quote\Model\Quote\Item|\PHPUnit_Framework_MockObject_MockObject $quoteItemMock */
- $quoteItemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(['getCalculationPrice', '__wakeup'])
- ->getMock();
- $quoteItemMock->expects($this->once())
- ->method('getCalculationPrice')
- ->will($this->returnValue($price));
- $this->renderer->setItem($quoteItemMock);
- $this->assertEquals($price, $this->renderer->getItemDisplayPriceExclTax());
- }
- public function testGetItemDisplayPriceExclTaxOrderItem()
- {
- $price = 10;
- /** @var \Magento\Sales\Model\Order\Item|\PHPUnit_Framework_MockObject_MockObject $orderItemMock */
- $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(['getPrice', '__wakeup'])
- ->getMock();
- $orderItemMock->expects($this->once())
- ->method('getPrice')
- ->will($this->returnValue($price));
- $this->renderer->setItem($orderItemMock);
- $this->assertEquals($price, $this->renderer->getItemDisplayPriceExclTax());
- }
- public function testGetTotalAmount()
- {
- $rowTotal = 100;
- $taxAmount = 10;
- $discountTaxCompensationAmount = 2;
- $discountAmount = 20;
- $expectedValue = $rowTotal + $taxAmount + $discountTaxCompensationAmount - $discountAmount;
- $itemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(
- [
- 'getRowTotal',
- 'getTaxAmount',
- 'getDiscountTaxCompensationAmount',
- 'getDiscountAmount',
- '__wakeup'
- ]
- )
- ->getMock();
- $itemMock->expects($this->once())
- ->method('getRowTotal')
- ->will($this->returnValue($rowTotal));
- $itemMock->expects($this->once())
- ->method('getTaxAmount')
- ->will($this->returnValue($taxAmount));
- $itemMock->expects($this->once())
- ->method('getDiscountTaxCompensationAmount')
- ->will($this->returnValue($discountTaxCompensationAmount));
- $itemMock->expects($this->once())
- ->method('getDiscountAmount')
- ->will($this->returnValue($discountAmount));
- $this->assertEquals($expectedValue, $this->renderer->getTotalAmount($itemMock));
- }
- public function testGetBaseTotalAmount()
- {
- $baseRowTotal = 100;
- $baseTaxAmount = 10;
- $baseDiscountTaxCompensationAmount = 2;
- $baseDiscountAmount = 20;
- $expectedValue = 92;
- $itemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(
- [
- 'getBaseRowTotal',
- 'getBaseTaxAmount',
- 'getBaseDiscountTaxCompensationAmount',
- 'getBaseDiscountAmount',
- '__wakeup'
- ]
- )
- ->getMock();
- $itemMock->expects($this->once())
- ->method('getBaseRowTotal')
- ->will($this->returnValue($baseRowTotal));
- $itemMock->expects($this->once())
- ->method('getBaseTaxAmount')
- ->will($this->returnValue($baseTaxAmount));
- $itemMock->expects($this->once())
- ->method('getBaseDiscountTaxCompensationAmount')
- ->will($this->returnValue($baseDiscountTaxCompensationAmount));
- $itemMock->expects($this->once())
- ->method('getBaseDiscountAmount')
- ->will($this->returnValue($baseDiscountAmount));
- $this->assertEquals($expectedValue, $this->renderer->getBaseTotalAmount($itemMock));
- }
- }
|