ShipmentRepositoryTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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\Order;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. /**
  9. * Unit test for shipment repository class.
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class ShipmentRepositoryTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * Subject of testing.
  16. *
  17. * @var \Magento\Sales\Model\Order\ShipmentRepository
  18. */
  19. protected $subject;
  20. /**
  21. * Sales resource metadata.
  22. *
  23. * @var \Magento\Sales\Model\ResourceModel\Metadata|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $metadata;
  26. /**
  27. * @var \Magento\Sales\Api\Data\ShipmentSearchResultInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $searchResultFactory;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $collectionProcessor;
  34. protected function setUp()
  35. {
  36. $objectManager = new ObjectManager($this);
  37. $this->metadata = $this->createPartialMock(
  38. \Magento\Sales\Model\ResourceModel\Metadata::class,
  39. ['getNewInstance', 'getMapper']
  40. );
  41. $this->searchResultFactory = $this->createPartialMock(
  42. \Magento\Sales\Api\Data\ShipmentSearchResultInterfaceFactory::class,
  43. ['create']
  44. );
  45. $this->collectionProcessor = $this->createMock(
  46. \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
  47. );
  48. $this->subject = $objectManager->getObject(
  49. \Magento\Sales\Model\Order\ShipmentRepository::class,
  50. [
  51. 'metadata' => $this->metadata,
  52. 'searchResultFactory' => $this->searchResultFactory,
  53. 'collectionProcessor' => $this->collectionProcessor
  54. ]
  55. );
  56. }
  57. /**
  58. * @param int|null $id
  59. * @param int|null $entityId
  60. * @dataProvider getDataProvider
  61. */
  62. public function testGet($id, $entityId)
  63. {
  64. if (!$id) {
  65. $this->expectException(\Magento\Framework\Exception\InputException::class);
  66. $this->subject->get($id);
  67. } else {
  68. $shipment = $this->createPartialMock(\Magento\Sales\Model\Order\Shipment::class, ['load', 'getEntityId']);
  69. $shipment->expects($this->once())
  70. ->method('load')
  71. ->with($id)
  72. ->willReturn($shipment);
  73. $shipment->expects($this->once())
  74. ->method('getEntityId')
  75. ->willReturn($entityId);
  76. $this->metadata->expects($this->once())
  77. ->method('getNewInstance')
  78. ->willReturn($shipment);
  79. if (!$entityId) {
  80. $this->expectException(\Magento\Framework\Exception\NoSuchEntityException::class);
  81. $this->subject->get($id);
  82. } else {
  83. $this->assertEquals($shipment, $this->subject->get($id));
  84. $shipment->expects($this->never())
  85. ->method('load')
  86. ->with($id)
  87. ->willReturn($shipment);
  88. $shipment->expects($this->never())
  89. ->method('getEntityId')
  90. ->willReturn($entityId);
  91. $this->metadata->expects($this->never())
  92. ->method('getNewInstance')
  93. ->willReturn($shipment);
  94. // Retrieve Shipment from registry.
  95. $this->assertEquals($shipment, $this->subject->get($id));
  96. }
  97. }
  98. }
  99. /**
  100. * @return array
  101. */
  102. public function getDataProvider()
  103. {
  104. return [
  105. [null, null],
  106. [1, null],
  107. [1, 1]
  108. ];
  109. }
  110. public function testGetList()
  111. {
  112. $searchCriteria = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
  113. $collection = $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Shipment\Collection::class);
  114. $this->collectionProcessor->expects($this->once())
  115. ->method('process')
  116. ->with($searchCriteria, $collection);
  117. $this->searchResultFactory->expects($this->once())
  118. ->method('create')
  119. ->willReturn($collection);
  120. $this->assertEquals($collection, $this->subject->getList($searchCriteria));
  121. }
  122. public function testDelete()
  123. {
  124. $shipment = $this->createPartialMock(\Magento\Sales\Model\Order\Shipment::class, ['getEntityId']);
  125. $shipment->expects($this->once())
  126. ->method('getEntityId')
  127. ->willReturn(1);
  128. $mapper = $this->getMockForAbstractClass(
  129. \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
  130. [],
  131. '',
  132. false,
  133. true,
  134. true,
  135. ['delete']
  136. );
  137. $mapper->expects($this->once())
  138. ->method('delete')
  139. ->with($shipment);
  140. $this->metadata->expects($this->any())
  141. ->method('getMapper')
  142. ->willReturn($mapper);
  143. $this->assertTrue($this->subject->delete($shipment));
  144. }
  145. /**
  146. * @expectedException \Magento\Framework\Exception\CouldNotDeleteException
  147. * @expectedExceptionMessage The shipment couldn't be deleted.
  148. */
  149. public function testDeleteWithException()
  150. {
  151. $shipment = $this->createPartialMock(\Magento\Sales\Model\Order\Shipment::class, ['getEntityId']);
  152. $shipment->expects($this->never())
  153. ->method('getEntityId');
  154. $mapper = $this->getMockForAbstractClass(
  155. \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
  156. [],
  157. '',
  158. false,
  159. true,
  160. true,
  161. ['delete']
  162. );
  163. $mapper->expects($this->once())
  164. ->method('delete')
  165. ->willThrowException(new \Exception('error'));
  166. $this->metadata->expects($this->any())
  167. ->method('getMapper')
  168. ->willReturn($mapper);
  169. $this->subject->delete($shipment);
  170. }
  171. public function testSave()
  172. {
  173. $shipment = $this->createPartialMock(\Magento\Sales\Model\Order\Shipment::class, ['getEntityId']);
  174. $shipment->expects($this->any())
  175. ->method('getEntityId')
  176. ->willReturn(1);
  177. $mapper = $this->getMockForAbstractClass(
  178. \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
  179. [],
  180. '',
  181. false,
  182. true,
  183. true,
  184. ['save']
  185. );
  186. $mapper->expects($this->once())
  187. ->method('save')
  188. ->with($shipment);
  189. $this->metadata->expects($this->any())
  190. ->method('getMapper')
  191. ->willReturn($mapper);
  192. $this->assertEquals($shipment, $this->subject->save($shipment));
  193. }
  194. /**
  195. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  196. * @expectedExceptionMessage The shipment couldn't be saved.
  197. */
  198. public function testSaveWithException()
  199. {
  200. $shipment = $this->createPartialMock(\Magento\Sales\Model\Order\Shipment::class, ['getEntityId']);
  201. $shipment->expects($this->never())
  202. ->method('getEntityId');
  203. $mapper = $this->getMockForAbstractClass(
  204. \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
  205. [],
  206. '',
  207. false,
  208. true,
  209. true,
  210. ['save']
  211. );
  212. $mapper->expects($this->once())
  213. ->method('save')
  214. ->willThrowException(new \Exception('error'));
  215. $this->metadata->expects($this->any())
  216. ->method('getMapper')
  217. ->willReturn($mapper);
  218. $this->assertEquals($shipment, $this->subject->save($shipment));
  219. }
  220. public function testCreate()
  221. {
  222. $shipment = $this->createMock(\Magento\Sales\Model\Order\Shipment::class);
  223. $this->metadata->expects($this->once())
  224. ->method('getNewInstance')
  225. ->willReturn($shipment);
  226. $this->assertEquals($shipment, $this->subject->create());
  227. }
  228. }