Skip to content

Commit e8cd40c

Browse files
author
Oleksii Korshenko
authored
MAGETWO-83668: 8022: Uncaught Error: Call to a member function addItem() on array in app/code/Magento/Sales/Model/Order/Shipment.php(backport to 2.2) #12173
2 parents 24ce033 + 8024886 commit e8cd40c

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

app/code/Magento/Sales/Model/Order/Shipment.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ class Shipment extends AbstractModel implements EntityInterface, ShipmentInterfa
9393
*/
9494
protected $orderRepository;
9595

96+
/**
97+
* @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection|null
98+
*/
99+
private $tracksCollection = null;
100+
96101
/**
97102
* @param \Magento\Framework\Model\Context $context
98103
* @param \Magento\Framework\Registry $registry
@@ -331,16 +336,11 @@ public function addItem(\Magento\Sales\Model\Order\Shipment\Item $item)
331336
*/
332337
public function getTracksCollection()
333338
{
334-
if (!$this->hasData(ShipmentInterface::TRACKS)) {
335-
$this->setTracks($this->_trackCollectionFactory->create()->setShipmentFilter($this->getId()));
336-
337-
if ($this->getId()) {
338-
foreach ($this->getTracks() as $track) {
339-
$track->setShipment($this);
340-
}
341-
}
339+
if ($this->tracksCollection === null) {
340+
$this->tracksCollection = $this->_trackCollectionFactory->create()->setShipmentFilter($this->getId());
342341
}
343-
return $this->getTracks();
342+
343+
return $this->tracksCollection;
344344
}
345345

346346
/**

app/code/Magento/Sales/Model/ResourceModel/Order/Shipment/Relation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public function processRelation(\Magento\Framework\Model\AbstractModel $object)
6262
$this->shipmentItemResource->save($item);
6363
}
6464
}
65-
if (null !== $object->getTracks()) {
66-
foreach ($object->getTracks() as $track) {
65+
if (null !== $object->getTracksCollection()) {
66+
foreach ($object->getTracksCollection() as $track) {
6767
$track->setParentId($object->getId());
6868
$this->shipmentTrackResource->save($track);
6969
}

app/code/Magento/Sales/Test/Unit/Model/ResourceModel/Order/Shipment/RelationTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ protected function setUp()
8686
'getId',
8787
'getItems',
8888
'getTracks',
89-
'getComments'
89+
'getComments',
90+
'getTracksCollection',
9091
]
9192
)
9293
->getMock();
@@ -123,7 +124,7 @@ public function testProcessRelations()
123124
->method('getComments')
124125
->willReturn([$this->commentMock]);
125126
$this->shipmentMock->expects($this->exactly(2))
126-
->method('getTracks')
127+
->method('getTracksCollection')
127128
->willReturn([$this->trackMock]);
128129
$this->itemMock->expects($this->once())
129130
->method('setParentId')

dev/tests/integration/testsuite/Magento/Sales/Model/Order/ShipmentTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Sales\Model\Order;
78

89
/**
@@ -47,4 +48,35 @@ public function testPackages()
4748
$shipment->load($shipment->getId());
4849
$this->assertEquals($packages, $shipment->getPackages());
4950
}
51+
52+
/**
53+
* Check that getTracksCollection() always return collection instance.
54+
*
55+
* @magentoDataFixture Magento/Sales/_files/order.php
56+
*/
57+
public function testAddTrack()
58+
{
59+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
60+
61+
$order = $objectManager->create(\Magento\Sales\Model\Order::class);
62+
$order->loadByIncrementId('100000001');
63+
64+
/** @var \Magento\Sales\Model\Order\Shipment $shipment */
65+
$shipment = $objectManager->create(\Magento\Sales\Model\Order\Shipment::class);
66+
$shipment->setOrder($order);
67+
68+
$shipment->addItem($objectManager->create(\Magento\Sales\Model\Order\Shipment\Item::class));
69+
$shipment->save();
70+
71+
/** @var $track \Magento\Sales\Model\Order\Shipment\Track */
72+
$track = $objectManager->get(\Magento\Sales\Model\Order\Shipment\Track::class);
73+
$track->setNumber('Test Number')->setTitle('Test Title')->setCarrierCode('Test CODE');
74+
75+
$this->assertEmpty($shipment->getTracks());
76+
$shipment->addTrack($track)->save();
77+
78+
//to empty cache
79+
$shipment->setTracks(null);
80+
$this->assertNotEmpty($shipment->getTracks());
81+
}
5082
}

0 commit comments

Comments
 (0)