Skip to content

Commit 294e3be

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-93057' into 2.3-develop-pr3
2 parents 86cb128 + 4783dc5 commit 294e3be

File tree

3 files changed

+168
-93
lines changed

3 files changed

+168
-93
lines changed

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

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Sales\Api\Data\ShipmentInterface;
1010
use Magento\Sales\Model\AbstractModel;
1111
use Magento\Sales\Model\EntityInterface;
12+
use Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\Collection as CommentsCollection;
1213

1314
/**
1415
* Sales order shipment model
@@ -94,9 +95,14 @@ class Shipment extends AbstractModel implements EntityInterface, ShipmentInterfa
9495
protected $orderRepository;
9596

9697
/**
97-
* @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection|null
98+
* @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection
9899
*/
99-
private $tracksCollection = null;
100+
private $tracksCollection;
101+
102+
/**
103+
* @var CommentsCollection
104+
*/
105+
private $commentsCollection;
100106

101107
/**
102108
* @param \Magento\Framework\Model\Context $context
@@ -414,43 +420,45 @@ public function addTrack(\Magento\Sales\Model\Order\Shipment\Track $track)
414420
public function addComment($comment, $notify = false, $visibleOnFront = false)
415421
{
416422
if (!$comment instanceof \Magento\Sales\Model\Order\Shipment\Comment) {
417-
$comment = $this->_commentFactory->create()->setComment(
418-
$comment
419-
)->setIsCustomerNotified(
420-
$notify
421-
)->setIsVisibleOnFront(
422-
$visibleOnFront
423-
);
423+
$comment = $this->_commentFactory->create()
424+
->setComment($comment)
425+
->setIsCustomerNotified($notify)
426+
->setIsVisibleOnFront($visibleOnFront);
424427
}
425-
$comment->setShipment($this)->setParentId($this->getId())->setStoreId($this->getStoreId());
428+
$comment->setShipment($this)
429+
->setParentId($this->getId())
430+
->setStoreId($this->getStoreId());
426431
if (!$comment->getId()) {
427432
$this->getCommentsCollection()->addItem($comment);
428433
}
434+
$comments = $this->getComments();
435+
$comments[] = $comment;
436+
$this->setComments($comments);
429437
$this->_hasDataChanges = true;
430438
return $this;
431439
}
432440

433441
/**
434-
* Retrieve comments collection.
442+
* Retrieves comments collection.
435443
*
436444
* @param bool $reload
437-
* @return \Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\Collection
445+
* @return CommentsCollection
438446
*/
439447
public function getCommentsCollection($reload = false)
440448
{
441-
if (!$this->hasData(ShipmentInterface::COMMENTS) || $reload) {
442-
$comments = $this->_commentCollectionFactory->create()
443-
->setShipmentFilter($this->getId())
444-
->setCreatedAtOrder();
445-
$this->setComments($comments);
446-
449+
if ($this->commentsCollection === null || $reload) {
450+
$this->commentsCollection = $this->_commentCollectionFactory->create();
447451
if ($this->getId()) {
448-
foreach ($this->getComments() as $comment) {
452+
$this->commentsCollection->setShipmentFilter($this->getId())
453+
->setCreatedAtOrder();
454+
455+
foreach ($this->commentsCollection as $comment) {
449456
$comment->setShipment($this);
450457
}
451458
}
452459
}
453-
return $this->getComments();
460+
461+
return $this->commentsCollection;
454462
}
455463

456464
/**
@@ -514,7 +522,7 @@ public function getPackages()
514522
}
515523

516524
/**
517-
* {@inheritdoc}
525+
* @inheritdoc
518526
* @codeCoverageIgnore
519527
*/
520528
public function setPackages(array $packages = null)
@@ -611,7 +619,7 @@ public function getCreatedAt()
611619
}
612620

613621
/**
614-
* {@inheritdoc}
622+
* @inheritdoc
615623
*/
616624
public function setCreatedAt($createdAt)
617625
{
@@ -743,95 +751,95 @@ public function setComments($comments = null)
743751
}
744752

745753
/**
746-
* {@inheritdoc}
754+
* @inheritdoc
747755
*/
748756
public function setStoreId($id)
749757
{
750758
return $this->setData(ShipmentInterface::STORE_ID, $id);
751759
}
752760

753761
/**
754-
* {@inheritdoc}
762+
* @inheritdoc
755763
*/
756764
public function setTotalWeight($totalWeight)
757765
{
758766
return $this->setData(ShipmentInterface::TOTAL_WEIGHT, $totalWeight);
759767
}
760768

761769
/**
762-
* {@inheritdoc}
770+
* @inheritdoc
763771
*/
764772
public function setTotalQty($qty)
765773
{
766774
return $this->setData(ShipmentInterface::TOTAL_QTY, $qty);
767775
}
768776

769777
/**
770-
* {@inheritdoc}
778+
* @inheritdoc
771779
*/
772780
public function setEmailSent($emailSent)
773781
{
774782
return $this->setData(ShipmentInterface::EMAIL_SENT, $emailSent);
775783
}
776784

777785
/**
778-
* {@inheritdoc}
786+
* @inheritdoc
779787
*/
780788
public function setOrderId($id)
781789
{
782790
return $this->setData(ShipmentInterface::ORDER_ID, $id);
783791
}
784792

785793
/**
786-
* {@inheritdoc}
794+
* @inheritdoc
787795
*/
788796
public function setCustomerId($id)
789797
{
790798
return $this->setData(ShipmentInterface::CUSTOMER_ID, $id);
791799
}
792800

793801
/**
794-
* {@inheritdoc}
802+
* @inheritdoc
795803
*/
796804
public function setShippingAddressId($id)
797805
{
798806
return $this->setData(ShipmentInterface::SHIPPING_ADDRESS_ID, $id);
799807
}
800808

801809
/**
802-
* {@inheritdoc}
810+
* @inheritdoc
803811
*/
804812
public function setBillingAddressId($id)
805813
{
806814
return $this->setData(ShipmentInterface::BILLING_ADDRESS_ID, $id);
807815
}
808816

809817
/**
810-
* {@inheritdoc}
818+
* @inheritdoc
811819
*/
812820
public function setShipmentStatus($shipmentStatus)
813821
{
814822
return $this->setData(ShipmentInterface::SHIPMENT_STATUS, $shipmentStatus);
815823
}
816824

817825
/**
818-
* {@inheritdoc}
826+
* @inheritdoc
819827
*/
820828
public function setIncrementId($id)
821829
{
822830
return $this->setData(ShipmentInterface::INCREMENT_ID, $id);
823831
}
824832

825833
/**
826-
* {@inheritdoc}
834+
* @inheritdoc
827835
*/
828836
public function setUpdatedAt($timestamp)
829837
{
830838
return $this->setData(ShipmentInterface::UPDATED_AT, $timestamp);
831839
}
832840

833841
/**
834-
* {@inheritdoc}
842+
* @inheritdoc
835843
*
836844
* @return \Magento\Sales\Api\Data\ShipmentExtensionInterface|null
837845
*/
@@ -841,7 +849,7 @@ public function getExtensionAttributes()
841849
}
842850

