Skip to content

Commit 2533e88

Browse files
committed
MAGETWO-95773: Credit memo is created instead of returning error via invoice refund API for Bundle product
- Update unit test coverage to include changed functionality
1 parent 547c6fe commit 2533e88

File tree

1 file changed

+60
-34
lines changed

1 file changed

+60
-34
lines changed

app/code/Magento/Sales/Test/Unit/Model/Order/ItemRepositoryTest.php

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function testGetEmptyEntity()
145145
$model->get($orderItemId);
146146
}
147147

148-
public function testGet()
148+
public function testGetAsParentWithChild()
149149
{
150150
$orderItemId = 1;
151151
$productType = 'configurable';
@@ -154,18 +154,27 @@ public function testGet()
154154

155155
$this->getProductOptionExtensionMock();
156156
$productOption = $this->getProductOptionMock();
157-
$orderItemMock = $this->getOrderMock($productType, $productOption);
157+
$orderItemMock = $this->getOrderItemMock($productType, $productOption);
158+
159+
$orderItemCollectionMock = $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Item\Collection::class);
160+
$orderItemCollectionMock->expects($this->once())
161+
->method('filterByParent')
162+
->with($orderItemId)
163+
->willReturnSelf();
164+
$orderItemCollectionMock->expects($this->once())
165+
->method('getItems')
166+
->willReturn([$orderItemMock]);
158167

159168
$orderMock = $this->createMock(\Magento\Sales\Model\Order::class);
160169
$orderMock->expects($this->once())
161-
->method('getAllItems')
162-
->willReturn([$orderItemMock]);
170+
->method('getItemsCollection')
171+
->willReturn($orderItemCollectionMock);
163172

164173
$orderItemMock->expects($this->once())
165174
->method('load')
166175
->with($orderItemId)
167176
->willReturn($orderItemMock);
168-
$orderItemMock->expects($this->exactly(2))
177+
$orderItemMock->expects($this->exactly(3))
169178
->method('getItemId')
170179
->willReturn($orderItemId);
171180
$orderItemMock->expects($this->once())
@@ -183,6 +192,45 @@ public function testGet()
183192
$this->assertSame($orderItemMock, $model->get($orderItemId));
184193
}
185194

195+
public function testGetAsChild()
196+
{
197+
$orderItemId = 1;
198+
$parentItemId = 66;
199+
$productType = 'configurable';
200+
201+
$this->productOptionData = ['option1' => 'value1'];
202+
203+
$this->getProductOptionExtensionMock();
204+
$productOption = $this->getProductOptionMock();
205+
$orderItemMock = $this->getOrderItemMock($productType, $productOption);
206+
207+
$orderItemMock->expects($this->once())
208+
->method('load')
209+
->with($orderItemId)
210+
->willReturn($orderItemMock);
211+
$orderItemMock->expects($this->once())
212+
->method('getItemId')
213+
->willReturn($orderItemId);
214+
$orderItemMock->expects($this->exactly(3))
215+
->method('getParentItemId')
216+
->willReturn($parentItemId);
217+
218+
$this->metadata->expects($this->once())
219+
->method('getNewInstance')
220+
->willReturn($orderItemMock);
221+
222+
$parentItemMock = $this->createMock(\Magento\Sales\Model\Order\Item::class);
223+
224+
$model = $this->getModel($orderItemMock, $productType);
225+
$reflectedRegistryProperty = new \ReflectionProperty($model, 'registry');
226+
$reflectedRegistryProperty->setAccessible(true);
227+
$reflectedRegistryProperty->setValue($model, [$parentItemId => $parentItemMock]);
228+
$this->assertSame($orderItemMock, $model->get($orderItemId));
229+
230+
// Assert already registered
231+
$this->assertSame($orderItemMock, $model->get($orderItemId));
232+
}
233+
186234
public function testGetList()
187235
{
188236
$productType = 'configurable';
@@ -192,7 +240,7 @@ public function testGetList()
192240
->getMock();
193241
$this->getProductOptionExtensionMock();
194242
$productOption = $this->getProductOptionMock();
195-
$orderItemMock = $this->getOrderMock($productType, $productOption);
243+
$orderItemMock = $this->getOrderItemMock($productType, $productOption);
196244

197245
$searchResultMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Item\Collection::class)
198246
->disableOriginalConstructor()
@@ -214,35 +262,12 @@ public function testDeleteById()
214262
$orderItemId = 1;
215263
$productType = 'configurable';
216264

217-
$requestMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
218-
->disableOriginalConstructor()
219-
->getMock();
220-
221265
$orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
222266
->disableOriginalConstructor()
223267
->getMock();
224-
225-
$orderMock = $this->createMock(\Magento\Sales\Model\Order::class);
226-
$orderMock->expects($this->once())
227-
->method('getAllItems')
228-
->willReturn([$orderItemMock]);
229-
230268
$orderItemMock->expects($this->once())
231-
->method('load')
232-
->with($orderItemId)
233-
->willReturn($orderItemMock);
234-
$orderItemMock->expects($this->exactly(2))
235-
->method('getItemId')
269+
->method('getEntityId')
236270
->willReturn($orderItemId);
237-
$orderItemMock->expects($this->once())
238-
->method('getProductType')
239-
->willReturn($productType);
240-
$orderItemMock->expects($this->once())
241-
->method('getBuyRequest')
242-
->willReturn($requestMock);
243-
$orderItemMock->expects($this->once())
244-
->method('getOrder')
245-
->willReturn($orderMock);
246271

247272
$orderItemResourceMock = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\Db\AbstractDb::class)
248273
->disableOriginalConstructor()
@@ -252,15 +277,16 @@ public function testDeleteById()
252277
->with($orderItemMock)
253278
->willReturnSelf();
254279

255-
$this->metadata->expects($this->once())
256-
->method('getNewInstance')
257-
->willReturn($orderItemMock);
258280
$this->metadata->expects($this->exactly(1))
259281
->method('getMapper')
260282
->willReturn($orderItemResourceMock);
261283

262284
$model = $this->getModel($orderItemMock, $productType);
285+
$reflectedRegistryProperty = new \ReflectionProperty($model, 'registry');
286+
$reflectedRegistryProperty->setAccessible(true);
287+
$reflectedRegistryProperty->setValue($model, [$orderItemId => $orderItemMock]);
263288
$this->assertTrue($model->deleteById($orderItemId));
289+
$this->assertEmpty($reflectedRegistryProperty->getValue($model));
264290
}
265291

266292
/**
@@ -318,7 +344,7 @@ protected function getModel(
318344
* @param \PHPUnit_Framework_MockObject_MockObject $productOption
319345
* @return \PHPUnit_Framework_MockObject_MockObject
320346
*/
321-
protected function getOrderMock($productType, $productOption)
347+
protected function getOrderItemMock($productType, $productOption)
322348
{
323349
$requestMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
324350
->disableOriginalConstructor()

0 commit comments

Comments
 (0)