Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions app/code/Magento/Sales/Model/Order/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class Shipment extends AbstractModel implements EntityInterface, ShipmentInterfa
*/
protected $orderRepository;

/**
* @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection|null
*/
private $tracksCollection = null;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand Down Expand Up @@ -331,16 +336,11 @@ public function addItem(\Magento\Sales\Model\Order\Shipment\Item $item)
*/
public function getTracksCollection()
{
if (!$this->hasData(ShipmentInterface::TRACKS)) {
$this->setTracks($this->_trackCollectionFactory->create()->setShipmentFilter($this->getId()));

if ($this->getId()) {
foreach ($this->getTracks() as $track) {
$track->setShipment($this);
}
}
if ($this->tracksCollection === null) {
$this->tracksCollection = $this->_trackCollectionFactory->create()->setShipmentFilter($this->getId());
}
return $this->getTracks();

return $this->tracksCollection;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public function processRelation(\Magento\Framework\Model\AbstractModel $object)
$this->shipmentItemResource->save($item);
}
}
if (null !== $object->getTracks()) {
foreach ($object->getTracks() as $track) {
if (null !== $object->getTracksCollection()) {
foreach ($object->getTracksCollection() as $track) {
$track->setParentId($object->getId());
$this->shipmentTrackResource->save($track);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ protected function setUp()
'getId',
'getItems',
'getTracks',
'getComments'
'getComments',
'getTracksCollection',
]
)
->getMock();
Expand Down Expand Up @@ -123,7 +124,7 @@ public function testProcessRelations()
->method('getComments')
->willReturn([$this->commentMock]);
$this->shipmentMock->expects($this->exactly(2))
->method('getTracks')
->method('getTracksCollection')
->willReturn([$this->trackMock]);
$this->itemMock->expects($this->once())
->method('setParentId')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sales\Model\Order;

/**
Expand Down Expand Up @@ -47,4 +48,35 @@ public function testPackages()
$shipment->load($shipment->getId());
$this->assertEquals($packages, $shipment->getPackages());
}

/**
* Check that getTracksCollection() always return collection instance.
*
* @magentoDataFixture Magento/Sales/_files/order.php
*/
public function testAddTrack()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

$order = $objectManager->create(\Magento\Sales\Model\Order::class);
$order->loadByIncrementId('100000001');

/** @var \Magento\Sales\Model\Order\Shipment $shipment */
$shipment = $objectManager->create(\Magento\Sales\Model\Order\Shipment::class);
$shipment->setOrder($order);

$shipment->addItem($objectManager->create(\Magento\Sales\Model\Order\Shipment\Item::class));
$shipment->save();

/** @var $track \Magento\Sales\Model\Order\Shipment\Track */
$track = $objectManager->get(\Magento\Sales\Model\Order\Shipment\Track::class);
$track->setNumber('Test Number')->setTitle('Test Title')->setCarrierCode('Test CODE');

$this->assertEmpty($shipment->getTracks());
$shipment->addTrack($track)->save();

//to empty cache
$shipment->setTracks(null);
$this->assertNotEmpty($shipment->getTracks());
}
}