RelationTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Model\ResourceModel\Order;
  7. /**
  8. * Class RelationTest
  9. */
  10. class RelationTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Sales\Model\ResourceModel\Order\Relation
  14. */
  15. protected $relationProcessor;
  16. /**
  17. * @var \Magento\Sales\Model\ResourceModel\Order\Handler\Address|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $addressHandlerMock;
  20. /**
  21. * @var \Magento\Sales\Api\OrderItemRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $orderItemRepositoryMock;
  24. /**
  25. * @var \Magento\Sales\Model\ResourceModel\Order\Payment|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $orderPaymentResourceMock;
  28. /**
  29. * @var \Magento\Sales\Model\ResourceModel\Order\Status\History|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $statusHistoryResource;
  32. /**
  33. * @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $orderMock;
  36. /**
  37. * @var \Magento\Sales\Model\Order\Item|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $orderItemMock;
  40. /**
  41. * @var \Magento\Sales\Model\Order\Payment|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $orderPaymentMock;
  44. /**
  45. * @var \Magento\Sales\Model\Order\Status\History|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $orderStatusHistoryMock;
  48. /**
  49. * @var \Magento\Sales\Model\Order\Invoice|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. protected $orderInvoiceMock;
  52. protected function setUp()
  53. {
  54. $this->addressHandlerMock = $this->getMockBuilder(
  55. \Magento\Sales\Model\ResourceModel\Order\Handler\Address::class
  56. )
  57. ->disableOriginalConstructor()
  58. ->setMethods(['removeEmptyAddresses', 'process'])
  59. ->getMock();
  60. $this->orderItemRepositoryMock = $this->getMockBuilder(\Magento\Sales\Api\OrderItemRepositoryInterface::class)
  61. ->disableOriginalConstructor()
  62. ->setMethods(['save'])
  63. ->getMockForAbstractClass();
  64. $this->orderPaymentResourceMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Payment::class)
  65. ->disableOriginalConstructor()
  66. ->setMethods(['save'])
  67. ->getMock();
  68. $this->statusHistoryResource = $this->getMockBuilder(
  69. \Magento\Sales\Model\ResourceModel\Order\Status\History::class
  70. )
  71. ->disableOriginalConstructor()
  72. ->setMethods(['save'])
  73. ->getMock();
  74. $this->orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  75. ->disableOriginalConstructor()
  76. ->setMethods(
  77. [
  78. 'getId',
  79. 'getItems',
  80. 'getPayment',
  81. 'getStatusHistories',
  82. 'getRelatedObjects'
  83. ]
  84. )
  85. ->getMock();
  86. $this->orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  87. ->disableOriginalConstructor()
  88. ->setMethods(['setOrderId', 'setOrder'])
  89. ->getMock();
  90. $this->orderPaymentMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Payment::class)
  91. ->disableOriginalConstructor()
  92. ->setMethods(['setParentId', 'setOrder'])
  93. ->getMock();
  94. $this->orderStatusHistoryMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  95. ->disableOriginalConstructor()
  96. ->setMethods(['setParentId', 'setOrder'])
  97. ->getMock();
  98. $this->orderStatusHistoryMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Status\History::class)
  99. ->disableOriginalConstructor()
  100. ->setMethods(['setParentId', 'setOrder'])
  101. ->getMock();
  102. $this->orderInvoiceMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
  103. ->disableOriginalConstructor()
  104. ->setMethods(['setOrder', 'save'])
  105. ->getMock();
  106. $this->relationProcessor = new \Magento\Sales\Model\ResourceModel\Order\Relation(
  107. $this->addressHandlerMock,
  108. $this->orderItemRepositoryMock,
  109. $this->orderPaymentResourceMock,
  110. $this->statusHistoryResource
  111. );
  112. }
  113. public function testProcessRelation()
  114. {
  115. $this->addressHandlerMock->expects($this->once())
  116. ->method('removeEmptyAddresses')
  117. ->with($this->orderMock)
  118. ->willReturnSelf();
  119. $this->addressHandlerMock->expects($this->once())
  120. ->method('process')
  121. ->with($this->orderMock)
  122. ->willReturnSelf();
  123. $this->orderMock->expects($this->exactly(2))
  124. ->method('getItems')
  125. ->willReturn([$this->orderItemMock]);
  126. $this->orderMock->expects($this->exactly(3))
  127. ->method('getId')
  128. ->willReturn('order-id-value');
  129. $this->orderItemMock->expects($this->once())
  130. ->method('setOrderId')
  131. ->with('order-id-value')
  132. ->willReturnSelf();
  133. $this->orderItemMock->expects($this->once())
  134. ->method('setOrder')
  135. ->with($this->orderMock)
  136. ->willReturnSelf();
  137. $this->orderItemRepositoryMock->expects($this->once())
  138. ->method('save')
  139. ->with($this->orderItemMock)
  140. ->willReturnSelf();
  141. $this->orderMock->expects($this->exactly(2))
  142. ->method('getPayment')
  143. ->willReturn($this->orderPaymentMock);
  144. $this->orderPaymentMock->expects($this->once())
  145. ->method('setParentId')
  146. ->with('order-id-value')
  147. ->willReturnSelf();
  148. $this->orderPaymentMock->expects($this->once())
  149. ->method('setOrder')
  150. ->with($this->orderMock)
  151. ->willReturnSelf();
  152. $this->orderPaymentResourceMock->expects($this->once())
  153. ->method('save')
  154. ->with($this->orderPaymentMock)
  155. ->willReturnSelf();
  156. $this->orderMock->expects($this->exactly(2))
  157. ->method('getStatusHistories')
  158. ->willReturn([$this->orderStatusHistoryMock]);
  159. $this->orderStatusHistoryMock->expects($this->once())
  160. ->method('setParentId')
  161. ->with('order-id-value')
  162. ->willReturnSelf();
  163. $this->orderStatusHistoryMock->expects($this->once())
  164. ->method('setOrder')
  165. ->with($this->orderMock)
  166. ->willReturnSelf();
  167. $this->statusHistoryResource->expects($this->once())
  168. ->method('save')
  169. ->with($this->orderStatusHistoryMock)
  170. ->willReturnSelf();
  171. $this->orderMock->expects($this->exactly(2))
  172. ->method('getRelatedObjects')
  173. ->willReturn([$this->orderInvoiceMock]);
  174. $this->orderInvoiceMock->expects($this->once())
  175. ->method('setOrder')
  176. ->with($this->orderMock)
  177. ->willReturnSelf();
  178. $this->orderInvoiceMock->expects($this->once())
  179. ->method('save')
  180. ->willReturnSelf();
  181. $this->relationProcessor->processRelation($this->orderMock);
  182. }
  183. }