GridTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Test\Unit\Block\Cart;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class GridTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Checkout\Block\Cart\Grid
  15. */
  16. private $block;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $itemCollectionFactoryMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $joinAttributeProcessorMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $scopeConfigMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $checkoutSessionMock;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $itemCollectionMock;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $quoteMock;
  41. /**
  42. * @var \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $layoutMock;
  45. /**
  46. * @var \PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $pagerBlockMock;
  49. protected function setUp()
  50. {
  51. $objectManagerHelper = new ObjectManagerHelper($this);
  52. $this->itemCollectionFactoryMock =
  53. $this->getMockBuilder(\Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory::class)
  54. ->disableOriginalConstructor()
  55. ->setMethods(['create'])
  56. ->getMock();
  57. $this->joinAttributeProcessorMock =
  58. $this->getMockBuilder(\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class)
  59. ->getMockForAbstractClass();
  60. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  61. ->getMockForAbstractClass();
  62. $this->checkoutSessionMock = $this->getMockBuilder(\Magento\Checkout\Model\Session::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->itemCollectionMock = $objectManagerHelper
  66. ->getCollectionMock(\Magento\Quote\Model\ResourceModel\Quote\Item\Collection::class, []);
  67. $this->quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
  71. ->getMockForAbstractClass();
  72. $this->pagerBlockMock = $this->getMockBuilder(\Magento\Theme\Block\Html\Pager::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->checkoutSessionMock->expects($this->any())->method('getQuote')->willReturn($this->quoteMock);
  76. $this->quoteMock->expects($this->any())->method('getAllVisibleItems')->willReturn([]);
  77. $this->scopeConfigMock->expects($this->at(0))
  78. ->method('getValue')
  79. ->with(
  80. \Magento\Checkout\Block\Cart\Grid::XPATH_CONFIG_NUMBER_ITEMS_TO_DISPLAY_PAGER,
  81. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  82. null
  83. )->willReturn(20);
  84. $this->block = $objectManagerHelper->getObject(
  85. \Magento\Checkout\Block\Cart\Grid::class,
  86. [
  87. 'itemCollectionFactory' => $this->itemCollectionFactoryMock,
  88. 'joinAttributeProcessor' => $this->joinAttributeProcessorMock,
  89. 'scopeConfig' => $this->scopeConfigMock,
  90. 'checkoutSession' => $this->checkoutSessionMock,
  91. 'layout' => $this->layoutMock,
  92. 'data' => ['template' => 'cart/form1.phtml']
  93. ]
  94. );
  95. }
  96. public function testGetTemplate()
  97. {
  98. $this->assertEquals('cart/form1.phtml', $this->block->getTemplate());
  99. }
  100. public function testGetItemsForGrid()
  101. {
  102. $this->getMockItemsForGrid();
  103. $this->assertEquals($this->itemCollectionMock, $this->block->getItemsForGrid());
  104. }
  105. /**
  106. * @cover \Magento\Checkout\Block\Cart\Grid::_prepareLayout
  107. */
  108. public function testSetLayout()
  109. {
  110. $itemsCount = 150;
  111. $availableLimit = 20;
  112. $this->getMockItemsForGrid();
  113. $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn($itemsCount);
  114. $this->scopeConfigMock->expects($this->at(1))
  115. ->method('getValue')
  116. ->with(
  117. \Magento\Checkout\Block\Cart\Grid::XPATH_CONFIG_NUMBER_ITEMS_TO_DISPLAY_PAGER,
  118. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  119. null
  120. )->willReturn($availableLimit);
  121. $this->layoutMock
  122. ->expects($this->once())
  123. ->method('createBlock')
  124. ->with(\Magento\Theme\Block\Html\Pager::class)
  125. ->willReturn($this->pagerBlockMock);
  126. $this->pagerBlockMock
  127. ->expects($this->once())
  128. ->method('setAvailableLimit')
  129. ->with([$availableLimit => $availableLimit])
  130. ->willReturnSelf();
  131. $this->pagerBlockMock
  132. ->expects($this->once())
  133. ->method('setCollection')
  134. ->with($this->itemCollectionMock)
  135. ->willReturnSelf();
  136. $this->layoutMock->expects($this->once())->method('setChild')->with(null, null, 'pager');
  137. $this->itemCollectionMock->expects($this->once())->method('load')->willReturnSelf();
  138. $this->quoteMock->expects($this->never())->method('getAllVisibleItems');
  139. $this->itemCollectionMock->expects($this->once())->method('getItems')->willReturn([]);
  140. $this->block->setLayout($this->layoutMock);
  141. }
  142. public function testGetItems()
  143. {
  144. $this->getMockItemsForGrid();
  145. $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(20);
  146. $this->itemCollectionMock->expects($this->once())->method('getItems')->willReturn(['expected']);
  147. $this->assertEquals(['expected'], $this->block->getItems());
  148. }
  149. private function getMockItemsForGrid()
  150. {
  151. $this->itemCollectionFactoryMock
  152. ->expects($this->once())
  153. ->method('create')
  154. ->willReturn($this->itemCollectionMock);
  155. $this->checkoutSessionMock->expects($this->any())->method('getQuote')->willReturn($this->quoteMock);
  156. $this->itemCollectionMock->expects($this->once())->method('setQuote')->with($this->quoteMock)->willReturnSelf();
  157. $this->itemCollectionMock
  158. ->expects($this->once())
  159. ->method('addFieldToFilter')
  160. ->with('parent_item_id', ['null' => true])
  161. ->willReturnSelf();
  162. $this->joinAttributeProcessorMock->expects($this->once())->method('process')->with($this->itemCollectionMock);
  163. }
  164. /**
  165. * @cover \Magento\Checkout\Block\Cart::prepareItemUrls
  166. */
  167. public function testGetItemsIfCustomItemsExists()
  168. {
  169. $itemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
  170. ->disableOriginalConstructor()
  171. ->getMock();
  172. $storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  173. ->getMockForAbstractClass();
  174. $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
  175. ->getMockForAbstractClass();
  176. $storeManager->expects($this->once())->method('getStore')->willReturn($storeMock);
  177. $objectManagerHelper = new ObjectManagerHelper($this);
  178. $this->block = $objectManagerHelper->getObject(
  179. \Magento\Checkout\Block\Cart\Grid::class,
  180. [
  181. 'itemCollectionFactory' => $this->itemCollectionFactoryMock,
  182. 'joinAttributeProcessor' => $this->joinAttributeProcessorMock,
  183. 'scopeConfig' => $this->scopeConfigMock,
  184. 'checkoutSession' => $this->checkoutSessionMock,
  185. 'layout' => $this->layoutMock,
  186. 'data' => ['custom_items' => [$itemMock]],
  187. 'storeManager' => $storeManager
  188. ]
  189. );
  190. $this->assertEquals([$itemMock], $this->block->getItems());
  191. }
  192. public function testGetItemsWhenPagerNotVisible()
  193. {
  194. $this->assertEquals([], $this->block->getItems());
  195. }
  196. }