AdjustmentTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Test\Unit\Pricing\Render;
  7. use Magento\Tax\Pricing\Render\Adjustment;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class AdjustmentTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * Context mock
  15. *
  16. * @var \Magento\Framework\View\Element\Template\Context
  17. */
  18. protected $contextMock;
  19. /**
  20. * Price currency model mock
  21. *
  22. * @var \Magento\Directory\Model\PriceCurrency | \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $priceCurrencyMock;
  25. /**
  26. * Price helper mock
  27. *
  28. * @var \Magento\Tax\Helper\Data | \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $taxHelperMock;
  31. /**
  32. * @var \Magento\Tax\Pricing\Render\Adjustment
  33. */
  34. protected $model;
  35. /**
  36. * Init mocks and model
  37. */
  38. protected function setUp()
  39. {
  40. $this->contextMock = $this->createPartialMock(
  41. \Magento\Framework\View\Element\Template\Context::class,
  42. ['getEventManager', 'getStoreConfig', 'getScopeConfig']
  43. );
  44. $this->priceCurrencyMock = $this->createMock(\Magento\Directory\Model\PriceCurrency::class);
  45. $this->taxHelperMock = $this->createMock(\Magento\Tax\Helper\Data::class);
  46. $eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
  47. ->disableOriginalConstructor()
  48. ->getMockForAbstractClass();
  49. $storeConfigMock = $this->getMockBuilder(\Magento\Store\Model\Store\Config::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $scopeConfigMock = $this->getMockForAbstractClass(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  53. $this->contextMock->expects($this->any())
  54. ->method('getEventManager')
  55. ->will($this->returnValue($eventManagerMock));
  56. $this->contextMock->expects($this->any())
  57. ->method('getStoreConfig')
  58. ->will($this->returnValue($storeConfigMock));
  59. $this->contextMock->expects($this->any())
  60. ->method('getScopeConfig')
  61. ->will($this->returnValue($scopeConfigMock));
  62. $this->model = new Adjustment(
  63. $this->contextMock,
  64. $this->priceCurrencyMock,
  65. $this->taxHelperMock
  66. );
  67. }
  68. /**
  69. * Test for method getAdjustmentCode
  70. */
  71. public function testGetAdjustmentCode()
  72. {
  73. $this->assertEquals(\Magento\Tax\Pricing\Adjustment::ADJUSTMENT_CODE, $this->model->getAdjustmentCode());
  74. }
  75. /**
  76. * Test for method getDefaultExclusions
  77. */
  78. public function testGetDefaultExclusions()
  79. {
  80. $defaultExclusions = $this->model->getDefaultExclusions();
  81. $this->assertNotEmpty($defaultExclusions, 'Expected to have at least one default exclusion');
  82. $this->assertContains($this->model->getAdjustmentCode(), $defaultExclusions);
  83. }
  84. /**
  85. * Test for method displayBothPrices
  86. */
  87. public function testDisplayBothPrices()
  88. {
  89. $shouldDisplayBothPrices = true;
  90. $this->taxHelperMock->expects($this->once())
  91. ->method('displayBothPrices')
  92. ->will($this->returnValue($shouldDisplayBothPrices));
  93. $this->assertEquals($shouldDisplayBothPrices, $this->model->displayBothPrices());
  94. }
  95. /**
  96. * Test for method getDisplayAmountExclTax
  97. */
  98. public function testGetDisplayAmountExclTax()
  99. {
  100. $expectedPriceValue = 1.23;
  101. $expectedPrice = '$4.56';
  102. /** @var \Magento\Framework\Pricing\Render\Amount $amountRender */
  103. $amountRender = $this->getMockBuilder(\Magento\Framework\Pricing\Render\Amount::class)
  104. ->disableOriginalConstructor()
  105. ->setMethods(['getAmount'])
  106. ->getMock();
  107. /** @var \Magento\Framework\Pricing\Amount\Base $baseAmount */
  108. $baseAmount = $this->getMockBuilder(\Magento\Framework\Pricing\Amount\Base::class)
  109. ->disableOriginalConstructor()
  110. ->setMethods(['getValue'])
  111. ->getMock();
  112. $baseAmount->expects($this->any())
  113. ->method('getValue')
  114. ->will($this->returnValue($expectedPriceValue));
  115. $amountRender->expects($this->any())
  116. ->method('getAmount')
  117. ->will($this->returnValue($baseAmount));
  118. $this->priceCurrencyMock->expects($this->any())
  119. ->method('format')
  120. ->will($this->returnValue($expectedPrice));
  121. $this->model->render($amountRender);
  122. $result = $this->model->getDisplayAmountExclTax();
  123. $this->assertEquals($expectedPrice, $result);
  124. }
  125. /**
  126. * Test for method getDisplayAmount
  127. *
  128. * @param bool $includeContainer
  129. * @dataProvider getDisplayAmountDataProvider
  130. */
  131. public function testGetDisplayAmount($includeContainer)
  132. {
  133. $expectedPriceValue = 1.23;
  134. $expectedPrice = '$4.56';
  135. /** @var \Magento\Framework\Pricing\Render\Amount $amountRender */
  136. $amountRender = $this->getMockBuilder(\Magento\Framework\Pricing\Render\Amount::class)
  137. ->disableOriginalConstructor()
  138. ->setMethods(['getAmount'])
  139. ->getMock();
  140. /** @var \Magento\Framework\Pricing\Amount\Base $baseAmount */
  141. $baseAmount = $this->getMockBuilder(\Magento\Framework\Pricing\Amount\Base::class)
  142. ->disableOriginalConstructor()
  143. ->setMethods(['getValue'])
  144. ->getMock();
  145. $baseAmount->expects($this->any())
  146. ->method('getValue')
  147. ->will($this->returnValue($expectedPriceValue));
  148. $amountRender->expects($this->any())
  149. ->method('getAmount')
  150. ->will($this->returnValue($baseAmount));
  151. $this->priceCurrencyMock->expects($this->any())
  152. ->method('format')
  153. ->with($this->anything(), $this->equalTo($includeContainer))
  154. ->will($this->returnValue($expectedPrice));
  155. $this->model->render($amountRender);
  156. $result = $this->model->getDisplayAmount($includeContainer);
  157. $this->assertEquals($expectedPrice, $result);
  158. }
  159. /**
  160. * Data provider for testGetDisplayAmount
  161. *
  162. * @return array
  163. */
  164. public function getDisplayAmountDataProvider()
  165. {
  166. return [[true], [false]];
  167. }
  168. /**
  169. * Test for method buildIdWithPrefix
  170. *
  171. * @param string $prefix
  172. * @param null|false|int $saleableId
  173. * @param null|false|string $suffix
  174. * @param string $expectedResult
  175. * @dataProvider buildIdWithPrefixDataProvider
  176. */
  177. public function testBuildIdWithPrefix($prefix, $saleableId, $suffix, $expectedResult)
  178. {
  179. /** @var \Magento\Framework\Pricing\Render\Amount $amountRender */
  180. $amountRender = $this->getMockBuilder(\Magento\Framework\Pricing\Render\Amount::class)
  181. ->disableOriginalConstructor()
  182. ->setMethods(['getSaleableItem'])
  183. ->getMock();
  184. /** @var \Magento\Catalog\Model\Product $saleable */
  185. $saleable = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  186. ->disableOriginalConstructor()
  187. ->setMethods(['getId', '__wakeup'])
  188. ->getMock();
  189. $amountRender->expects($this->any())
  190. ->method('getSaleableItem')
  191. ->will($this->returnValue($saleable));
  192. $saleable->expects($this->any())
  193. ->method('getId')
  194. ->will($this->returnValue($saleableId));
  195. $this->model->setIdSuffix($suffix);
  196. $this->model->render($amountRender);
  197. $result = $this->model->buildIdWithPrefix($prefix);
  198. $this->assertEquals($expectedResult, $result);
  199. }
  200. /**
  201. * data provider for testBuildIdWithPrefix
  202. *
  203. * @return array
  204. */
  205. public function buildIdWithPrefixDataProvider()
  206. {
  207. return [
  208. ['some_prefix_', null, '_suffix', 'some_prefix__suffix'],
  209. ['some_prefix_', false, '_suffix', 'some_prefix__suffix'],
  210. ['some_prefix_', 123, '_suffix', 'some_prefix_123_suffix'],
  211. ['some_prefix_', 123, null, 'some_prefix_123'],
  212. ['some_prefix_', 123, false, 'some_prefix_123'],
  213. ];
  214. }
  215. /**
  216. * test for method displayPriceIncludingTax
  217. */
  218. public function testDisplayPriceIncludingTax()
  219. {
  220. $expectedResult = true;
  221. $this->taxHelperMock->expects($this->once())
  222. ->method('displayPriceIncludingTax')
  223. ->will($this->returnValue($expectedResult));
  224. $result = $this->model->displayPriceIncludingTax();
  225. $this->assertEquals($expectedResult, $result);
  226. }
  227. /**
  228. * test for method displayPriceExcludingTax
  229. */
  230. public function testDisplayPriceExcludingTax()
  231. {
  232. $expectedResult = true;
  233. $this->taxHelperMock->expects($this->once())
  234. ->method('displayPriceExcludingTax')
  235. ->will($this->returnValue($expectedResult));
  236. $result = $this->model->displayPriceExcludingTax();
  237. $this->assertEquals($expectedResult, $result);
  238. }
  239. public function testGetHtmlExcluding()
  240. {
  241. $arguments = [];
  242. $displayValue = 8.0;
  243. $amountRender = $this->getMockForAbstractClass(\Magento\Framework\Pricing\Render\AmountRenderInterface::class);
  244. $amountMock = $this->getMockForAbstractClass(\Magento\Framework\Pricing\Amount\AmountInterface::class);
  245. $amountMock->expects($this->once())
  246. ->method('getValue')
  247. ->with(\Magento\Tax\Pricing\Adjustment::ADJUSTMENT_CODE)
  248. ->willReturn($displayValue);
  249. $this->taxHelperMock->expects($this->once())
  250. ->method('displayBothPrices')
  251. ->will($this->returnValue(false));
  252. $this->taxHelperMock->expects($this->once())
  253. ->method('displayPriceExcludingTax')
  254. ->will($this->returnValue(true));
  255. $amountRender->expects($this->once())
  256. ->method('setDisplayValue')
  257. ->with($displayValue);
  258. $amountRender->expects($this->once())
  259. ->method('getAmount')
  260. ->will($this->returnValue($amountMock));
  261. $this->model->render($amountRender, $arguments);
  262. }
  263. public function testGetHtmlBoth()
  264. {
  265. $arguments = [];
  266. $this->model->setZone(\Magento\Framework\Pricing\Render::ZONE_ITEM_VIEW);
  267. $amountRender = $this->createPartialMock(\Magento\Framework\Pricing\Render\Amount::class, [
  268. 'setPriceDisplayLabel',
  269. 'setPriceWrapperCss',
  270. 'setPriceId',
  271. 'getSaleableItem'
  272. ]);
  273. $product = $this->getMockForAbstractClass(\Magento\Framework\Pricing\SaleableInterface::class);
  274. $product->expects($this->once())
  275. ->method('getId');
  276. $this->taxHelperMock->expects($this->once())
  277. ->method('displayBothPrices')
  278. ->will($this->returnValue(true));
  279. $amountRender->expects($this->once())
  280. ->method('setPriceDisplayLabel');
  281. $amountRender->expects($this->once())
  282. ->method('getSaleableItem')
  283. ->will($this->returnValue($product));
  284. $amountRender->expects($this->once())
  285. ->method('setPriceId');
  286. $amountRender->expects($this->once())
  287. ->method('setPriceWrapperCss');
  288. $this->model->render($amountRender, $arguments);
  289. }
  290. }