OrderTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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;
  7. use Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. use Magento\Sales\Model\ResourceModel\Order;
  10. /**
  11. * Class OrderTest
  12. *
  13. * @SuppressWarnings(PHPMD.TooManyFields)
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class OrderTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var \Magento\Sales\Model\ResourceModel\Order
  20. */
  21. protected $resource;
  22. /**
  23. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $resourceMock;
  26. /**
  27. * @var \Magento\SalesSequence\Model\Manager|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $salesSequenceManagerMock;
  30. /**
  31. * @var \Magento\SalesSequence\Model\Sequence|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $salesSequenceMock;
  34. /**
  35. * @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $orderMock;
  38. /**
  39. * @var \Magento\Sales\Model\Order\Item|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $orderItemMock;
  42. /**
  43. * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $storeMock;
  46. /**
  47. * @var \Magento\Store\Model\Website|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $websiteMock;
  50. /**
  51. * @var \Magento\Store\Model\Group|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $storeGroupMock;
  54. /**
  55. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected $connectionMock;
  58. /**
  59. * @var \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot|\PHPUnit_Framework_MockObject_MockObject
  60. */
  61. protected $entitySnapshotMock;
  62. /**
  63. * @var RelationComposite|\PHPUnit_Framework_MockObject_MockObject
  64. */
  65. protected $relationCompositeMock;
  66. /**
  67. * @var \Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor|\PHPUnit_Framework_MockObject_MockObject
  68. */
  69. protected $objectRelationProcessorMock;
  70. /**
  71. * Mock class dependencies
  72. */
  73. protected function setUp()
  74. {
  75. $this->resourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  76. $this->orderMock = $this->createMock(\Magento\Sales\Model\Order::class);
  77. $this->orderItemMock = $this->createPartialMock(
  78. \Magento\Sales\Model\Order\Item::class,
  79. ['getQuoteParentItemId', 'setTotalItemCount', 'getChildrenItems']
  80. );
  81. $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  82. $this->storeGroupMock = $this->createPartialMock(
  83. \Magento\Store\Model\Group::class,
  84. ['getName', 'getDefaultStoreId']
  85. );
  86. $this->websiteMock = $this->createPartialMock(\Magento\Store\Model\Website::class, ['getName']);
  87. $this->connectionMock = $this->createPartialMock(
  88. \Magento\Framework\DB\Adapter\Pdo\Mysql::class,
  89. [
  90. 'describeTable',
  91. 'insert',
  92. 'lastInsertId',
  93. 'beginTransaction',
  94. 'rollback',
  95. 'commit',
  96. 'quoteInto',
  97. 'update'
  98. ]
  99. );
  100. $this->salesSequenceManagerMock = $this->createMock(\Magento\SalesSequence\Model\Manager::class);
  101. $this->salesSequenceMock = $this->createMock(\Magento\SalesSequence\Model\Sequence::class);
  102. $this->entitySnapshotMock = $this->createMock(
  103. \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot::class
  104. );
  105. $this->relationCompositeMock = $this->createMock(
  106. \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite::class
  107. );
  108. $this->objectRelationProcessorMock = $this->createMock(
  109. \Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor::class
  110. );
  111. $contextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
  112. $contextMock->expects($this->once())->method('getResources')->willReturn($this->resourceMock);
  113. $contextMock->expects($this->once())
  114. ->method('getObjectRelationProcessor')
  115. ->willReturn($this->objectRelationProcessorMock);
  116. $objectManager = new ObjectManagerHelper($this);
  117. $this->resource = $objectManager->getObject(
  118. \Magento\Sales\Model\ResourceModel\Order::class,
  119. [
  120. 'context' => $contextMock,
  121. 'sequenceManager' => $this->salesSequenceManagerMock,
  122. 'entitySnapshot' => $this->entitySnapshotMock,
  123. 'entityRelationComposite' => $this->relationCompositeMock
  124. ]
  125. );
  126. }
  127. public function testSave()
  128. {
  129. $this->orderMock->expects($this->exactly(3))
  130. ->method('getId')
  131. ->willReturn(null);
  132. $this->orderItemMock->expects($this->once())
  133. ->method('getChildrenItems')
  134. ->willReturn([]);
  135. $this->orderItemMock->expects($this->once())
  136. ->method('getQuoteParentItemId')
  137. ->willReturn(null);
  138. $this->orderMock->expects($this->once())
  139. ->method('setTotalItemCount')
  140. ->with(1);
  141. $this->storeGroupMock->expects($this->once())
  142. ->method('getDefaultStoreId')
  143. ->willReturn(1);
  144. $this->orderMock->expects($this->once())
  145. ->method('getAllItems')
  146. ->willReturn([$this->orderItemMock]);
  147. $this->orderMock->expects($this->once())
  148. ->method('validateBeforeSave')
  149. ->willReturnSelf();
  150. $this->orderMock->expects($this->once())
  151. ->method('beforeSave')
  152. ->willReturnSelf();
  153. $this->orderMock->expects($this->once())
  154. ->method('isSaveAllowed')
  155. ->willReturn(true);
  156. $this->orderMock->expects($this->once())
  157. ->method('getEntityType')
  158. ->willReturn('order');
  159. $this->orderMock->expects($this->exactly(2))
  160. ->method('getStore')
  161. ->willReturn($this->storeMock);
  162. $this->storeMock->expects($this->exactly(2))
  163. ->method('getGroup')
  164. ->willReturn($this->storeGroupMock);
  165. $this->storeMock->expects($this->once())
  166. ->method('getWebsite')
  167. ->willReturn($this->websiteMock);
  168. $this->storeGroupMock->expects($this->once())
  169. ->method('getDefaultStoreId')
  170. ->willReturn(1);
  171. $this->salesSequenceManagerMock->expects($this->once())
  172. ->method('getSequence')
  173. ->with('order', 1)
  174. ->willReturn($this->salesSequenceMock);
  175. $this->salesSequenceMock->expects($this->once())
  176. ->method('getNextValue')
  177. ->willReturn('10000001');
  178. $this->orderMock->expects($this->once())
  179. ->method('setIncrementId')
  180. ->with('10000001')
  181. ->willReturnSelf();
  182. $this->orderMock->expects($this->once())
  183. ->method('getIncrementId')
  184. ->willReturn(null);
  185. $this->orderMock->expects($this->once())
  186. ->method('getData')
  187. ->willReturn(['increment_id' => '10000001']);
  188. $this->objectRelationProcessorMock->expects($this->once())
  189. ->method('validateDataIntegrity')
  190. ->with(null, ['increment_id' => '10000001']);
  191. $this->relationCompositeMock->expects($this->once())
  192. ->method('processRelations')
  193. ->with($this->orderMock);
  194. $this->resourceMock->expects($this->any())
  195. ->method('getConnection')
  196. ->willReturn($this->connectionMock);
  197. $this->connectionMock->expects($this->any())
  198. ->method('quoteInto');
  199. $this->connectionMock->expects($this->any())
  200. ->method('describeTable')
  201. ->will($this->returnValue([]));
  202. $this->connectionMock->expects($this->any())
  203. ->method('update');
  204. $this->connectionMock->expects($this->any())
  205. ->method('lastInsertId');
  206. $this->orderMock->expects($this->any())
  207. ->method('getId')
  208. ->will($this->returnValue(1));
  209. $this->entitySnapshotMock->expects($this->once())
  210. ->method('isModified')
  211. ->with($this->orderMock)
  212. ->will($this->returnValue(true));
  213. $this->resource->save($this->orderMock);
  214. }
  215. }