Skip to content

Commit ed946fc

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-52381' into MPI-BUGFIXES
2 parents bac009a + 57ee7a0 commit ed946fc

File tree

2 files changed

+139
-11
lines changed

2 files changed

+139
-11
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,16 +438,11 @@ public function addComment($comment, $notify = false, $visibleOnFront = false)
438438
public function getCommentsCollection($reload = false)
439439
{
440440
if (!$this->hasData(ShipmentInterface::COMMENTS) || $reload) {
441-
$comments = $this->_commentCollectionFactory->create()->setShipmentFilter($this->getId())
441+
$comments = $this->_commentCollectionFactory->create()
442+
->setShipmentFilter($this->getId())
442443
->setCreatedAtOrder();
443444
$this->setComments($comments);
444445

445-
/**
446-
* When shipment created with adding comment,
447-
* comments collection must be loaded before we added this comment.
448-
*/
449-
$this->getComments()->load();
450-
451446
if ($this->getId()) {
452447
foreach ($this->getComments() as $comment) {
453448
$comment->setShipment($this);
@@ -578,6 +573,7 @@ public function getTracks()
578573
}
579574

580575
//@codeCoverageIgnoreStart
576+
581577
/**
582578
* Returns tracks
583579
*
@@ -714,6 +710,19 @@ public function getUpdatedAt()
714710
*/
715711
public function getComments()
716712
{
713+
if (!$this->getId()) {
714+
return $this->getData(ShipmentInterface::COMMENTS);
715+
}
716+
717+
if ($this->getData(ShipmentInterface::COMMENTS) == null) {
718+
$collection = $this->_commentCollectionFactory->create()
719+
->setShipmentFilter($this->getId());
720+
721+
foreach ($collection as $item) {
722+
$item->setShipment($this);
723+
}
724+
$this->setData(ShipmentInterface::COMMENTS, $collection->getItems());
725+
}
717726
return $this->getData(ShipmentInterface::COMMENTS);
718727
}
719728

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

Lines changed: 123 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,142 @@
55
*/
66
namespace Magento\Sales\Test\Unit\Model\Order;
77

8+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9+
use Magento\Sales\Model\Order\Shipment;
10+
use Magento\Sales\Model\Order\Shipment\Item as ShipmentItem;
11+
use Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\Collection;
12+
use Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\CollectionFactory;
13+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
14+
815
class ShipmentTest extends \PHPUnit_Framework_TestCase
916
{
17+
/**
18+
* @var CollectionFactory|MockObject
19+
*/
20+
private $commentCollectionFactory;
21+
22+
/**
23+
* @var Collection|MockObject
24+
*/
25+
private $commentCollection;
26+
1027
/**
1128
* @var \Magento\Sales\Model\Order\shipment
1229
*/
13-
protected $shipmentModel;
30+
private $shipmentModel;
1431

1532
protected function setUp()
1633
{
17-
$helperManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
18-
$arguments = [];
19-
$this->shipmentModel = $helperManager->getObject('Magento\Sales\Model\Order\Shipment', $arguments);
34+
$helperManager = new ObjectManager($this);
35+
36+
$this->initCommentsCollectionFactoryMock();
37+
38+
$this->shipmentModel = $helperManager->getObject(Shipment::class, [
39+
'commentCollectionFactory' => $this->commentCollectionFactory
40+
]);
2041
}
2142

2243
public function testGetIncrementId()
2344
{
2445
$this->shipmentModel->setIncrementId('test_increment_id');
2546
$this->assertEquals('test_increment_id', $this->shipmentModel->getIncrementId());
2647
}
48+
49+
/**
50+
* @covers \Magento\Sales\Model\Order\Shipment::getCommentsCollection
51+
*/
52+
public function testGetCommentsCollection()
53+
{
54+
$shipmentId = 1;
55+
$this->shipmentModel->setId($shipmentId);
56+
57+
$shipmentItem = $this->getMockBuilder(ShipmentItem::class)
58+
->disableOriginalConstructor()
59+
->setMethods(['setShipment'])
60+
->getMock();
61+
$shipmentItem->expects(static::once())
62+
->method('setShipment')
63+
->with($this->shipmentModel);
64+
$collection = [$shipmentItem];
65+
66+
$this->commentCollection->expects(static::once())
67+
->method('setShipmentFilter')
68+
->with($shipmentId)
69+
->willReturnSelf();
70+
$this->commentCollection->expects(static::once())
71+
->method('setCreatedAtOrder')
72+
->willReturnSelf();
73+
74+
$this->commentCollection->expects(static::once())
75+
->method('load')
76+
->willReturnSelf();
77+
78+
$reflection = new \ReflectionClass(Collection::class);
79+
$reflectionProperty = $reflection->getProperty('_items');
80+
$reflectionProperty->setAccessible(true);
81+
$reflectionProperty->setValue($this->commentCollection, $collection);
82+
83+
$expected = $this->shipmentModel->getCommentsCollection();
84+
85+
static::assertEquals($expected, $this->commentCollection);
86+
}
87+
88+
/**
89+
* @covers \Magento\Sales\Model\Order\Shipment::getComments
90+
*/
91+
public function testGetComments()
92+
{
93+
$shipmentId = 1;
94+
$this->shipmentModel->setId($shipmentId);
95+
96+
$shipmentItem = $this->getMockBuilder(ShipmentItem::class)
97+
->disableOriginalConstructor()
98+
->setMethods(['setShipment'])
99+
->getMock();
100+
$shipmentItem->expects(static::once())
101+
->method('setShipment')
102+
->with($this->shipmentModel);
103+
$collection = [$shipmentItem];
104+
105+
$this->commentCollection->expects(static::once())
106+
->method('setShipmentFilter')
107+
->with($shipmentId)
108+
->willReturnSelf();
109+
110+
$this->commentCollection->expects(static::once())
111+
->method('load')
112+
->willReturnSelf();
113+
114+
$reflection = new \ReflectionClass(Collection::class);
115+
$reflectionProperty = $reflection->getProperty('_items');
116+
$reflectionProperty->setAccessible(true);
117+
$reflectionProperty->setValue($this->commentCollection, $collection);
118+
119+
$this->commentCollection->expects(static::once())
120+
->method('getItems')
121+
->willReturn($collection);
122+
123+
static::assertEquals($this->shipmentModel->getComments(), $collection);
124+
}
125+
126+
/**
127+
* Creates mock for comments collection factory
128+
* @return void
129+
*/
130+
private function initCommentsCollectionFactoryMock()
131+
{
132+
$this->commentCollection = $this->getMockBuilder(Collection::class)
133+
->disableOriginalConstructor()
134+
->setMethods(['setShipmentFilter', 'setCreatedAtOrder', 'getItems', 'load', '__wakeup'])
135+
->getMock();
136+
137+
$this->commentCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
138+
->disableOriginalConstructor()
139+
->setMethods(['create'])
140+
->getMock();
141+
142+
$this->commentCollectionFactory->expects(static::any())
143+
->method('create')
144+
->willReturn($this->commentCollection);
145+
}
27146
}

0 commit comments

Comments
 (0)