|
5 | 5 | */
|
6 | 6 | namespace Magento\Sales\Test\Unit\Model\Order;
|
7 | 7 |
|
| 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 | + |
8 | 15 | class ShipmentTest extends \PHPUnit_Framework_TestCase
|
9 | 16 | {
|
| 17 | + /** |
| 18 | + * @var CollectionFactory|MockObject |
| 19 | + */ |
| 20 | + private $commentCollectionFactory; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var Collection|MockObject |
| 24 | + */ |
| 25 | + private $commentCollection; |
| 26 | + |
10 | 27 | /**
|
11 | 28 | * @var \Magento\Sales\Model\Order\shipment
|
12 | 29 | */
|
13 |
| - protected $shipmentModel; |
| 30 | + private $shipmentModel; |
14 | 31 |
|
15 | 32 | protected function setUp()
|
16 | 33 | {
|
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 | + ]); |
20 | 41 | }
|
21 | 42 |
|
22 | 43 | public function testGetIncrementId()
|
23 | 44 | {
|
24 | 45 | $this->shipmentModel->setIncrementId('test_increment_id');
|
25 | 46 | $this->assertEquals('test_increment_id', $this->shipmentModel->getIncrementId());
|
26 | 47 | }
|
| 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 | + } |
27 | 146 | }
|
0 commit comments