Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShipmentRepositoryTest.php
Go to the documentation of this file.
1 <?php
8 
10 
15 class ShipmentRepositoryTest extends \PHPUnit\Framework\TestCase
16 {
22  protected $subject;
23 
29  protected $metadata;
30 
35 
39  private $collectionProcessor;
40 
41  protected function setUp()
42  {
43  $objectManager = new ObjectManager($this);
44 
45  $this->metadata = $this->createPartialMock(
46  \Magento\Sales\Model\ResourceModel\Metadata::class,
47  ['getNewInstance', 'getMapper']
48  );
49 
50  $this->searchResultFactory = $this->createPartialMock(
51  \Magento\Sales\Api\Data\ShipmentSearchResultInterfaceFactory::class,
52  ['create']
53  );
54  $this->collectionProcessor = $this->createMock(
55  \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
56  );
57  $this->subject = $objectManager->getObject(
58  \Magento\Sales\Model\Order\ShipmentRepository::class,
59  [
60  'metadata' => $this->metadata,
61  'searchResultFactory' => $this->searchResultFactory,
62  'collectionProcessor' => $this->collectionProcessor
63  ]
64  );
65  }
66 
72  public function testGet($id, $entityId)
73  {
74  if (!$id) {
75  $this->expectException(\Magento\Framework\Exception\InputException::class);
76 
77  $this->subject->get($id);
78  } else {
79  $shipment = $this->createPartialMock(\Magento\Sales\Model\Order\Shipment::class, ['load', 'getEntityId']);
80  $shipment->expects($this->once())
81  ->method('load')
82  ->with($id)
83  ->willReturn($shipment);
84  $shipment->expects($this->once())
85  ->method('getEntityId')
86  ->willReturn($entityId);
87 
88  $this->metadata->expects($this->once())
89  ->method('getNewInstance')
90  ->willReturn($shipment);
91 
92  if (!$entityId) {
93  $this->expectException(\Magento\Framework\Exception\NoSuchEntityException::class);
94 
95  $this->subject->get($id);
96  } else {
97  $this->assertEquals($shipment, $this->subject->get($id));
98 
99  $shipment->expects($this->never())
100  ->method('load')
101  ->with($id)
102  ->willReturn($shipment);
103  $shipment->expects($this->never())
104  ->method('getEntityId')
105  ->willReturn($entityId);
106 
107  $this->metadata->expects($this->never())
108  ->method('getNewInstance')
109  ->willReturn($shipment);
110 
111  // Retrieve Shipment from registry.
112  $this->assertEquals($shipment, $this->subject->get($id));
113  }
114  }
115  }
116 
120  public function getDataProvider()
121  {
122  return [
123  [null, null],
124  [1, null],
125  [1, 1]
126  ];
127  }
128 
129  public function testGetList()
130  {
131  $searchCriteria = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
132 
133  $collection = $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Shipment\Collection::class);
134  $this->collectionProcessor->expects($this->once())
135  ->method('process')
136  ->with($searchCriteria, $collection);
137  $this->searchResultFactory->expects($this->once())
138  ->method('create')
139  ->willReturn($collection);
140 
141  $this->assertEquals($collection, $this->subject->getList($searchCriteria));
142  }
143 
144  public function testDelete()
145  {
146  $shipment = $this->createPartialMock(\Magento\Sales\Model\Order\Shipment::class, ['getEntityId']);
147  $shipment->expects($this->once())
148  ->method('getEntityId')
149  ->willReturn(1);
150 
151  $mapper = $this->getMockForAbstractClass(
152  \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
153  [],
154  '',
155  false,
156  true,
157  true,
158  ['delete']
159  );
160  $mapper->expects($this->once())
161  ->method('delete')
162  ->with($shipment);
163 
164  $this->metadata->expects($this->any())
165  ->method('getMapper')
166  ->willReturn($mapper);
167 
168  $this->assertTrue($this->subject->delete($shipment));
169  }
170 
175  public function testDeleteWithException()
176  {
177  $shipment = $this->createPartialMock(\Magento\Sales\Model\Order\Shipment::class, ['getEntityId']);
178  $shipment->expects($this->never())
179  ->method('getEntityId');
180 
181  $mapper = $this->getMockForAbstractClass(
182  \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
183  [],
184  '',
185  false,
186  true,
187  true,
188  ['delete']
189  );
190  $mapper->expects($this->once())
191  ->method('delete')
192  ->willThrowException(new \Exception('error'));
193 
194  $this->metadata->expects($this->any())
195  ->method('getMapper')
196  ->willReturn($mapper);
197 
198  $this->subject->delete($shipment);
199  }
200 
201  public function testSave()
202  {
203  $shipment = $this->createPartialMock(\Magento\Sales\Model\Order\Shipment::class, ['getEntityId']);
204  $shipment->expects($this->any())
205  ->method('getEntityId')
206  ->willReturn(1);
207 
208  $mapper = $this->getMockForAbstractClass(
209  \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
210  [],
211  '',
212  false,
213  true,
214  true,
215  ['save']
216  );
217  $mapper->expects($this->once())
218  ->method('save')
219  ->with($shipment);
220 
221  $this->metadata->expects($this->any())
222  ->method('getMapper')
223  ->willReturn($mapper);
224 
225  $this->assertEquals($shipment, $this->subject->save($shipment));
226  }
227 
232  public function testSaveWithException()
233  {
234  $shipment = $this->createPartialMock(\Magento\Sales\Model\Order\Shipment::class, ['getEntityId']);
235  $shipment->expects($this->never())
236  ->method('getEntityId');
237 
238  $mapper = $this->getMockForAbstractClass(
239  \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
240  [],
241  '',
242  false,
243  true,
244  true,
245  ['save']
246  );
247  $mapper->expects($this->once())
248  ->method('save')
249  ->willThrowException(new \Exception('error'));
250 
251  $this->metadata->expects($this->any())
252  ->method('getMapper')
253  ->willReturn($mapper);
254 
255  $this->assertEquals($shipment, $this->subject->save($shipment));
256  }
257 
258  public function testCreate()
259  {
260  $shipment = $this->createMock(\Magento\Sales\Model\Order\Shipment::class);
261 
262  $this->metadata->expects($this->once())
263  ->method('getNewInstance')
264  ->willReturn($shipment);
265 
266  $this->assertEquals($shipment, $this->subject->create());
267  }
268 }
$objectManager
Definition: bootstrap.php:17
$id
Definition: fieldset.phtml:14
$searchCriteria
foreach($order->getItems() as $orderItem) $shipment