DataTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Test\Unit\Helper;
  7. use Magento\Checkout\Helper\Data;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\Store\Model\ScopeInterface;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class DataTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Pricing\PriceCurrencyInterface
  17. */
  18. private $priceCurrency;
  19. /**
  20. * @var Data
  21. */
  22. private $helper;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $transportBuilder;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $translator;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $checkoutSession;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $scopeConfig;
  39. /**
  40. * @var \PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $eventManager;
  43. /**
  44. * @inheritdoc
  45. */
  46. protected function setUp()
  47. {
  48. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  49. $className = \Magento\Checkout\Helper\Data::class;
  50. $arguments = $objectManagerHelper->getConstructArguments($className);
  51. /** @var \Magento\Framework\App\Helper\Context $context */
  52. $context = $arguments['context'];
  53. $this->translator = $arguments['inlineTranslation'];
  54. $this->eventManager = $context->getEventManager();
  55. $this->scopeConfig = $context->getScopeConfig();
  56. $this->scopeConfig->expects($this->any())
  57. ->method('getValue')
  58. ->willReturnMap(
  59. [
  60. [
  61. 'checkout/payment_failed/template',
  62. ScopeInterface::SCOPE_STORE,
  63. 8,
  64. 'fixture_email_template_payment_failed',
  65. ],
  66. [
  67. 'checkout/payment_failed/receiver',
  68. ScopeInterface::SCOPE_STORE,
  69. 8,
  70. 'sysadmin',
  71. ],
  72. [
  73. 'trans_email/ident_sysadmin/email',
  74. ScopeInterface::SCOPE_STORE,
  75. 8,
  76. 'sysadmin@example.com',
  77. ],
  78. [
  79. 'trans_email/ident_sysadmin/name',
  80. ScopeInterface::SCOPE_STORE,
  81. 8,
  82. 'System Administrator',
  83. ],
  84. [
  85. 'checkout/payment_failed/identity',
  86. ScopeInterface::SCOPE_STORE,
  87. 8,
  88. 'noreply@example.com',
  89. ],
  90. [
  91. 'carriers/ground/title',
  92. ScopeInterface::SCOPE_STORE,
  93. null,
  94. 'Ground Shipping',
  95. ],
  96. [
  97. 'payment/fixture-payment-method/title',
  98. ScopeInterface::SCOPE_STORE,
  99. null,
  100. 'Check Money Order',
  101. ],
  102. [
  103. 'checkout/options/onepage_checkout_enabled',
  104. ScopeInterface::SCOPE_STORE,
  105. null,
  106. 'One Page Checkout',
  107. ],
  108. ]
  109. );
  110. $this->checkoutSession = $arguments['checkoutSession'];
  111. $arguments['localeDate']->expects($this->any())
  112. ->method('formatDateTime')
  113. ->willReturn('Oct 02, 2013');
  114. $this->transportBuilder = $arguments['transportBuilder'];
  115. $this->priceCurrency = $arguments['priceCurrency'];
  116. $this->helper = $objectManagerHelper->getObject($className, $arguments);
  117. }
  118. /**
  119. * @return void
  120. */
  121. public function testSendPaymentFailedEmail()
  122. {
  123. $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  124. ->setMethods(['getId'])
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $quoteMock->expects($this->any())->method('getId')->willReturn(1);
  128. $this->assertSame($this->helper, $this->helper->sendPaymentFailedEmail($quoteMock, 'test message'));
  129. }
  130. /**
  131. * @return \PHPUnit_Framework_MockObject_MockObject
  132. */
  133. public function testGetCheckout()
  134. {
  135. $this->assertEquals($this->checkoutSession, $this->helper->getCheckout());
  136. }
  137. public function testGetQuote()
  138. {
  139. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  140. $this->checkoutSession->expects($this->once())->method('getQuote')->will($this->returnValue($quoteMock));
  141. $this->assertEquals($quoteMock, $this->helper->getQuote());
  142. }
  143. public function testFormatPrice()
  144. {
  145. $price = 5.5;
  146. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  147. $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['formatPrice', '__wakeup']);
  148. $this->checkoutSession->expects($this->once())->method('getQuote')->will($this->returnValue($quoteMock));
  149. $quoteMock->expects($this->once())->method('getStore')->will($this->returnValue($storeMock));
  150. $this->priceCurrency->expects($this->once())->method('format')->will($this->returnValue('5.5'));
  151. $this->assertEquals('5.5', $this->helper->formatPrice($price));
  152. }
  153. public function testConvertPrice()
  154. {
  155. $price = 5.5;
  156. $this->priceCurrency->expects($this->once())->method('convertAndFormat')->willReturn($price);
  157. $this->assertEquals(5.5, $this->helper->convertPrice($price));
  158. }
  159. public function testCanOnepageCheckout()
  160. {
  161. $this->scopeConfig->expects($this->once())->method('isSetFlag')->with(
  162. 'checkout/options/onepage_checkout_enabled',
  163. 'store'
  164. )->will($this->returnValue(true));
  165. $this->assertTrue($this->helper->canOnepageCheckout());
  166. }
  167. public function testIsContextCheckout()
  168. {
  169. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  170. $context = $objectManagerHelper->getObject(
  171. \Magento\Framework\App\Helper\Context::class
  172. );
  173. $helper = $objectManagerHelper->getObject(
  174. \Magento\Checkout\Helper\Data::class,
  175. ['context' => $context]
  176. );
  177. $context->getRequest()->expects($this->once())->method('getParam')->with('context')->will(
  178. $this->returnValue('checkout')
  179. );
  180. $this->assertTrue($helper->isContextCheckout());
  181. }
  182. public function testIsCustomerMustBeLogged()
  183. {
  184. $this->scopeConfig->expects($this->once())->method('isSetFlag')->with(
  185. 'checkout/options/customer_must_be_logged',
  186. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  187. )->will($this->returnValue(true));
  188. $this->assertTrue($this->helper->isCustomerMustBeLogged());
  189. }
  190. public function testGetPriceInclTax()
  191. {
  192. $itemMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getPriceInclTax']);
  193. $itemMock->expects($this->exactly(2))->method('getPriceInclTax')->will($this->returnValue(5.5));
  194. $this->assertEquals(5.5, $this->helper->getPriceInclTax($itemMock));
  195. }
  196. public function testGetPriceInclTaxWithoutTax()
  197. {
  198. $qty = 1;
  199. $taxAmount = 1;
  200. $discountTaxCompensation = 1;
  201. $rowTotal = 15;
  202. $roundPrice = 17;
  203. $expected = 17;
  204. $storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  205. $objectManagerHelper = new ObjectManager($this);
  206. $helper = $objectManagerHelper->getObject(
  207. \Magento\Checkout\Helper\Data::class,
  208. [
  209. 'storeManager' => $storeManager,
  210. 'priceCurrency' => $this->priceCurrency,
  211. ]
  212. );
  213. $itemMock = $this->createPartialMock(
  214. \Magento\Framework\DataObject::class,
  215. ['getPriceInclTax', 'getQty', 'getTaxAmount', 'getDiscountTaxCompensation', 'getRowTotal', 'getQtyOrdered']
  216. );
  217. $itemMock->expects($this->once())->method('getPriceInclTax')->will($this->returnValue(false));
  218. $itemMock->expects($this->exactly(2))->method('getQty')->will($this->returnValue($qty));
  219. $itemMock->expects($this->never())->method('getQtyOrdered');
  220. $itemMock->expects($this->once())->method('getTaxAmount')->will($this->returnValue($taxAmount));
  221. $itemMock->expects($this->once())
  222. ->method('getDiscountTaxCompensation')->will($this->returnValue($discountTaxCompensation));
  223. $itemMock->expects($this->once())->method('getRowTotal')->will($this->returnValue($rowTotal));
  224. $this->priceCurrency->expects($this->once())->method('round')->with($roundPrice)->willReturn($roundPrice);
  225. $this->assertEquals($expected, $helper->getPriceInclTax($itemMock));
  226. }
  227. public function testGetSubtotalInclTax()
  228. {
  229. $rowTotalInclTax = 5.5;
  230. $expected = 5.5;
  231. $itemMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getRowTotalInclTax']);
  232. $itemMock->expects($this->exactly(2))->method('getRowTotalInclTax')->will($this->returnValue($rowTotalInclTax));
  233. $this->assertEquals($expected, $this->helper->getSubtotalInclTax($itemMock));
  234. }
  235. public function testGetSubtotalInclTaxNegative()
  236. {
  237. $taxAmount = 1;
  238. $discountTaxCompensation = 1;
  239. $rowTotal = 15;
  240. $expected = 17;
  241. $itemMock = $this->createPartialMock(
  242. \Magento\Framework\DataObject::class,
  243. ['getRowTotalInclTax', 'getTaxAmount', 'getDiscountTaxCompensation', 'getRowTotal']
  244. );
  245. $itemMock->expects($this->once())->method('getRowTotalInclTax')->will($this->returnValue(false));
  246. $itemMock->expects($this->once())->method('getTaxAmount')->will($this->returnValue($taxAmount));
  247. $itemMock->expects($this->once())
  248. ->method('getDiscountTaxCompensation')->will($this->returnValue($discountTaxCompensation));
  249. $itemMock->expects($this->once())->method('getRowTotal')->will($this->returnValue($rowTotal));
  250. $this->assertEquals($expected, $this->helper->getSubtotalInclTax($itemMock));
  251. }
  252. public function testGetBasePriceInclTaxWithoutQty()
  253. {
  254. $storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  255. $objectManagerHelper = new ObjectManager($this);
  256. $helper = $objectManagerHelper->getObject(
  257. \Magento\Checkout\Helper\Data::class,
  258. [
  259. 'storeManager' => $storeManager,
  260. 'priceCurrency' => $this->priceCurrency,
  261. ]
  262. );
  263. $itemMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getQty']);
  264. $itemMock->expects($this->once())->method('getQty');
  265. $this->priceCurrency->expects($this->once())->method('round');
  266. $helper->getPriceInclTax($itemMock);
  267. }
  268. public function testGetBasePriceInclTax()
  269. {
  270. $storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  271. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  272. $helper = $objectManagerHelper->getObject(
  273. \Magento\Checkout\Helper\Data::class,
  274. [
  275. 'storeManager' => $storeManager,
  276. 'priceCurrency' => $this->priceCurrency,
  277. ]
  278. );
  279. $itemMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getQty', 'getQtyOrdered']);
  280. $itemMock->expects($this->once())->method('getQty')->will($this->returnValue(false));
  281. $itemMock->expects($this->exactly(2))->method('getQtyOrdered')->will($this->returnValue(5.5));
  282. $this->priceCurrency->expects($this->once())->method('round');
  283. $helper->getBasePriceInclTax($itemMock);
  284. }
  285. public function testGetBaseSubtotalInclTax()
  286. {
  287. $itemMock = $this->createPartialMock(
  288. \Magento\Framework\DataObject::class,
  289. ['getBaseTaxAmount', 'getBaseDiscountTaxCompensation', 'getBaseRowTotal']
  290. );
  291. $itemMock->expects($this->once())->method('getBaseTaxAmount');
  292. $itemMock->expects($this->once())->method('getBaseDiscountTaxCompensation');
  293. $itemMock->expects($this->once())->method('getBaseRowTotal');
  294. $this->helper->getBaseSubtotalInclTax($itemMock);
  295. }
  296. public function testIsAllowedGuestCheckoutWithoutStore()
  297. {
  298. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  299. $store = null;
  300. $quoteMock->expects($this->once())->method('getStoreId')->will($this->returnValue(1));
  301. $this->scopeConfig->expects($this->once())
  302. ->method('isSetFlag')
  303. ->will($this->returnValue(true));
  304. $this->assertTrue($this->helper->isAllowedGuestCheckout($quoteMock, $store));
  305. }
  306. }