RendererTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Test\Unit\Block\Item\Price;
  7. use Magento\Framework\Pricing\Render;
  8. class RendererTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Tax\Block\Item\Price\Renderer
  12. */
  13. protected $renderer;
  14. /**
  15. * @var \Magento\Tax\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $taxHelper;
  18. /**
  19. * @var \Magento\Framework\Pricing\PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $priceCurrency;
  22. protected function setUp()
  23. {
  24. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  25. $this->priceCurrency = $this->getMockBuilder(
  26. \Magento\Framework\Pricing\PriceCurrencyInterface::class
  27. )->getMock();
  28. $this->taxHelper = $this->getMockBuilder(\Magento\Tax\Helper\Data::class)
  29. ->disableOriginalConstructor()
  30. ->setMethods([
  31. 'displayCartPriceExclTax',
  32. 'displayCartBothPrices',
  33. 'displayCartPriceInclTax',
  34. 'displaySalesPriceExclTax',
  35. 'displaySalesBothPrices',
  36. 'displaySalesPriceInclTax',
  37. ])
  38. ->getMock();
  39. $this->renderer = $objectManager->getObject(
  40. \Magento\Tax\Block\Item\Price\Renderer::class,
  41. [
  42. 'taxHelper' => $this->taxHelper,
  43. 'priceCurrency' => $this->priceCurrency,
  44. 'data' => [
  45. 'zone' => Render::ZONE_CART,
  46. ]
  47. ]
  48. );
  49. }
  50. /**
  51. * @param $storeId
  52. * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Sales\Model\Order\Item
  53. */
  54. protected function getItemMockWithStoreId($storeId)
  55. {
  56. $itemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  57. ->disableOriginalConstructor()
  58. ->setMethods(['getStoreId', '__wakeup'])
  59. ->getMock();
  60. $itemMock->expects($this->once())
  61. ->method('getStoreId')
  62. ->will($this->returnValue($storeId));
  63. return $itemMock;
  64. }
  65. /**
  66. * Test displayPriceInclTax
  67. *
  68. * @param string $zone
  69. * @param string $methodName
  70. * @dataProvider displayPriceInclTaxDataProvider
  71. */
  72. public function testDisplayPriceInclTax($zone, $methodName)
  73. {
  74. $storeId = 1;
  75. $flag = true;
  76. $itemMock = $this->getItemMockWithStoreId($storeId);
  77. $this->renderer->setItem($itemMock);
  78. $this->renderer->setZone($zone);
  79. $this->taxHelper->expects($this->once())
  80. ->method($methodName)
  81. ->with($storeId)
  82. ->will($this->returnValue($flag));
  83. $this->assertEquals($flag, $this->renderer->displayPriceInclTax());
  84. }
  85. /**
  86. * @return array
  87. */
  88. public function displayPriceInclTaxDataProvider()
  89. {
  90. $data = [
  91. 'cart' => [
  92. 'zone' => Render::ZONE_CART,
  93. 'method_name' => 'displayCartPriceInclTax',
  94. ],
  95. 'anythingelse' => [
  96. 'zone' => 'anythingelse',
  97. 'method_name' => 'displayCartPriceInclTax',
  98. ],
  99. 'sale' => [
  100. 'zone' => Render::ZONE_SALES,
  101. 'method_name' => 'displaySalesPriceInclTax',
  102. ],
  103. 'email' => [
  104. 'zone' => Render::ZONE_EMAIL,
  105. 'method_name' => 'displaySalesPriceInclTax',
  106. ],
  107. ];
  108. return $data;
  109. }
  110. /**
  111. * Test displayPriceExclTax
  112. *
  113. * @param string $zone
  114. * @param string $methodName
  115. * @dataProvider displayPriceExclTaxDataProvider
  116. */
  117. public function testDisplayPriceExclTax($zone, $methodName)
  118. {
  119. $storeId = 1;
  120. $flag = true;
  121. $itemMock = $this->getItemMockWithStoreId($storeId);
  122. $this->renderer->setItem($itemMock);
  123. $this->renderer->setZone($zone);
  124. $this->taxHelper->expects($this->once())
  125. ->method($methodName)
  126. ->with($storeId)
  127. ->will($this->returnValue($flag));
  128. $this->assertEquals($flag, $this->renderer->displayPriceExclTax());
  129. }
  130. /**
  131. * @return array
  132. */
  133. public function displayPriceExclTaxDataProvider()
  134. {
  135. $data = [
  136. 'cart' => [
  137. 'zone' => Render::ZONE_CART,
  138. 'method_name' => 'displayCartPriceExclTax',
  139. ],
  140. 'anythingelse' => [
  141. 'zone' => 'anythingelse',
  142. 'method_name' => 'displayCartPriceExclTax',
  143. ],
  144. 'sale' => [
  145. 'zone' => Render::ZONE_SALES,
  146. 'method_name' => 'displaySalesPriceExclTax',
  147. ],
  148. 'email' => [
  149. 'zone' => Render::ZONE_EMAIL,
  150. 'method_name' => 'displaySalesPriceExclTax',
  151. ],
  152. ];
  153. return $data;
  154. }
  155. /**
  156. * Test displayBothPrices
  157. *
  158. * @param string $zone
  159. * @param string $methodName
  160. * @dataProvider displayBothPricesDataProvider
  161. */
  162. public function testDisplayBothPrices($zone, $methodName)
  163. {
  164. $storeId = 1;
  165. $flag = true;
  166. $itemMock = $this->getItemMockWithStoreId($storeId);
  167. $this->renderer->setItem($itemMock);
  168. $this->renderer->setZone($zone);
  169. $this->taxHelper->expects($this->once())
  170. ->method($methodName)
  171. ->with($storeId)
  172. ->will($this->returnValue($flag));
  173. $this->assertEquals($flag, $this->renderer->displayBothPrices());
  174. }
  175. /**
  176. * @return array
  177. */
  178. public function displayBothPricesDataProvider()
  179. {
  180. $data = [
  181. 'cart' => [
  182. 'zone' => Render::ZONE_CART,
  183. 'method_name' => 'displayCartBothPrices',
  184. ],
  185. 'anythingelse' => [
  186. 'zone' => 'anythingelse',
  187. 'method_name' => 'displayCartBothPrices',
  188. ],
  189. 'sale' => [
  190. 'zone' => Render::ZONE_SALES,
  191. 'method_name' => 'displaySalesBothPrices',
  192. ],
  193. 'email' => [
  194. 'zone' => Render::ZONE_EMAIL,
  195. 'method_name' => 'displaySalesBothPrices',
  196. ],
  197. ];
  198. return $data;
  199. }
  200. public function testFormatPriceQuoteItem()
  201. {
  202. $price = 3.554;
  203. $formattedPrice = "$3.55";
  204. $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  205. ->disableOriginalConstructor()
  206. ->setMethods(['formatPrice', '__wakeup'])
  207. ->getMock();
  208. $this->priceCurrency->expects($this->once())
  209. ->method('format')
  210. ->with($price, true)
  211. ->will($this->returnValue($formattedPrice));
  212. $itemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
  213. ->disableOriginalConstructor()
  214. ->setMethods(['getStore', '__wakeup'])
  215. ->getMock();
  216. $itemMock->expects($this->once())
  217. ->method('getStore')
  218. ->will($this->returnValue($storeMock));
  219. $this->renderer->setItem($itemMock);
  220. $this->assertEquals($formattedPrice, $this->renderer->formatPrice($price));
  221. }
  222. public function testFormatPriceOrderItem()
  223. {
  224. $price = 3.554;
  225. $formattedPrice = "$3.55";
  226. $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  227. ->disableOriginalConstructor()
  228. ->getMock();
  229. $orderMock->expects($this->once())
  230. ->method('formatPrice')
  231. ->with($price, false)
  232. ->will($this->returnValue($formattedPrice));
  233. $itemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  234. ->disableOriginalConstructor()
  235. ->setMethods(['getOrder', '__wakeup'])
  236. ->getMock();
  237. $itemMock->expects($this->once())
  238. ->method('getOrder')
  239. ->will($this->returnValue($orderMock));
  240. $this->renderer->setItem($itemMock);
  241. $this->assertEquals($formattedPrice, $this->renderer->formatPrice($price));
  242. }
  243. public function testFormatPriceInvoiceItem()
  244. {
  245. $price = 3.554;
  246. $formattedPrice = "$3.55";
  247. $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  248. ->disableOriginalConstructor()
  249. ->setMethods(['formatPrice', '__wakeup'])
  250. ->getMock();
  251. $orderMock->expects($this->once())
  252. ->method('formatPrice')
  253. ->with($price, false)
  254. ->will($this->returnValue($formattedPrice));
  255. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  256. ->disableOriginalConstructor()
  257. ->setMethods(['getOrder', '__wakeup'])
  258. ->getMock();
  259. $orderItemMock->expects($this->once())
  260. ->method('getOrder')
  261. ->will($this->returnValue($orderMock));
  262. $invoiceItemMock = $this->getMockBuilder(\Magento\Sales\Model\Invoice\Item::class)
  263. ->disableOriginalConstructor()
  264. ->setMethods(['getOrderItem', '__wakeup', 'getStoreId'])
  265. ->getMock();
  266. $invoiceItemMock->expects($this->once())
  267. ->method('getOrderItem')
  268. ->will($this->returnValue($orderItemMock));
  269. $this->renderer->setItem($invoiceItemMock);
  270. $this->assertEquals($formattedPrice, $this->renderer->formatPrice($price));
  271. }
  272. public function testGetZone()
  273. {
  274. $this->assertEquals(Render::ZONE_CART, $this->renderer->getZone());
  275. }
  276. public function testGetStoreId()
  277. {
  278. $storeId = 'default';
  279. $itemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
  280. ->disableOriginalConstructor()
  281. ->setMethods(['getStoreId', '__wakeup'])
  282. ->getMock();
  283. $itemMock->expects($this->once())
  284. ->method('getStoreId')
  285. ->will($this->returnValue($storeId));
  286. $this->renderer->setItem($itemMock);
  287. $this->assertEquals($storeId, $this->renderer->getStoreId());
  288. }
  289. public function testGetItemDisplayPriceExclTaxQuoteItem()
  290. {
  291. $price = 10;
  292. /** @var \Magento\Quote\Model\Quote\Item|\PHPUnit_Framework_MockObject_MockObject $quoteItemMock */
  293. $quoteItemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
  294. ->disableOriginalConstructor()
  295. ->setMethods(['getCalculationPrice', '__wakeup'])
  296. ->getMock();
  297. $quoteItemMock->expects($this->once())
  298. ->method('getCalculationPrice')
  299. ->will($this->returnValue($price));
  300. $this->renderer->setItem($quoteItemMock);
  301. $this->assertEquals($price, $this->renderer->getItemDisplayPriceExclTax());
  302. }
  303. public function testGetItemDisplayPriceExclTaxOrderItem()
  304. {
  305. $price = 10;
  306. /** @var \Magento\Sales\Model\Order\Item|\PHPUnit_Framework_MockObject_MockObject $orderItemMock */
  307. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  308. ->disableOriginalConstructor()
  309. ->setMethods(['getPrice', '__wakeup'])
  310. ->getMock();
  311. $orderItemMock->expects($this->once())
  312. ->method('getPrice')
  313. ->will($this->returnValue($price));
  314. $this->renderer->setItem($orderItemMock);
  315. $this->assertEquals($price, $this->renderer->getItemDisplayPriceExclTax());
  316. }
  317. public function testGetTotalAmount()
  318. {
  319. $rowTotal = 100;
  320. $taxAmount = 10;
  321. $discountTaxCompensationAmount = 2;
  322. $discountAmount = 20;
  323. $expectedValue = $rowTotal + $taxAmount + $discountTaxCompensationAmount - $discountAmount;
  324. $itemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  325. ->disableOriginalConstructor()
  326. ->setMethods(
  327. [
  328. 'getRowTotal',
  329. 'getTaxAmount',
  330. 'getDiscountTaxCompensationAmount',
  331. 'getDiscountAmount',
  332. '__wakeup'
  333. ]
  334. )
  335. ->getMock();
  336. $itemMock->expects($this->once())
  337. ->method('getRowTotal')
  338. ->will($this->returnValue($rowTotal));
  339. $itemMock->expects($this->once())
  340. ->method('getTaxAmount')
  341. ->will($this->returnValue($taxAmount));
  342. $itemMock->expects($this->once())
  343. ->method('getDiscountTaxCompensationAmount')
  344. ->will($this->returnValue($discountTaxCompensationAmount));
  345. $itemMock->expects($this->once())
  346. ->method('getDiscountAmount')
  347. ->will($this->returnValue($discountAmount));
  348. $this->assertEquals($expectedValue, $this->renderer->getTotalAmount($itemMock));
  349. }
  350. public function testGetBaseTotalAmount()
  351. {
  352. $baseRowTotal = 100;
  353. $baseTaxAmount = 10;
  354. $baseDiscountTaxCompensationAmount = 2;
  355. $baseDiscountAmount = 20;
  356. $expectedValue = 92;
  357. $itemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  358. ->disableOriginalConstructor()
  359. ->setMethods(
  360. [
  361. 'getBaseRowTotal',
  362. 'getBaseTaxAmount',
  363. 'getBaseDiscountTaxCompensationAmount',
  364. 'getBaseDiscountAmount',
  365. '__wakeup'
  366. ]
  367. )
  368. ->getMock();
  369. $itemMock->expects($this->once())
  370. ->method('getBaseRowTotal')
  371. ->will($this->returnValue($baseRowTotal));
  372. $itemMock->expects($this->once())
  373. ->method('getBaseTaxAmount')
  374. ->will($this->returnValue($baseTaxAmount));
  375. $itemMock->expects($this->once())
  376. ->method('getBaseDiscountTaxCompensationAmount')
  377. ->will($this->returnValue($baseDiscountTaxCompensationAmount));
  378. $itemMock->expects($this->once())
  379. ->method('getBaseDiscountAmount')
  380. ->will($this->returnValue($baseDiscountAmount));
  381. $this->assertEquals($expectedValue, $this->renderer->getBaseTotalAmount($itemMock));
  382. }
  383. }