CanInvoiceTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\OrderValidator class
  10. */
  11. class CanInvoiceTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Sales\Model\Order\Validation\CanInvoice|\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(['getQtyToInvoice', 'getLockedDoInvoice'])
  39. ->getMockForAbstractClass();
  40. $this->model = new \Magento\Sales\Model\Order\Validation\CanInvoice();
  41. }
  42. /**
  43. * @param string $state
  44. *
  45. * @dataProvider canInvoiceWrongStateDataProvider
  46. */
  47. public function testCanInvoiceWrongState($state)
  48. {
  49. $this->orderMock->expects($this->any())
  50. ->method('getState')
  51. ->willReturn($state);
  52. $this->orderMock->expects($this->never())
  53. ->method('getItems');
  54. $this->orderMock->expects($this->once())
  55. ->method('getStatus')
  56. ->willReturn('status');
  57. $this->assertEquals(
  58. [__('An invoice 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 testCanInvoiceWrongState
  64. * @return array
  65. */
  66. public function canInvoiceWrongStateDataProvider()
  67. {
  68. return [
  69. [Order::STATE_PAYMENT_REVIEW],
  70. [Order::STATE_HOLDED],
  71. [Order::STATE_CANCELED],
  72. [Order::STATE_COMPLETE],
  73. [Order::STATE_CLOSED],
  74. ];
  75. }
  76. public function testCanInvoiceNoItems()
  77. {
  78. $this->orderMock->expects($this->any())
  79. ->method('getState')
  80. ->willReturn(Order::STATE_PROCESSING);
  81. $this->orderMock->expects($this->once())
  82. ->method('getItems')
  83. ->willReturn([]);
  84. $this->assertNotEmpty(
  85. $this->model->validate($this->orderMock)
  86. );
  87. }
  88. /**
  89. * @param float $qtyToInvoice
  90. * @param bool|null $itemLockedDoInvoice
  91. * @param bool $expectedResult
  92. *
  93. * @dataProvider canInvoiceDataProvider
  94. */
  95. public function testCanInvoice($qtyToInvoice, $itemLockedDoInvoice, $expectedResult)
  96. {
  97. $this->orderMock->expects($this->any())
  98. ->method('getState')
  99. ->willReturn(Order::STATE_PROCESSING);
  100. $items = [$this->orderItemMock];
  101. $this->orderMock->expects($this->once())
  102. ->method('getItems')
  103. ->willReturn($items);
  104. $this->orderItemMock->expects($this->any())
  105. ->method('getQtyToInvoice')
  106. ->willReturn($qtyToInvoice);
  107. $this->orderItemMock->expects($this->any())
  108. ->method('getLockedDoInvoice')
  109. ->willReturn($itemLockedDoInvoice);
  110. $this->assertEquals(
  111. $expectedResult,
  112. $this->model->validate($this->orderMock)
  113. );
  114. }
  115. /**
  116. * Data provider for testCanInvoice
  117. *
  118. * @return array
  119. */
  120. public function canInvoiceDataProvider()
  121. {
  122. return [
  123. [0, null, [__('The order does not allow an invoice to be created.')]],
  124. [-1, null, [__('The order does not allow an invoice to be created.')]],
  125. [1, true, [__('The order does not allow an invoice to be created.')]],
  126. [0.5, false, []],
  127. ];
  128. }
  129. }