Skip to content

Commit 5a84790

Browse files
author
Oleksii Korshenko
authored
MAGETWO-82656: #8022: Uncaught Error: Call to a member function addItem() on array in app/code/Magento/Sales/Model/Order/Shipment.php #11680
2 parents 19f3a9e + dd141a7 commit 5a84790

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
@@ -333,16 +338,11 @@ public function addItem(\Magento\Sales\Model\Order\Shipment\Item $item)
333338
*/
334339
public function getTracksCollection()
335340
{
336-
if (!$this->hasData(ShipmentInterface::TRACKS)) {
337-
$this->setTracks($this->_trackCollectionFactory->create()->setShipmentFilter($this->getId()));
338-
339-
if ($this->getId()) {
340-
foreach ($this->getTracks() as $track) {
341-
$track->setShipment($this);
342-
}
343-
}
341+
if ($this->tracksCollection === null) {
342+
$this->tracksCollection = $this->_trackCollectionFactory->create()->setShipmentFilter($this->getId());
344343
}
345-
return $this->getTracks();
344+
345+
return $this->tracksCollection;
346346
}
347347

348348
/**

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)