123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Tax\Test\Unit\Pricing\Render;
- use Magento\Tax\Pricing\Render\Adjustment;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class AdjustmentTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * Context mock
- *
- * @var \Magento\Framework\View\Element\Template\Context
- */
- protected $contextMock;
- /**
- * Price currency model mock
- *
- * @var \Magento\Directory\Model\PriceCurrency | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $priceCurrencyMock;
- /**
- * Price helper mock
- *
- * @var \Magento\Tax\Helper\Data | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $taxHelperMock;
- /**
- * @var \Magento\Tax\Pricing\Render\Adjustment
- */
- protected $model;
- /**
- * Init mocks and model
- */
- protected function setUp()
- {
- $this->contextMock = $this->createPartialMock(
- \Magento\Framework\View\Element\Template\Context::class,
- ['getEventManager', 'getStoreConfig', 'getScopeConfig']
- );
- $this->priceCurrencyMock = $this->createMock(\Magento\Directory\Model\PriceCurrency::class);
- $this->taxHelperMock = $this->createMock(\Magento\Tax\Helper\Data::class);
- $eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $storeConfigMock = $this->getMockBuilder(\Magento\Store\Model\Store\Config::class)
- ->disableOriginalConstructor()
- ->getMock();
- $scopeConfigMock = $this->getMockForAbstractClass(\Magento\Framework\App\Config\ScopeConfigInterface::class);
- $this->contextMock->expects($this->any())
- ->method('getEventManager')
- ->will($this->returnValue($eventManagerMock));
- $this->contextMock->expects($this->any())
- ->method('getStoreConfig')
- ->will($this->returnValue($storeConfigMock));
- $this->contextMock->expects($this->any())
- ->method('getScopeConfig')
- ->will($this->returnValue($scopeConfigMock));
- $this->model = new Adjustment(
- $this->contextMock,
- $this->priceCurrencyMock,
- $this->taxHelperMock
- );
- }
- /**
- * Test for method getAdjustmentCode
- */
- public function testGetAdjustmentCode()
- {
- $this->assertEquals(\Magento\Tax\Pricing\Adjustment::ADJUSTMENT_CODE, $this->model->getAdjustmentCode());
- }
- /**
- * Test for method getDefaultExclusions
- */
- public function testGetDefaultExclusions()
- {
- $defaultExclusions = $this->model->getDefaultExclusions();
- $this->assertNotEmpty($defaultExclusions, 'Expected to have at least one default exclusion');
- $this->assertContains($this->model->getAdjustmentCode(), $defaultExclusions);
- }
- /**
- * Test for method displayBothPrices
- */
- public function testDisplayBothPrices()
- {
- $shouldDisplayBothPrices = true;
- $this->taxHelperMock->expects($this->once())
- ->method('displayBothPrices')
- ->will($this->returnValue($shouldDisplayBothPrices));
- $this->assertEquals($shouldDisplayBothPrices, $this->model->displayBothPrices());
- }
- /**
- * Test for method getDisplayAmountExclTax
- */
- public function testGetDisplayAmountExclTax()
- {
- $expectedPriceValue = 1.23;
- $expectedPrice = '$4.56';
- /** @var \Magento\Framework\Pricing\Render\Amount $amountRender */
- $amountRender = $this->getMockBuilder(\Magento\Framework\Pricing\Render\Amount::class)
- ->disableOriginalConstructor()
- ->setMethods(['getAmount'])
- ->getMock();
- /** @var \Magento\Framework\Pricing\Amount\Base $baseAmount */
- $baseAmount = $this->getMockBuilder(\Magento\Framework\Pricing\Amount\Base::class)
- ->disableOriginalConstructor()
- ->setMethods(['getValue'])
- ->getMock();
- $baseAmount->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue($expectedPriceValue));
- $amountRender->expects($this->any())
- ->method('getAmount')
- ->will($this->returnValue($baseAmount));
- $this->priceCurrencyMock->expects($this->any())
- ->method('format')
- ->will($this->returnValue($expectedPrice));
- $this->model->render($amountRender);
- $result = $this->model->getDisplayAmountExclTax();
- $this->assertEquals($expectedPrice, $result);
- }
- /**
- * Test for method getDisplayAmount
- *
- * @param bool $includeContainer
- * @dataProvider getDisplayAmountDataProvider
- */
- public function testGetDisplayAmount($includeContainer)
- {
- $expectedPriceValue = 1.23;
- $expectedPrice = '$4.56';
- /** @var \Magento\Framework\Pricing\Render\Amount $amountRender */
- $amountRender = $this->getMockBuilder(\Magento\Framework\Pricing\Render\Amount::class)
- ->disableOriginalConstructor()
- ->setMethods(['getAmount'])
- ->getMock();
- /** @var \Magento\Framework\Pricing\Amount\Base $baseAmount */
- $baseAmount = $this->getMockBuilder(\Magento\Framework\Pricing\Amount\Base::class)
- ->disableOriginalConstructor()
- ->setMethods(['getValue'])
- ->getMock();
- $baseAmount->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue($expectedPriceValue));
- $amountRender->expects($this->any())
- ->method('getAmount')
- ->will($this->returnValue($baseAmount));
- $this->priceCurrencyMock->expects($this->any())
- ->method('format')
- ->with($this->anything(), $this->equalTo($includeContainer))
- ->will($this->returnValue($expectedPrice));
- $this->model->render($amountRender);
- $result = $this->model->getDisplayAmount($includeContainer);
- $this->assertEquals($expectedPrice, $result);
- }
- /**
- * Data provider for testGetDisplayAmount
- *
- * @return array
- */
- public function getDisplayAmountDataProvider()
- {
- return [[true], [false]];
- }
- /**
- * Test for method buildIdWithPrefix
- *
- * @param string $prefix
- * @param null|false|int $saleableId
- * @param null|false|string $suffix
- * @param string $expectedResult
- * @dataProvider buildIdWithPrefixDataProvider
- */
- public function testBuildIdWithPrefix($prefix, $saleableId, $suffix, $expectedResult)
- {
- /** @var \Magento\Framework\Pricing\Render\Amount $amountRender */
- $amountRender = $this->getMockBuilder(\Magento\Framework\Pricing\Render\Amount::class)
- ->disableOriginalConstructor()
- ->setMethods(['getSaleableItem'])
- ->getMock();
- /** @var \Magento\Catalog\Model\Product $saleable */
- $saleable = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
- ->disableOriginalConstructor()
- ->setMethods(['getId', '__wakeup'])
- ->getMock();
- $amountRender->expects($this->any())
- ->method('getSaleableItem')
- ->will($this->returnValue($saleable));
- $saleable->expects($this->any())
- ->method('getId')
- ->will($this->returnValue($saleableId));
- $this->model->setIdSuffix($suffix);
- $this->model->render($amountRender);
- $result = $this->model->buildIdWithPrefix($prefix);
- $this->assertEquals($expectedResult, $result);
- }
- /**
- * data provider for testBuildIdWithPrefix
- *
- * @return array
- */
- public function buildIdWithPrefixDataProvider()
- {
- return [
- ['some_prefix_', null, '_suffix', 'some_prefix__suffix'],
- ['some_prefix_', false, '_suffix', 'some_prefix__suffix'],
- ['some_prefix_', 123, '_suffix', 'some_prefix_123_suffix'],
- ['some_prefix_', 123, null, 'some_prefix_123'],
- ['some_prefix_', 123, false, 'some_prefix_123'],
- ];
- }
- /**
- * test for method displayPriceIncludingTax
- */
- public function testDisplayPriceIncludingTax()
- {
- $expectedResult = true;
- $this->taxHelperMock->expects($this->once())
- ->method('displayPriceIncludingTax')
- ->will($this->returnValue($expectedResult));
- $result = $this->model->displayPriceIncludingTax();
- $this->assertEquals($expectedResult, $result);
- }
- /**
- * test for method displayPriceExcludingTax
- */
- public function testDisplayPriceExcludingTax()
- {
- $expectedResult = true;
- $this->taxHelperMock->expects($this->once())
- ->method('displayPriceExcludingTax')
- ->will($this->returnValue($expectedResult));
- $result = $this->model->displayPriceExcludingTax();
- $this->assertEquals($expectedResult, $result);
- }
- public function testGetHtmlExcluding()
- {
- $arguments = [];
- $displayValue = 8.0;
- $amountRender = $this->getMockForAbstractClass(\Magento\Framework\Pricing\Render\AmountRenderInterface::class);
- $amountMock = $this->getMockForAbstractClass(\Magento\Framework\Pricing\Amount\AmountInterface::class);
- $amountMock->expects($this->once())
- ->method('getValue')
- ->with(\Magento\Tax\Pricing\Adjustment::ADJUSTMENT_CODE)
- ->willReturn($displayValue);
- $this->taxHelperMock->expects($this->once())
- ->method('displayBothPrices')
- ->will($this->returnValue(false));
- $this->taxHelperMock->expects($this->once())
- ->method('displayPriceExcludingTax')
- ->will($this->returnValue(true));
- $amountRender->expects($this->once())
- ->method('setDisplayValue')
- ->with($displayValue);
- $amountRender->expects($this->once())
- ->method('getAmount')
- ->will($this->returnValue($amountMock));
- $this->model->render($amountRender, $arguments);
- }
- public function testGetHtmlBoth()
- {
- $arguments = [];
- $this->model->setZone(\Magento\Framework\Pricing\Render::ZONE_ITEM_VIEW);
- $amountRender = $this->createPartialMock(\Magento\Framework\Pricing\Render\Amount::class, [
- 'setPriceDisplayLabel',
- 'setPriceWrapperCss',
- 'setPriceId',
- 'getSaleableItem'
- ]);
- $product = $this->getMockForAbstractClass(\Magento\Framework\Pricing\SaleableInterface::class);
- $product->expects($this->once())
- ->method('getId');
- $this->taxHelperMock->expects($this->once())
- ->method('displayBothPrices')
- ->will($this->returnValue(true));
- $amountRender->expects($this->once())
- ->method('setPriceDisplayLabel');
- $amountRender->expects($this->once())
- ->method('getSaleableItem')
- ->will($this->returnValue($product));
- $amountRender->expects($this->once())
- ->method('setPriceId');
- $amountRender->expects($this->once())
- ->method('setPriceWrapperCss');
- $this->model->render($amountRender, $arguments);
- }
- }
|