PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Sales/Test/Unit/Model/OrderInvoiceTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 393 lines | 264 code | 63 blank | 66 comment | 4 complexity | 7db519b82e03fc1ced08dc3210efae13 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Model;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\DB\Adapter\AdapterInterface;
  9. use Magento\Sales\Api\Data\InvoiceCommentCreationInterface;
  10. use Magento\Sales\Api\Data\InvoiceCreationArgumentsInterface;
  11. use Magento\Sales\Api\Data\InvoiceInterface;
  12. use Magento\Sales\Api\Data\OrderInterface;
  13. use Magento\Sales\Api\OrderRepositoryInterface;
  14. use Magento\Sales\Model\Order;
  15. use Magento\Sales\Model\Order\Config as OrderConfig;
  16. use Magento\Sales\Model\Order\Invoice\NotifierInterface;
  17. use Magento\Sales\Model\Order\InvoiceDocumentFactory;
  18. use Magento\Sales\Model\Order\InvoiceRepository;
  19. use Magento\Sales\Model\Order\InvoiceValidatorInterface;
  20. use Magento\Sales\Model\Order\OrderStateResolverInterface;
  21. use Magento\Sales\Model\Order\PaymentAdapterInterface;
  22. use Magento\Sales\Model\OrderInvoice;
  23. use Psr\Log\LoggerInterface;
  24. /**
  25. * Class OrderInvoiceTest
  26. * @SuppressWarnings(PHPMD.TooManyFields)
  27. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  28. */
  29. class OrderInvoiceTest extends \PHPUnit_Framework_TestCase
  30. {
  31. /**
  32. * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $resourceConnectionMock;
  35. /**
  36. * @var OrderRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $orderRepositoryMock;
  39. /**
  40. * @var InvoiceDocumentFactory|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $invoiceDocumentFactoryMock;
  43. /**
  44. * @var InvoiceValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $invoiceValidatorMock;
  47. /**
  48. * @var PaymentAdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  49. */
  50. private $paymentAdapterMock;
  51. /**
  52. * @var OrderStateResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  53. */
  54. private $orderStateResolverMock;
  55. /**
  56. * @var OrderConfig|\PHPUnit_Framework_MockObject_MockObject
  57. */
  58. private $configMock;
  59. /**
  60. * @var InvoiceRepository|\PHPUnit_Framework_MockObject_MockObject
  61. */
  62. private $invoiceRepositoryMock;
  63. /**
  64. * @var NotifierInterface|\PHPUnit_Framework_MockObject_MockObject
  65. */
  66. private $notifierInterfaceMock;
  67. /**
  68. * @var OrderInvoice|\PHPUnit_Framework_MockObject_MockObject
  69. */
  70. private $orderInvoice;
  71. /**
  72. * @var InvoiceCreationArgumentsInterface|\PHPUnit_Framework_MockObject_MockObject
  73. */
  74. private $invoiceCommentCreationMock;
  75. /**
  76. * @var InvoiceCommentCreationInterface|\PHPUnit_Framework_MockObject_MockObject
  77. */
  78. private $invoiceCreationArgumentsMock;
  79. /**
  80. * @var OrderInterface|\PHPUnit_Framework_MockObject_MockObject
  81. */
  82. private $orderMock;
  83. /**
  84. * @var InvoiceInterface|\PHPUnit_Framework_MockObject_MockObject
  85. */
  86. private $invoiceMock;
  87. /**
  88. * @var AdapterInterface
  89. */
  90. private $adapterInterface;
  91. /**
  92. * @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  93. */
  94. private $loggerMock;
  95. protected function setUp()
  96. {
  97. $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. $this->orderRepositoryMock = $this->getMockBuilder(OrderRepositoryInterface::class)
  101. ->disableOriginalConstructor()
  102. ->getMock();
  103. $this->invoiceDocumentFactoryMock = $this->getMockBuilder(InvoiceDocumentFactory::class)
  104. ->disableOriginalConstructor()
  105. ->getMock();
  106. $this->invoiceValidatorMock = $this->getMockBuilder(InvoiceValidatorInterface::class)
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $this->paymentAdapterMock = $this->getMockBuilder(PaymentAdapterInterface::class)
  110. ->disableOriginalConstructor()
  111. ->getMock();
  112. $this->orderStateResolverMock = $this->getMockBuilder(OrderStateResolverInterface::class)
  113. ->disableOriginalConstructor()
  114. ->getMock();
  115. $this->configMock = $this->getMockBuilder(OrderConfig::class)
  116. ->disableOriginalConstructor()
  117. ->getMock();
  118. $this->invoiceRepositoryMock = $this->getMockBuilder(InvoiceRepository::class)
  119. ->disableOriginalConstructor()
  120. ->getMock();
  121. $this->notifierInterfaceMock = $this->getMockBuilder(NotifierInterface::class)
  122. ->disableOriginalConstructor()
  123. ->getMock();
  124. $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $this->invoiceCommentCreationMock = $this->getMockBuilder(InvoiceCommentCreationInterface::class)
  128. ->disableOriginalConstructor()
  129. ->getMock();
  130. $this->invoiceCreationArgumentsMock = $this->getMockBuilder(InvoiceCreationArgumentsInterface::class)
  131. ->disableOriginalConstructor()
  132. ->getMock();
  133. $this->orderMock = $this->getMockBuilder(OrderInterface::class)
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. $this->invoiceMock = $this->getMockBuilder(InvoiceInterface::class)
  137. ->disableOriginalConstructor()
  138. ->getMock();
  139. $this->adapterInterface = $this->getMockBuilder(AdapterInterface::class)
  140. ->disableOriginalConstructor()
  141. ->getMock();
  142. $this->orderInvoice = new OrderInvoice(
  143. $this->resourceConnectionMock,
  144. $this->orderRepositoryMock,
  145. $this->invoiceDocumentFactoryMock,
  146. $this->invoiceValidatorMock,
  147. $this->paymentAdapterMock,
  148. $this->orderStateResolverMock,
  149. $this->configMock,
  150. $this->invoiceRepositoryMock,
  151. $this->notifierInterfaceMock,
  152. $this->loggerMock
  153. );
  154. }
  155. /**
  156. * @dataProvider dataProvider
  157. */
  158. public function testOrderInvoice($orderId, $capture, $items, $notify, $appendComment)
  159. {
  160. $this->resourceConnectionMock->expects($this->once())
  161. ->method('getConnection')
  162. ->with('sales')
  163. ->willReturn($this->adapterInterface);
  164. $this->orderRepositoryMock->expects($this->once())
  165. ->method('get')
  166. ->willReturn($this->orderMock);
  167. $this->invoiceDocumentFactoryMock->expects($this->once())
  168. ->method('create')
  169. ->with(
  170. $this->orderMock,
  171. $items,
  172. $this->invoiceCommentCreationMock,
  173. ($appendComment && $notify),
  174. $this->invoiceCreationArgumentsMock
  175. )->willReturn($this->invoiceMock);
  176. $this->invoiceValidatorMock->expects($this->once())
  177. ->method('validate')
  178. ->with($this->invoiceMock, $this->orderMock)
  179. ->willReturn([]);
  180. $this->paymentAdapterMock->expects($this->once())
  181. ->method('pay')
  182. ->with($this->orderMock, $this->invoiceMock, $capture)
  183. ->willReturn($this->orderMock);
  184. $this->orderStateResolverMock->expects($this->once())
  185. ->method('getStateForOrder')
  186. ->with($this->orderMock, [OrderStateResolverInterface::IN_PROGRESS])
  187. ->willReturn(Order::STATE_PROCESSING);
  188. $this->orderMock->expects($this->once())
  189. ->method('setState')
  190. ->with(Order::STATE_PROCESSING)
  191. ->willReturnSelf();
  192. $this->orderMock->expects($this->once())
  193. ->method('getState')
  194. ->willReturn(Order::STATE_PROCESSING);
  195. $this->configMock->expects($this->once())
  196. ->method('getStateDefaultStatus')
  197. ->with(Order::STATE_PROCESSING)
  198. ->willReturn('Processing');
  199. $this->orderMock->expects($this->once())
  200. ->method('setStatus')
  201. ->with('Processing')
  202. ->willReturnSelf();
  203. $this->invoiceMock->expects($this->once())
  204. ->method('setState')
  205. ->with(\Magento\Sales\Model\Order\Invoice::STATE_PAID)
  206. ->willReturnSelf();
  207. $this->invoiceRepositoryMock->expects($this->once())
  208. ->method('save')
  209. ->with($this->invoiceMock)
  210. ->willReturn($this->invoiceMock);
  211. $this->orderRepositoryMock->expects($this->once())
  212. ->method('save')
  213. ->with($this->orderMock)
  214. ->willReturn($this->orderMock);
  215. if ($notify) {
  216. $this->notifierInterfaceMock->expects($this->once())
  217. ->method('notify')
  218. ->with($this->orderMock, $this->invoiceMock, $this->invoiceCommentCreationMock);
  219. }
  220. $this->invoiceMock->expects($this->once())
  221. ->method('getEntityId')
  222. ->willReturn(2);
  223. $this->assertEquals(
  224. 2,
  225. $this->orderInvoice->execute(
  226. $orderId,
  227. $capture,
  228. $items,
  229. $notify,
  230. $appendComment,
  231. $this->invoiceCommentCreationMock,
  232. $this->invoiceCreationArgumentsMock
  233. )
  234. );
  235. }
  236. /**
  237. * @expectedException \Magento\Sales\Api\Exception\DocumentValidationExceptionInterface
  238. */
  239. public function testDocumentValidationException()
  240. {
  241. $orderId = 1;
  242. $capture = true;
  243. $items = [1 => 2];
  244. $notify = true;
  245. $appendComment = true;
  246. $errorMessages = ['error1', 'error2'];
  247. $this->orderRepositoryMock->expects($this->once())
  248. ->method('get')
  249. ->willReturn($this->orderMock);
  250. $this->invoiceDocumentFactoryMock->expects($this->once())
  251. ->method('create')
  252. ->with(
  253. $this->orderMock,
  254. $items,
  255. $this->invoiceCommentCreationMock,
  256. ($appendComment && $notify),
  257. $this->invoiceCreationArgumentsMock
  258. )->willReturn($this->invoiceMock);
  259. $this->invoiceValidatorMock->expects($this->once())
  260. ->method('validate')
  261. ->with($this->invoiceMock, $this->orderMock)
  262. ->willReturn($errorMessages);
  263. $this->orderInvoice->execute(
  264. $orderId,
  265. $capture,
  266. $items,
  267. $notify,
  268. $appendComment,
  269. $this->invoiceCommentCreationMock,
  270. $this->invoiceCreationArgumentsMock
  271. );
  272. }
  273. /**
  274. * @expectedException \Magento\Sales\Api\Exception\CouldNotInvoiceExceptionInterface
  275. */
  276. public function testCouldNotInvoiceException()
  277. {
  278. $orderId = 1;
  279. $items = [1 => 2];
  280. $capture = true;
  281. $notify = true;
  282. $appendComment = true;
  283. $this->resourceConnectionMock->expects($this->once())
  284. ->method('getConnection')
  285. ->with('sales')
  286. ->willReturn($this->adapterInterface);
  287. $this->orderRepositoryMock->expects($this->once())
  288. ->method('get')
  289. ->willReturn($this->orderMock);
  290. $this->invoiceDocumentFactoryMock->expects($this->once())
  291. ->method('create')
  292. ->with(
  293. $this->orderMock,
  294. $items,
  295. $this->invoiceCommentCreationMock,
  296. ($appendComment && $notify),
  297. $this->invoiceCreationArgumentsMock
  298. )->willReturn($this->invoiceMock);
  299. $this->invoiceValidatorMock->expects($this->once())
  300. ->method('validate')
  301. ->with($this->invoiceMock, $this->orderMock)
  302. ->willReturn([]);
  303. $e = new \Exception();
  304. $this->paymentAdapterMock->expects($this->once())
  305. ->method('pay')
  306. ->with($this->orderMock, $this->invoiceMock, $capture)
  307. ->willThrowException($e);
  308. $this->loggerMock->expects($this->once())
  309. ->method('critical')
  310. ->with($e);
  311. $this->adapterInterface->expects($this->once())
  312. ->method('rollBack');
  313. $this->orderInvoice->execute(
  314. $orderId,
  315. $capture,
  316. $items,
  317. $notify,
  318. $appendComment,
  319. $this->invoiceCommentCreationMock,
  320. $this->invoiceCreationArgumentsMock
  321. );
  322. }
  323. public function dataProvider()
  324. {
  325. return [
  326. 'TestWithNotifyTrue' => [1, true, [1 => 2], true, true],
  327. 'TestWithNotifyFalse' => [1, true, [1 => 2], false, true],
  328. ];
  329. }
  330. }