From 52dff0b47ef921b1f08fda4015bdd042555e4b23 Mon Sep 17 00:00:00 2001 From: RomanKis Date: Fri, 10 Nov 2017 12:36:57 +0200 Subject: [PATCH] 8022: Uncaught Error: Call to a member function addItem() on array in app/code/Magento/Sales/Model/Order/Shipment.php(backport to 2.2) --- .../Magento/Sales/Model/Order/Shipment.php | 18 +++++------ .../ResourceModel/Order/Shipment/Relation.php | 4 +-- .../Order/Shipment/RelationTest.php | 5 +-- .../Sales/Model/Order/ShipmentTest.php | 32 +++++++++++++++++++ 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment.php b/app/code/Magento/Sales/Model/Order/Shipment.php index cb26fb611f219..64e20d5a69041 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment.php +++ b/app/code/Magento/Sales/Model/Order/Shipment.php @@ -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 @@ -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; } /** diff --git a/app/code/Magento/Sales/Model/ResourceModel/Order/Shipment/Relation.php b/app/code/Magento/Sales/Model/ResourceModel/Order/Shipment/Relation.php index 5851b2d936139..9c8671d02c578 100644 --- a/app/code/Magento/Sales/Model/ResourceModel/Order/Shipment/Relation.php +++ b/app/code/Magento/Sales/Model/ResourceModel/Order/Shipment/Relation.php @@ -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); } diff --git a/app/code/Magento/Sales/Test/Unit/Model/ResourceModel/Order/Shipment/RelationTest.php b/app/code/Magento/Sales/Test/Unit/Model/ResourceModel/Order/Shipment/RelationTest.php index 787e6f8e065d2..a7a615fb0f508 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/ResourceModel/Order/Shipment/RelationTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/ResourceModel/Order/Shipment/RelationTest.php @@ -86,7 +86,8 @@ protected function setUp() 'getId', 'getItems', 'getTracks', - 'getComments' + 'getComments', + 'getTracksCollection', ] ) ->getMock(); @@ -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') diff --git a/dev/tests/integration/testsuite/Magento/Sales/Model/Order/ShipmentTest.php b/dev/tests/integration/testsuite/Magento/Sales/Model/Order/ShipmentTest.php index bfe839c0dfe9d..46820d1440ca6 100644 --- a/dev/tests/integration/testsuite/Magento/Sales/Model/Order/ShipmentTest.php +++ b/dev/tests/integration/testsuite/Magento/Sales/Model/Order/ShipmentTest.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Sales\Model\Order; /** @@ -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()); + } }