CanShipTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\Validation;
  7. use Magento\Sales\Model\Order;
  8. /**
  9. * Test for \Magento\Sales\Model\Order\Validation\CanShip class
  10. */
  11. class CanShipTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Sales\Model\Order\Validation\CanShip|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. private $model;
  17. /**
  18. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  19. */
  20. private $objectManager;
  21. /**
  22. * @var \Magento\Sales\Api\Data\OrderInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $orderMock;
  25. /**
  26. * @var \Magento\Sales\Api\Data\OrderItemInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $orderItemMock;
  29. protected function setUp()
  30. {
  31. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  32. $this->orderMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderInterface::class)
  33. ->disableOriginalConstructor()
  34. ->setMethods(['getStatus', 'getItems'])
  35. ->getMockForAbstractClass();
  36. $this->orderItemMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderItemInterface::class)
  37. ->disableOriginalConstructor()
  38. ->setMethods(['getQtyToShip', 'getLockedDoShip'])
  39. ->getMockForAbstractClass();
  40. $this->model = new \Magento\Sales\Model\Order\Validation\CanShip();
  41. }
  42. /**
  43. * @param string $state
  44. *
  45. * @dataProvider canShipWrongStateDataProvider
  46. */
  47. public function testCanShipWrongState($state)
  48. {
  49. $this->orderMock->expects($this->any())
  50. ->method('getState')
  51. ->willReturn($state);
  52. $this->orderMock->expects($this->once())
  53. ->method('getStatus')
  54. ->willReturn('status');
  55. $this->orderMock->expects($this->never())
  56. ->method('getItems');
  57. $this->assertEquals(
  58. [__('A shipment cannot be created when an order has a status of %1', 'status')],
  59. $this->model->validate($this->orderMock)
  60. );
  61. }
  62. /**
  63. * Data provider for testCanShipWrongState
  64. * @return array
  65. */
  66. public function canShipWrongStateDataProvider()
  67. {
  68. return [
  69. [Order::STATE_PAYMENT_REVIEW],
  70. [Order::STATE_HOLDED],
  71. [Order::STATE_CANCELED],
  72. ];
  73. }
  74. public function testCanShipNoItems()
  75. {
  76. $this->orderMock->expects($this->any())
  77. ->method('getState')
  78. ->willReturn(Order::STATE_PROCESSING);
  79. $this->orderMock->expects($this->once())
  80. ->method('getItems')
  81. ->willReturn([]);
  82. $this->assertNotEmpty(
  83. $this->model->validate($this->orderMock)
  84. );
  85. }
  86. /**
  87. * @param float $qtyToShipment
  88. * @param bool|null $itemLockedDoShipment
  89. * @param bool $expectedResult
  90. *
  91. * @dataProvider canShipDataProvider
  92. */
  93. public function testCanShip($qtyToShipment, $itemLockedDoShipment, $expectedResult)
  94. {
  95. $this->orderMock->expects($this->any())
  96. ->method('getState')
  97. ->willReturn(Order::STATE_PROCESSING);
  98. $items = [$this->orderItemMock];
  99. $this->orderMock->expects($this->once())
  100. ->method('getItems')
  101. ->willReturn($items);
  102. $this->orderItemMock->expects($this->any())
  103. ->method('getQtyToShip')
  104. ->willReturn($qtyToShipment);
  105. $this->orderItemMock->expects($this->any())
  106. ->method('getLockedDoShip')
  107. ->willReturn($itemLockedDoShipment);
  108. $this->assertEquals(
  109. $expectedResult,
  110. $this->model->validate($this->orderMock)
  111. );
  112. }
  113. /**
  114. * Data provider for testCanShip
  115. *
  116. * @return array
  117. */
  118. public function canShipDataProvider()
  119. {
  120. return [
  121. [0, null, [__('The order does not allow a shipment to be created.')]],
  122. [-1, null, [__('The order does not allow a shipment to be created.')]],
  123. [1, true, [__('The order does not allow a shipment to be created.')]],
  124. [0.5, false, []],
  125. ];
  126. }
  127. }