CartTotalRepositoryTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Quote\Test\Unit\Model\Cart;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class CartTotalRepositoryTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var ObjectManager
  16. */
  17. protected $objectManager;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $converterMock;
  22. /**
  23. * @var \Magento\Quote\Model\Cart\CartTotalRepository
  24. */
  25. protected $model;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $quoteRepositoryMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $quoteMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $totalsFactoryMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $addressMock;
  42. /**
  43. * @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $dataObjectHelperMock;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $couponServiceMock;
  50. /**
  51. * @var \PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $totalsConverterMock;
  54. protected function setUp()
  55. {
  56. $this->objectManager = new ObjectManager($this);
  57. $this->totalsFactoryMock = $this->createPartialMock(
  58. \Magento\Quote\Api\Data\TotalsInterfaceFactory::class,
  59. ['create']
  60. );
  61. $this->quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
  62. 'isVirtual',
  63. 'getShippingAddress',
  64. 'getBillingAddress',
  65. 'getAllVisibleItems',
  66. 'getBaseCurrencyCode',
  67. 'getQuoteCurrencyCode',
  68. 'getItemsQty',
  69. 'collectTotals'
  70. ]);
  71. $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
  72. $this->addressMock = $this->createPartialMock(
  73. \Magento\Quote\Model\Quote\Address::class,
  74. ['getData', 'getTotals']
  75. );
  76. $this->dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $this->converterMock = $this->createMock(\Magento\Quote\Model\Cart\Totals\ItemConverter::class);
  80. $this->couponServiceMock = $this->createMock(\Magento\Quote\Api\CouponManagementInterface::class);
  81. $this->totalsConverterMock = $this->createMock(\Magento\Quote\Model\Cart\TotalsConverter::class);
  82. $this->model = new \Magento\Quote\Model\Cart\CartTotalRepository(
  83. $this->totalsFactoryMock,
  84. $this->quoteRepositoryMock,
  85. $this->dataObjectHelperMock,
  86. $this->couponServiceMock,
  87. $this->totalsConverterMock,
  88. $this->converterMock
  89. );
  90. }
  91. /**
  92. * @param bool $isVirtual
  93. * @param string $getAddressType
  94. * @dataProvider getDataProvider
  95. */
  96. public function testGet($isVirtual, $getAddressType)
  97. {
  98. $cartId = 12;
  99. $itemsQty = 100;
  100. $coupon = 'coupon';
  101. $addressTotals = ['address' => 'totals'];
  102. $itemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
  103. $visibleItems = [
  104. 11 => $itemMock,
  105. ];
  106. $itemArray = [
  107. 'name' => 'item',
  108. 'options' => [ 4 => ['label' => 'justLabel']],
  109. ];
  110. $currencyCode = 'US';
  111. $this->quoteRepositoryMock->expects($this->once())
  112. ->method('getActive')
  113. ->with($cartId)
  114. ->willReturn($this->quoteMock);
  115. $this->quoteMock->expects($this->once())->method('isVirtual')->willReturn($isVirtual);
  116. $this->quoteMock->expects($this->exactly(2))->method($getAddressType)->willReturn($this->addressMock);
  117. $this->quoteMock->expects($this->once())->method('getAllVisibleItems')->willReturn($visibleItems);
  118. $this->quoteMock->expects($this->once())->method('getBaseCurrencyCode')->willReturn($currencyCode);
  119. $this->quoteMock->expects($this->once())->method('getQuoteCurrencyCode')->willReturn($currencyCode);
  120. $this->quoteMock->expects($this->once())->method('getItemsQty')->willReturn($itemsQty);
  121. $this->addressMock->expects($this->any())->method('getData')->willReturn($addressTotals);
  122. $this->addressMock->expects($this->once())->method('getTotals')->willReturn($addressTotals);
  123. $totalsMock = $this->createMock(\Magento\Quote\Api\Data\TotalsInterface::class);
  124. $this->totalsFactoryMock->expects($this->once())->method('create')->willReturn($totalsMock);
  125. $this->dataObjectHelperMock->expects($this->once())->method('populateWithArray');
  126. $this->converterMock->expects($this->once())
  127. ->method('modelToDataObject')
  128. ->with($itemMock)
  129. ->willReturn($itemArray);
  130. $totalSegmentsMock = $this->createMock(\Magento\Quote\Api\Data\TotalSegmentInterface::class);
  131. $this->totalsConverterMock->expects($this->once())
  132. ->method('process')
  133. ->with($addressTotals)
  134. ->willReturn($totalSegmentsMock);
  135. $this->couponServiceMock->expects($this->once())->method('get')->with($cartId)->willReturn($coupon);
  136. $totalsMock->expects($this->once())->method('setItems')->with([11 => $itemArray])->willReturnSelf();
  137. $totalsMock->expects($this->once())->method('setTotalSegments')->with($totalSegmentsMock)->willReturnSelf();
  138. $totalsMock->expects($this->once())->method('setCouponCode')->with($coupon)->willReturnSelf();
  139. $totalsMock->expects($this->once())->method('setGrandTotal')->willReturnSelf();
  140. $totalsMock->expects($this->once())->method('setItemsQty')->with($itemsQty)->willReturnSelf();
  141. $totalsMock->expects($this->once())->method('setBaseCurrencyCode')->with($currencyCode)->willReturnSelf();
  142. $totalsMock->expects($this->once())->method('setQuoteCurrencyCode')->with($currencyCode)->willReturnSelf();
  143. $this->assertEquals($totalsMock, $this->model->get($cartId));
  144. }
  145. /**
  146. * @return array
  147. */
  148. public function getDataProvider()
  149. {
  150. return [
  151. 'Virtual Quote' => [
  152. 'isVirtual' => true,
  153. 'getAddressType' => 'getBillingAddress'
  154. ],
  155. 'Non-virtual Quote' => [
  156. 'isVirtual' => false,
  157. 'getAddressType' => 'getShippingAddress'
  158. ]
  159. ];
  160. }
  161. }