ViewTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Controller\Adminhtml\Order;
  7. /**
  8. * @covers \Magento\Sales\Controller\Adminhtml\Order\View
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. * @SuppressWarnings(PHPMD.TooManyFields)
  11. */
  12. class ViewTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Sales\Controller\Adminhtml\Order\View
  16. */
  17. protected $viewAction;
  18. /**
  19. * @var \Magento\Backend\App\Action\Context
  20. */
  21. protected $context;
  22. /**
  23. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $requestMock;
  26. /**
  27. * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $objectManagerMock;
  30. /**
  31. * @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $orderMock;
  34. /**
  35. * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $messageManagerMock;
  38. /**
  39. * @var \Magento\Framework\App\ActionFlag|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $actionFlagMock;
  42. /**
  43. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $coreRegistryMock;
  46. /**
  47. * @var \Magento\Framework\View\Page\Config|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $pageConfigMock;
  50. /**
  51. * @var \Magento\Framework\View\Page\Title|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $pageTitleMock;
  54. /**
  55. * @var \Magento\Framework\View\Result\PageFactory|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected $resultPageFactoryMock;
  58. /**
  59. * @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
  60. */
  61. protected $resultRedirectFactoryMock;
  62. /**
  63. * @var \Magento\Backend\Model\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject
  64. */
  65. protected $resultPageMock;
  66. /**
  67. * @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
  68. */
  69. protected $resultRedirectMock;
  70. /**
  71. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  72. */
  73. protected $loggerMock;
  74. /**
  75. * @var \Magento\Sales\Api\OrderManagementInterface|\PHPUnit_Framework_MockObject_MockObject
  76. */
  77. protected $orderManagementMock;
  78. /**
  79. * @var \Magento\Sales\Api\OrderRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  80. */
  81. protected $orderRepositoryMock;
  82. /**
  83. * Test setup
  84. */
  85. protected function setUp()
  86. {
  87. $this->orderManagementMock = $this->getMockBuilder(\Magento\Sales\Api\OrderManagementInterface::class)
  88. ->getMockForAbstractClass();
  89. $this->orderRepositoryMock = $this->getMockBuilder(\Magento\Sales\Api\OrderRepositoryInterface::class)
  90. ->getMockForAbstractClass();
  91. $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  92. ->getMockForAbstractClass();
  93. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  94. ->getMock();
  95. $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
  96. ->getMock();
  97. $this->orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. $this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
  101. ->getMock();
  102. $this->actionFlagMock = $this->getMockBuilder(\Magento\Framework\App\ActionFlag::class)
  103. ->disableOriginalConstructor()
  104. ->getMock();
  105. $this->coreRegistryMock = $this->getMockBuilder(\Magento\Framework\Registry::class)
  106. ->disableOriginalConstructor()
  107. ->getMock();
  108. $this->pageConfigMock = $this->getMockBuilder(\Magento\Framework\View\Page\Config::class)
  109. ->disableOriginalConstructor()
  110. ->getMock();
  111. $this->pageTitleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
  112. ->disableOriginalConstructor()
  113. ->getMock();
  114. $this->resultPageFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
  115. ->disableOriginalConstructor()
  116. ->setMethods(['create'])
  117. ->getMock();
  118. $this->resultRedirectFactoryMock = $this->getMockBuilder(
  119. \Magento\Backend\Model\View\Result\RedirectFactory::class
  120. )
  121. ->disableOriginalConstructor()
  122. ->setMethods(['create'])
  123. ->getMock();
  124. $this->resultPageMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Page::class)
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $this->resultRedirectMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  128. ->disableOriginalConstructor()
  129. ->getMock();
  130. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  131. $this->context = $objectManager->getObject(
  132. \Magento\Backend\App\Action\Context::class,
  133. [
  134. 'request' => $this->requestMock,
  135. 'objectManager' => $this->objectManagerMock,
  136. 'actionFlag' => $this->actionFlagMock,
  137. 'messageManager' => $this->messageManagerMock,
  138. 'resultRedirectFactory' => $this->resultRedirectFactoryMock
  139. ]
  140. );
  141. $this->viewAction = $objectManager->getObject(
  142. \Magento\Sales\Controller\Adminhtml\Order\View::class,
  143. [
  144. 'context' => $this->context,
  145. 'coreRegistry' => $this->coreRegistryMock,
  146. 'resultPageFactory' => $this->resultPageFactoryMock,
  147. 'resultRedirectFactory' => $this->resultRedirectFactoryMock,
  148. 'orderManagement' => $this->orderManagementMock,
  149. 'orderRepository' => $this->orderRepositoryMock,
  150. 'logger' => $this->loggerMock
  151. ]
  152. );
  153. }
  154. /**
  155. * @covers \Magento\Sales\Controller\Adminhtml\Order\View::execute
  156. */
  157. public function testExecute()
  158. {
  159. $id = 111;
  160. $titlePart = '#111';
  161. $this->initOrder();
  162. $this->initOrderSuccess($id);
  163. $this->prepareRedirect();
  164. $this->initAction();
  165. $this->resultPageMock->expects($this->atLeastOnce())
  166. ->method('getConfig')
  167. ->willReturn($this->pageConfigMock);
  168. $this->pageConfigMock->expects($this->atLeastOnce())
  169. ->method('getTitle')
  170. ->willReturn($this->pageTitleMock);
  171. $this->orderMock->expects($this->atLeastOnce())
  172. ->method('getIncrementId')
  173. ->willReturn($id);
  174. $this->pageTitleMock->expects($this->exactly(2))
  175. ->method('prepend')
  176. ->withConsecutive(
  177. ['Orders'],
  178. [$titlePart]
  179. )
  180. ->willReturnSelf();
  181. $this->assertInstanceOf(
  182. \Magento\Backend\Model\View\Result\Page::class,
  183. $this->viewAction->execute()
  184. );
  185. }
  186. /**
  187. * @covers \Magento\Sales\Controller\Adminhtml\Order\View::execute
  188. */
  189. public function testExecuteNoOrder()
  190. {
  191. $orderIdParam = 111;
  192. $this->requestMock->expects($this->atLeastOnce())
  193. ->method('getParam')
  194. ->with('order_id')
  195. ->willReturn($orderIdParam);
  196. $this->orderRepositoryMock->expects($this->once())
  197. ->method('get')
  198. ->with($orderIdParam)
  199. ->willThrowException(
  200. new \Magento\Framework\Exception\NoSuchEntityException(
  201. __("The entity that was requested doesn't exist. Verify the entity and try again.")
  202. )
  203. );
  204. $this->initOrderFail();
  205. $this->prepareRedirect();
  206. $this->setPath('sales/*/');
  207. $this->assertInstanceOf(
  208. \Magento\Backend\Model\View\Result\Redirect::class,
  209. $this->viewAction->execute()
  210. );
  211. }
  212. /**
  213. * @covers \Magento\Sales\Controller\Adminhtml\Order\View::execute
  214. */
  215. public function testGlobalException()
  216. {
  217. $id = 111;
  218. $exception = new \Exception();
  219. $this->initOrder();
  220. $this->initOrderSuccess($id);
  221. $this->prepareRedirect();
  222. $this->resultPageFactoryMock->expects($this->once())
  223. ->method('create')
  224. ->willThrowException($exception);
  225. $this->loggerMock->expects($this->once())
  226. ->method('critical')
  227. ->with($exception);
  228. $this->messageManagerMock->expects($this->once())
  229. ->method('addErrorMessage')
  230. ->with('Exception occurred during order load')
  231. ->willReturnSelf();
  232. $this->setPath('sales/order/index');
  233. $this->assertInstanceOf(
  234. \Magento\Backend\Model\View\Result\Redirect::class,
  235. $this->viewAction->execute()
  236. );
  237. }
  238. /**
  239. * initOrder
  240. */
  241. protected function initOrder()
  242. {
  243. $orderIdParam = 111;
  244. $this->requestMock->expects($this->atLeastOnce())
  245. ->method('getParam')
  246. ->with('order_id')
  247. ->willReturn($orderIdParam);
  248. $this->orderRepositoryMock->expects($this->once())
  249. ->method('get')
  250. ->with($orderIdParam)
  251. ->willReturn($this->orderMock);
  252. }
  253. /**
  254. * init Order Success
  255. */
  256. protected function initOrderSuccess()
  257. {
  258. $this->coreRegistryMock->expects($this->exactly(2))
  259. ->method('register')
  260. ->withConsecutive(
  261. ['sales_order', $this->orderMock],
  262. ['current_order', $this->orderMock]
  263. );
  264. }
  265. /**
  266. * initOrderFail
  267. */
  268. protected function initOrderFail()
  269. {
  270. $this->messageManagerMock->expects($this->once())
  271. ->method('addErrorMessage')
  272. ->with('This order no longer exists.')
  273. ->willReturnSelf();
  274. $this->actionFlagMock->expects($this->once())
  275. ->method('set')
  276. ->with('', \Magento\Sales\Controller\Adminhtml\Order::FLAG_NO_DISPATCH, true);
  277. }
  278. /**
  279. * initAction
  280. */
  281. protected function initAction()
  282. {
  283. $this->resultPageFactoryMock->expects($this->once())
  284. ->method('create')
  285. ->willReturn($this->resultPageMock);
  286. $this->resultPageMock->expects($this->once())
  287. ->method('setActiveMenu')
  288. ->with('Magento_Sales::sales_order')
  289. ->willReturnSelf();
  290. $this->resultPageMock->expects($this->exactly(2))
  291. ->method('addBreadcrumb')
  292. ->withConsecutive(
  293. ['Sales', 'Sales'],
  294. ['Orders', 'Orders']
  295. )
  296. ->willReturnSelf();
  297. }
  298. /**
  299. * prepareRedirect
  300. */
  301. protected function prepareRedirect()
  302. {
  303. $this->resultRedirectFactoryMock->expects($this->once())
  304. ->method('create')
  305. ->willReturn($this->resultRedirectMock);
  306. }
  307. /**
  308. * @param string $path
  309. * @param array $params
  310. */
  311. protected function setPath($path, $params = [])
  312. {
  313. $this->resultRedirectMock->expects($this->once())
  314. ->method('setPath')
  315. ->with($path, $params);
  316. }
  317. }