843851
/**
844-
* {@inheritdoc}
852+
* @inheritdoc
845853
*
846854
* @param \Magento\Sales\Api\Data\ShipmentExtensionInterface $extensionAttributes
847855
* @return $this

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

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ class ShipmentTest extends \PHPUnit\Framework\TestCase
2525
private $commentCollection;
2626

2727
/**
28-
* @var \Magento\Sales\Model\Order\shipment
28+
* @var Shipment
2929
*/
3030
private $shipmentModel;
3131

32+
/**
33+
* @return void
34+
*/
3235
protected function setUp()
3336
{
3437
$helperManager = new ObjectManager($this);
@@ -40,14 +43,22 @@ protected function setUp()
4043
]);
4144
}
4245

46+
/**
47+
* Test to Returns increment id
48+
*
49+
* @return void
50+
*/
4351
public function testGetIncrementId()
4452
{
4553
$this->shipmentModel->setIncrementId('test_increment_id');
4654
$this->assertEquals('test_increment_id', $this->shipmentModel->getIncrementId());
4755
}
4856

4957
/**
50-
* @covers \Magento\Sales\Model\Order\Shipment::getCommentsCollection
58+
* Test to Retrieves comments collection
59+
*
60+
* @return void
61+
* @throws \ReflectionException
5162
*/
5263
public function testGetCommentsCollection()
5364
{
@@ -58,35 +69,34 @@ public function testGetCommentsCollection()
5869
->disableOriginalConstructor()
5970
->setMethods(['setShipment'])
6071
->getMock();
61-
$shipmentItem->expects(static::once())
62-
->method('setShipment')
72+
$shipmentItem->method('setShipment')
6373
->with($this->shipmentModel);
6474
$collection = [$shipmentItem];
6575

66-
$this->commentCollection->expects(static::once())
76+
$this->commentCollection->expects(self::once())
6777
->method('setShipmentFilter')
6878
->with($shipmentId)
6979
->willReturnSelf();
70-
$this->commentCollection->expects(static::once())
80+
$this->commentCollection->expects(self::once())
7181
->method('setCreatedAtOrder')
7282
->willReturnSelf();
7383

74-
$this->commentCollection->expects(static::once())
75-
->method('load')
76-
->willReturnSelf();
77-
7884
$reflection = new \ReflectionClass(Collection::class);
7985
$reflectionProperty = $reflection->getProperty('_items');
8086
$reflectionProperty->setAccessible(true);
8187
$reflectionProperty->setValue($this->commentCollection, $collection);
8288

83-
$expected = $this->shipmentModel->getCommentsCollection();
89+
$actual = $this->shipmentModel->getCommentsCollection();
8490

85-
static::assertEquals($expected, $this->commentCollection);
91+
self::assertTrue(is_object($actual));
92+
self::assertEquals($this->commentCollection, $actual);
8693
}
8794

