AbstractItemsTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Block\Adminhtml\Items;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. /**
  9. * Class AbstractItemsTest
  10. * @package Magento\Sales\Block\Adminhtml\Items
  11. * TODO refactor me PLEASE
  12. *
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class AbstractItemsTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /** @var ObjectManagerHelper */
  18. protected $objectManagerHelper;
  19. /** @var \PHPUnit_Framework_MockObject_MockObject */
  20. protected $stockItemMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $stockRegistry;
  25. protected function setUp()
  26. {
  27. $this->objectManagerHelper = new ObjectManagerHelper($this);
  28. $this->stockRegistry = $this->getMockBuilder(\Magento\CatalogInventory\Model\StockRegistry::class)
  29. ->disableOriginalConstructor()
  30. ->setMethods(['getStockItem', '__wakeup'])
  31. ->getMock();
  32. $this->stockItemMock = $this->createPartialMock(
  33. \Magento\CatalogInventory\Model\Stock\Item::class,
  34. ['getManageStock', '__wakeup']
  35. );
  36. $this->stockRegistry->expects($this->any())
  37. ->method('getStockItem')
  38. ->will($this->returnValue($this->stockItemMock));
  39. }
  40. public function testGetItemRenderer()
  41. {
  42. $layout = $this->createPartialMock(
  43. \Magento\Framework\View\Layout::class,
  44. ['getChildName', 'getBlock', 'getGroupChildNames']
  45. );
  46. $layout->expects($this->any())
  47. ->method('getChildName')
  48. ->with(null, 'some-type')
  49. ->will($this->returnValue('column_block-name'));
  50. $layout->expects($this->any())
  51. ->method('getGroupChildNames')
  52. ->with(null, 'column')
  53. ->will($this->returnValue(['column_block-name']));
  54. /** @var \Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer $renderer */
  55. $renderer = $this->objectManagerHelper
  56. ->getObject(\Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer::class);
  57. $renderer->setLayout($layout);
  58. $layout->expects($this->any())
  59. ->method('getBlock')
  60. ->with('column_block-name')
  61. ->will($this->returnValue($renderer));
  62. /** @var \Magento\Sales\Block\Adminhtml\Items\AbstractItems $block */
  63. $block = $this->objectManagerHelper->getObject(\Magento\Sales\Block\Adminhtml\Items\AbstractItems::class);
  64. $block->setLayout($layout);
  65. $this->assertSame($renderer, $block->getItemRenderer('some-type'));
  66. $this->assertSame($renderer, $renderer->getColumnRenderer('block-name'));
  67. }
  68. /**
  69. * @expectedException \RuntimeException
  70. * @expectedExceptionMessage Renderer for type "some-type" does not exist.
  71. */
  72. public function testGetItemRendererThrowsExceptionForNonexistentRenderer()
  73. {
  74. $renderer = $this->createMock(\stdClass::class);
  75. $layout = $this->createPartialMock(
  76. \Magento\Framework\View\Layout::class,
  77. ['getChildName', 'getBlock', '__wakeup']
  78. );
  79. $layout->expects($this->at(0))
  80. ->method('getChildName')
  81. ->with(null, 'some-type')
  82. ->will($this->returnValue('some-block-name'));
  83. $layout->expects($this->at(1))
  84. ->method('getBlock')
  85. ->with('some-block-name')
  86. ->will($this->returnValue($renderer));
  87. /** @var $block \Magento\Sales\Block\Adminhtml\Items\AbstractItems */
  88. $block = $this->objectManagerHelper->getObject(
  89. \Magento\Sales\Block\Adminhtml\Items\AbstractItems::class,
  90. [
  91. 'context' => $this->objectManagerHelper->getObject(
  92. \Magento\Backend\Block\Template\Context::class,
  93. ['layout' => $layout]
  94. )
  95. ]
  96. );
  97. $block->getItemRenderer('some-type');
  98. }
  99. /**
  100. * @param bool $canReturnToStock
  101. * @param array $itemConfig
  102. * @param bool $result
  103. * @dataProvider canReturnItemToStockDataProvider
  104. */
  105. public function testCanReturnItemToStock($canReturnToStock, $itemConfig, $result)
  106. {
  107. $productId = isset($itemConfig['product_id']) ? $itemConfig['product_id'] : null;
  108. $manageStock = isset($itemConfig['manage_stock']) ? $itemConfig['manage_stock'] : null;
  109. $item = $this->createPartialMock(
  110. \Magento\Sales\Model\Order\Creditmemo\Item::class,
  111. ['hasCanReturnToStock', 'getOrderItem', 'setCanReturnToStock', 'getCanReturnToStock', '__wakeup']
  112. );
  113. $dependencies = $this->prepareServiceMockDependency(
  114. $item,
  115. $canReturnToStock,
  116. $productId,
  117. $manageStock,
  118. $itemConfig
  119. );
  120. /** @var $block \Magento\Sales\Block\Adminhtml\Items\AbstractItems */
  121. $block = $this->objectManagerHelper->getObject(
  122. \Magento\Sales\Block\Adminhtml\Items\AbstractItems::class,
  123. $dependencies
  124. );
  125. $this->assertSame($result, $block->canReturnItemToStock($item));
  126. }
  127. /**
  128. * @param \PHPUnit_Framework_MockObject_MockObject $item
  129. * @param bool $canReturnToStock
  130. * @param int|null $productId
  131. * @param bool $manageStock
  132. * @param array $itemConfig
  133. * @return array
  134. */
  135. protected function prepareServiceMockDependency($item, $canReturnToStock, $productId, $manageStock, $itemConfig)
  136. {
  137. $dependencies = [];
  138. $this->stockItemMock->expects($this->any())
  139. ->method('getManageStock')
  140. ->will($this->returnValue($manageStock));
  141. $dependencies['stockRegistry'] = $this->stockRegistry;
  142. $item->expects($this->once())
  143. ->method('hasCanReturnToStock')
  144. ->will($this->returnValue($itemConfig['has_can_return_to_stock']));
  145. if (!$itemConfig['has_can_return_to_stock']) {
  146. $orderItem = $this->createPartialMock(
  147. \Magento\Sales\Model\Order\Item::class,
  148. ['getProductId', '__wakeup', 'getStore']
  149. );
  150. $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getWebsiteId']);
  151. $store->expects($this->once())
  152. ->method('getWebsiteId')
  153. ->will($this->returnValue(10));
  154. $orderItem->expects($this->once())
  155. ->method('getStore')
  156. ->will($this->returnValue($store));
  157. $orderItem->expects($this->once())
  158. ->method('getProductId')
  159. ->will($this->returnValue($productId));
  160. $item->expects($this->any())
  161. ->method('getOrderItem')
  162. ->will($this->returnValue($orderItem));
  163. if ($productId && $manageStock) {
  164. $canReturn = true;
  165. } else {
  166. $canReturn = false;
  167. }
  168. $item->expects($this->once())
  169. ->method('setCanReturnToStock')
  170. ->with($this->equalTo($canReturn))
  171. ->will($this->returnSelf());
  172. }
  173. $item->expects($this->once())
  174. ->method('getCanReturnToStock')
  175. ->will($this->returnValue($canReturnToStock));
  176. return $dependencies;
  177. }
  178. public function testCanReturnItemToStockEmpty()
  179. {
  180. $stockConfiguration = $this->getMockBuilder(\Magento\CatalogInventory\Model\Configuration::class)
  181. ->disableOriginalConstructor()
  182. ->setMethods(['canSubtractQty', '__wakeup'])
  183. ->getMock();
  184. $stockConfiguration->expects($this->once())
  185. ->method('canSubtractQty')
  186. ->will($this->returnValue(true));
  187. /** @var $block \Magento\Sales\Block\Adminhtml\Items\AbstractItems */
  188. $block = $this->objectManagerHelper->getObject(
  189. \Magento\Sales\Block\Adminhtml\Items\AbstractItems::class,
  190. [
  191. 'stockConfiguration' => $stockConfiguration
  192. ]
  193. );
  194. $result = $block->canReturnItemToStock();
  195. $this->assertTrue($result);
  196. }
  197. /**
  198. * @return array
  199. */
  200. public function canReturnItemToStockDataProvider()
  201. {
  202. return [
  203. [true, ['has_can_return_to_stock' => true], true],
  204. [false, ['has_can_return_to_stock' => true], false],
  205. [false, ['has_can_return_to_stock' => false, 'product_id' => 2, 'manage_stock' => false], false],
  206. [true, ['has_can_return_to_stock' => false, 'product_id' => 2, 'manage_stock' => true], true],
  207. ];
  208. }
  209. }