PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-shipping/Test/Unit/Controller/Adminhtml/Order/Shipment/SaveTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 389 lines | 285 code | 36 blank | 68 comment | 3 complexity | d2aa383e0022739c0d5ba6251e2a103f MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreFile
  7. namespace Magento\Shipping\Test\Unit\Controller\Adminhtml\Order\Shipment;
  8. use Magento\Backend\App\Action;
  9. use Magento\Sales\Model\Order\Email\Sender\ShipmentSender;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  11. /**
  12. * Class SaveTest
  13. *
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class SaveTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @var \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $shipmentLoader;
  22. /**
  23. * @var \Magento\Shipping\Model\Shipping\LabelGenerator|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $labelGenerator;
  26. /**
  27. * @var ShipmentSender|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $shipmentSender;
  30. /**
  31. * @var Action\Context|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $context;
  34. /**
  35. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $request;
  38. /**
  39. * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $response;
  42. /**
  43. * @var \Magento\Framework\Message\Manager|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $messageManager;
  46. /**
  47. * @var \Magento\Framework\ObjectManager\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $objectManager;
  50. /**
  51. * @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $session;
  54. /**
  55. * @var \Magento\Framework\App\ActionFlag|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected $actionFlag;
  58. /**
  59. * @var \Magento\Backend\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  60. */
  61. protected $helper;
  62. /**
  63. * @var \Magento\Framework\Controller\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
  64. */
  65. protected $resultRedirect;
  66. /**
  67. * @var \Magento\Framework\Data\Form\FormKey\Validator|\PHPUnit_Framework_MockObject_MockObject
  68. */
  69. protected $formKeyValidator;
  70. /**
  71. * @var \Magento\Shipping\Controller\Adminhtml\Order\Shipment\Save
  72. */
  73. protected $saveAction;
  74. /**
  75. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  76. */
  77. public function setUp()
  78. {
  79. $objectManagerHelper = new ObjectManagerHelper($this);
  80. $this->shipmentLoader = $this->getMockBuilder('Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader')
  81. ->disableOriginalConstructor()
  82. ->setMethods([])
  83. ->getMock();
  84. $this->labelGenerator = $this->getMockBuilder('Magento\Shipping\Model\Shipping\LabelGenerator')
  85. ->disableOriginalConstructor()
  86. ->setMethods([])
  87. ->getMock();
  88. $this->shipmentSender = $this->getMockBuilder('Magento\Sales\Model\Order\Email\Sender\ShipmentSender')
  89. ->disableOriginalConstructor()
  90. ->setMethods([])
  91. ->getMock();
  92. $this->objectManager = $this->getMock('Magento\Framework\ObjectManagerInterface');
  93. $this->context = $this->getMock(
  94. 'Magento\Backend\App\Action\Context',
  95. [
  96. 'getRequest', 'getResponse', 'getMessageManager', 'getRedirect',
  97. 'getObjectManager', 'getSession', 'getActionFlag', 'getHelper',
  98. 'getResultRedirectFactory', 'getFormKeyValidator'
  99. ],
  100. [],
  101. '',
  102. false
  103. );
  104. $this->response = $this->getMock(
  105. 'Magento\Framework\App\ResponseInterface',
  106. ['setRedirect', 'sendResponse'],
  107. [],
  108. '',
  109. false
  110. );
  111. $this->request = $this->getMockBuilder('Magento\Framework\App\Request\Http')
  112. ->disableOriginalConstructor()->getMock();
  113. $this->objectManager = $this->getMock(
  114. 'Magento\Framework\ObjectManager\ObjectManager',
  115. ['create', 'get'],
  116. [],
  117. '',
  118. false
  119. );
  120. $this->messageManager = $this->getMock(
  121. 'Magento\Framework\Message\Manager',
  122. ['addSuccess', 'addError'],
  123. [],
  124. '',
  125. false
  126. );
  127. $this->session = $this->getMock(
  128. 'Magento\Backend\Model\Session',
  129. ['setIsUrlNotice', 'getCommentText'],
  130. [],
  131. '',
  132. false
  133. );
  134. $this->actionFlag = $this->getMock('Magento\Framework\App\ActionFlag', ['get'], [], '', false);
  135. $this->helper = $this->getMock('Magento\Backend\Helper\Data', ['getUrl'], [], '', false);
  136. $this->resultRedirect = $this->getMock(
  137. 'Magento\Framework\Controller\Result\Redirect',
  138. ['setPath'],
  139. [],
  140. '',
  141. false
  142. );
  143. $this->resultRedirect->expects($this->any())
  144. ->method('setPath')
  145. ->willReturn($this->resultRedirect);
  146. $resultRedirectFactory = $this->getMock(
  147. 'Magento\Framework\Controller\Result\RedirectFactory',
  148. ['create'],
  149. [],
  150. '',
  151. false
  152. );
  153. $resultRedirectFactory->expects($this->once())
  154. ->method('create')
  155. ->willReturn($this->resultRedirect);
  156. $this->formKeyValidator = $this->getMock(
  157. 'Magento\Framework\Data\Form\FormKey\Validator',
  158. ['validate'],
  159. [],
  160. '',
  161. false
  162. );
  163. $this->context->expects($this->once())
  164. ->method('getMessageManager')
  165. ->will($this->returnValue($this->messageManager));
  166. $this->context->expects($this->once())
  167. ->method('getRequest')
  168. ->will($this->returnValue($this->request));
  169. $this->context->expects($this->once())
  170. ->method('getResponse')
  171. ->will($this->returnValue($this->response));
  172. $this->context->expects($this->once())
  173. ->method('getObjectManager')
  174. ->will($this->returnValue($this->objectManager));
  175. $this->context->expects($this->once())
  176. ->method('getSession')
  177. ->will($this->returnValue($this->session));
  178. $this->context->expects($this->once())
  179. ->method('getActionFlag')
  180. ->will($this->returnValue($this->actionFlag));
  181. $this->context->expects($this->once())
  182. ->method('getHelper')
  183. ->will($this->returnValue($this->helper));
  184. $this->context->expects($this->once())
  185. ->method('getResultRedirectFactory')
  186. ->will($this->returnValue($resultRedirectFactory));
  187. $this->context->expects($this->once())
  188. ->method('getFormKeyValidator')
  189. ->will($this->returnValue($this->formKeyValidator));
  190. $this->saveAction = $objectManagerHelper->getObject(
  191. 'Magento\Shipping\Controller\Adminhtml\Order\Shipment\Save',
  192. [
  193. 'labelGenerator' => $this->labelGenerator,
  194. 'shipmentSender' => $this->shipmentSender,
  195. 'context' => $this->context,
  196. 'shipmentLoader' => $this->shipmentLoader,
  197. 'request' => $this->request,
  198. 'response' => $this->response
  199. ]
  200. );
  201. }
  202. /**
  203. * @param bool $formKeyIsValid
  204. * @param bool $isPost
  205. * @dataProvider executeDataProvider
  206. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  207. */
  208. public function testExecute($formKeyIsValid, $isPost)
  209. {
  210. $this->formKeyValidator->expects($this->any())
  211. ->method('validate')
  212. ->willReturn($formKeyIsValid);
  213. $this->request->expects($this->any())
  214. ->method('isPost')
  215. ->willReturn($isPost);
  216. if (!$formKeyIsValid || !$isPost) {
  217. $this->messageManager->expects($this->once())
  218. ->method('addError');
  219. $this->resultRedirect->expects($this->once())
  220. ->method('setPath')
  221. ->with('sales/order/index');
  222. $this->shipmentLoader->expects($this->never())
  223. ->method('load');
  224. $this->assertEquals($this->resultRedirect, $this->saveAction->execute());
  225. } else {
  226. $shipmentId = 1000012;
  227. $orderId = 10003;
  228. $tracking = [];
  229. $shipmentData = ['items' => [], 'send_email' => ''];
  230. $shipment = $this->getMock(
  231. 'Magento\Sales\Model\Order\Shipment',
  232. ['load', 'save', 'register', 'getOrder', 'getOrderId', '__wakeup'],
  233. [],
  234. '',
  235. false
  236. );
  237. $order = $this->getMock(
  238. 'Magento\Sales\Model\Order',
  239. ['setCustomerNoteNotify', '__wakeup'],
  240. [],
  241. '',
  242. false
  243. );
  244. $this->request->expects($this->any())
  245. ->method('getParam')
  246. ->will(
  247. $this->returnValueMap(
  248. [
  249. ['order_id', null, $orderId],
  250. ['shipment_id', null, $shipmentId],
  251. ['shipment', null, $shipmentData],
  252. ['tracking', null, $tracking],
  253. ]
  254. )
  255. );
  256. $this->shipmentLoader->expects($this->any())
  257. ->method('setShipmentId')
  258. ->with($shipmentId);
  259. $this->shipmentLoader->expects($this->any())
  260. ->method('setOrderId')
  261. ->with($orderId);
  262. $this->shipmentLoader->expects($this->any())
  263. ->method('setShipment')
  264. ->with($shipmentData);
  265. $this->shipmentLoader->expects($this->any())
  266. ->method('setTracking')
  267. ->with($tracking);
  268. $this->shipmentLoader->expects($this->once())
  269. ->method('load')
  270. ->will($this->returnValue($shipment));
  271. $shipment->expects($this->once())
  272. ->method('register')
  273. ->will($this->returnSelf());
  274. $shipment->expects($this->any())
  275. ->method('getOrder')
  276. ->will($this->returnValue($order));
  277. $order->expects($this->once())
  278. ->method('setCustomerNoteNotify')
  279. ->with(false);
  280. $this->labelGenerator->expects($this->any())
  281. ->method('create')
  282. ->with($shipment, $this->request)
  283. ->will($this->returnValue(true));
  284. $saveTransaction = $this->getMockBuilder('Magento\Framework\DB\Transaction')
  285. ->disableOriginalConstructor()
  286. ->setMethods([])
  287. ->getMock();
  288. $saveTransaction->expects($this->at(0))
  289. ->method('addObject')
  290. ->with($shipment)
  291. ->will($this->returnSelf());
  292. $saveTransaction->expects($this->at(1))
  293. ->method('addObject')
  294. ->with($order)
  295. ->will($this->returnSelf());
  296. $saveTransaction->expects($this->at(2))
  297. ->method('save');
  298. $this->session->expects($this->once())
  299. ->method('getCommentText')
  300. ->with(true);
  301. $this->objectManager->expects($this->once())
  302. ->method('create')
  303. ->with('Magento\Framework\DB\Transaction')
  304. ->will($this->returnValue($saveTransaction));
  305. $this->objectManager->expects($this->once())
  306. ->method('get')
  307. ->with('Magento\Backend\Model\Session')
  308. ->will($this->returnValue($this->session));
  309. $path = 'sales/order/view';
  310. $arguments = ['order_id' => $orderId];
  311. $shipment->expects($this->once())
  312. ->method('getOrderId')
  313. ->will($this->returnValue($orderId));
  314. $this->prepareRedirect($path, $arguments);
  315. $this->saveAction->execute();
  316. $this->assertEquals($this->response, $this->saveAction->getResponse());
  317. }
  318. }
  319. /**
  320. * @return array
  321. */
  322. public function executeDataProvider()
  323. {
  324. return [
  325. [false, false],
  326. [true, false],
  327. [false, true],
  328. [true, true]
  329. ];
  330. }
  331. /**
  332. * @param string $path
  333. * @param array $arguments
  334. */
  335. protected function prepareRedirect($path, array $arguments = [])
  336. {
  337. $this->actionFlag->expects($this->any())
  338. ->method('get')
  339. ->with('', 'check_url_settings')
  340. ->will($this->returnValue(true));
  341. $this->session->expects($this->any())
  342. ->method('setIsUrlNotice')
  343. ->with(true);
  344. $url = $path . '/' . (!empty($arguments) ? $arguments['order_id'] : '');
  345. $this->helper->expects($this->atLeastOnce())
  346. ->method('getUrl')
  347. ->with($path, $arguments)
  348. ->will($this->returnValue($url));
  349. $this->response->expects($this->atLeastOnce())
  350. ->method('setRedirect')
  351. ->with($url);
  352. }
  353. }