8895
/**
89-
* @covers \Magento\Sales\Model\Order\Shipment::getComments
96+
* Test to Returns comments
97+
*
98+
* @return void
99+
* @throws \ReflectionException
90100
*/
91101
public function testGetComments()
92102
{
@@ -97,30 +107,27 @@ public function testGetComments()
97107
->disableOriginalConstructor()
98108
->setMethods(['setShipment'])
99109
->getMock();
100-
$shipmentItem->expects(static::once())
110+
$shipmentItem->expects(self::once())
101111
->method('setShipment')
102112
->with($this->shipmentModel);
103113
$collection = [$shipmentItem];
104114

105-
$this->commentCollection->expects(static::once())
106-
->method('setShipmentFilter')
115+
$this->commentCollection->method('setShipmentFilter')
107116
->with($shipmentId)
108117
->willReturnSelf();
109118

110-
$this->commentCollection->expects(static::once())
111-
->method('load')
112-
->willReturnSelf();
113-
114119
$reflection = new \ReflectionClass(Collection::class);
115120
$reflectionProperty = $reflection->getProperty('_items');
116121
$reflectionProperty->setAccessible(true);
117122
$reflectionProperty->setValue($this->commentCollection, $collection);
118123

119-
$this->commentCollection->expects(static::once())
124+
$this->commentCollection->expects(self::once())
120125
->method('getItems')
121126
->willReturn($collection);
122127

123-
static::assertEquals($this->shipmentModel->getComments(), $collection);
128+
$actual = $this->shipmentModel->getComments();
129+
self::assertTrue(is_array($actual));
130+
self::assertEquals($collection, $actual);
124131
}
125132

126133
/**
@@ -131,16 +138,15 @@ private function initCommentsCollectionFactoryMock()
131138
{
132139
$this->commentCollection = $this->getMockBuilder(Collection::class)
133140
->disableOriginalConstructor()
134-
->setMethods(['setShipmentFilter', 'setCreatedAtOrder', 'getItems', 'load', '__wakeup'])
141+
->setMethods(['setShipmentFilter', 'setCreatedAtOrder', 'getItems', 'load'])
135142
->getMock();
136143

137144
$this->commentCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
138145
->disableOriginalConstructor()
139146
->setMethods(['create'])
140147
->getMock();
141148

142-
$this->commentCollectionFactory->expects(static::any())
143-
->method('create')
149+
$this->commentCollectionFactory->method('create')
144150
->willReturn($this->commentCollection);
145151
}
146152
}

0 commit comments

Comments
 (0)