1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Test\Unit\Model\Order;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Sales\Model\Order\Shipment;
use Magento\Sales\Model\Order\Shipment\Item as ShipmentItem;
use Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\Collection;
use Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\CollectionFactory;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
class ShipmentTest extends \PHPUnit\Framework\TestCase
{
/**
* @var CollectionFactory|MockObject
*/
private $commentCollectionFactory;
/**
* @var Collection|MockObject
*/
private $commentCollection;
/**
* @var Shipment
*/
private $shipmentModel;
/**
* @return void
*/
protected function setUp()
{
$helperManager = new ObjectManager($this);
$this->initCommentsCollectionFactoryMock();
$this->shipmentModel = $helperManager->getObject(Shipment::class, [
'commentCollectionFactory' => $this->commentCollectionFactory
]);
}
/**
* Test to Returns increment id
*
* @return void
*/
public function testGetIncrementId()
{
$this->shipmentModel->setIncrementId('test_increment_id');
$this->assertEquals('test_increment_id', $this->shipmentModel->getIncrementId());
}
/**
* Test to Retrieves comments collection
*
* @return void
* @throws \ReflectionException
*/
public function testGetCommentsCollection()
{
$shipmentId = 1;
$this->shipmentModel->setId($shipmentId);
$shipmentItem = $this->getMockBuilder(ShipmentItem::class)
->disableOriginalConstructor()
->setMethods(['setShipment'])
->getMock();
$shipmentItem->method('setShipment')
->with($this->shipmentModel);
$collection = [$shipmentItem];
$this->commentCollection->expects(self::once())
->method('setShipmentFilter')
->with($shipmentId)
->willReturnSelf();
$this->commentCollection->expects(self::once())
->method('setCreatedAtOrder')
->willReturnSelf();
$reflection = new \ReflectionClass(Collection::class);
$reflectionProperty = $reflection->getProperty('_items');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->commentCollection, $collection);
$actual = $this->shipmentModel->getCommentsCollection();
self::assertTrue(is_object($actual));
self::assertEquals($this->commentCollection, $actual);
}
/**
* Test to Returns comments
*
* @return void
* @throws \ReflectionException
*/
public function testGetComments()
{
$shipmentId = 1;
$this->shipmentModel->setId($shipmentId);
$shipmentItem = $this->getMockBuilder(ShipmentItem::class)
->disableOriginalConstructor()
->setMethods(['setShipment'])
->getMock();
$shipmentItem->expects(self::once())
->method('setShipment')
->with($this->shipmentModel);
$collection = [$shipmentItem];
$this->commentCollection->method('setShipmentFilter')
->with($shipmentId)
->willReturnSelf();
$reflection = new \ReflectionClass(Collection::class);
$reflectionProperty = $reflection->getProperty('_items');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->commentCollection, $collection);
$this->commentCollection->expects(self::once())
->method('getItems')
->willReturn($collection);
$actual = $this->shipmentModel->getComments();
self::assertTrue(is_array($actual));
self::assertEquals($collection, $actual);
}
/**
* Creates mock for comments collection factory
* @return void
*/
private function initCommentsCollectionFactoryMock()
{
$this->commentCollection = $this->getMockBuilder(Collection::class)
->disableOriginalConstructor()
->setMethods(['setShipmentFilter', 'setCreatedAtOrder', 'getItems', 'load'])
->getMock();
$this->commentCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->commentCollectionFactory->method('create')
->willReturn($this->commentCollection);
}
}