ShipmentTest.php 4.6 KB

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