Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReturnValidatorTest.php
Go to the documentation of this file.
1 <?php
7 
14 
18 class ReturnValidatorTest extends \PHPUnit\Framework\TestCase
19 {
23  private $orderItemRepositoryMock;
24 
28  private $creditMemoMock;
29 
33  private $creditMemoItemMock;
34 
38  private $orderItemMock;
39 
43  private $returnValidator;
44 
45  protected function setUp()
46  {
47  $this->orderItemRepositoryMock = $this->getMockBuilder(OrderItemRepositoryInterface::class)
48  ->disableOriginalConstructor()
49  ->getMock();
50 
51  $this->creditMemoMock = $this->getMockBuilder(CreditmemoInterface::class)
52  ->disableOriginalConstructor()
53  ->getMock();
54 
55  $this->creditMemoItemMock = $this->getMockBuilder(CreditmemoItemInterface::class)
56  ->disableOriginalConstructor()
57  ->getMock();
58 
59  $this->orderItemMock = $this->getMockBuilder(OrderItemInterface::class)
60  ->disableOriginalConstructor()
61  ->getMock();
62 
63  $this->returnValidator = new ReturnValidator(
64  $this->orderItemRepositoryMock
65  );
66  }
67 
71  public function testValidate(
72  $expectedResult,
73  $returnToStockItems,
74  $orderItemId,
75  $creditMemoItemId,
76  $productSku = null
77  ) {
78  $this->creditMemoMock->expects($this->once())
79  ->method('getItems')
80  ->willReturn([$this->creditMemoItemMock]);
81 
82  $this->orderItemRepositoryMock->expects($this->once())
83  ->method('get')
84  ->with($returnToStockItems[0])
85  ->willReturn($this->orderItemMock);
86 
87  $this->orderItemMock->expects($this->once())
88  ->method('getItemId')
89  ->willReturn($orderItemId);
90 
91  $this->creditMemoItemMock->expects($this->once())
92  ->method('getOrderItemId')
93  ->willReturn($creditMemoItemId);
94 
95  if ($productSku) {
96  $this->orderItemMock->expects($this->once())
97  ->method('getSku')
98  ->willReturn($productSku);
99  }
100 
101  $this->assertEquals(
102  $this->returnValidator->validate($returnToStockItems, $this->creditMemoMock),
103  $expectedResult
104  );
105  }
106 
108  {
109  $returnToStockItems = [1];
110  $this->creditMemoMock->expects($this->once())
111  ->method('getItems')
112  ->willReturn([$this->creditMemoItemMock]);
113  $this->orderItemRepositoryMock->expects($this->once())
114  ->method('get')
115  ->with($returnToStockItems[0])
116  ->willThrowException(new NoSuchEntityException());
117 
118  $this->assertEquals(
119  $this->returnValidator->validate($returnToStockItems, $this->creditMemoMock),
120  __('The return to stock argument contains product item that is not part of the original order.')
121  );
122  }
123 
127  public function dataProvider()
128  {
129  return [
130  'PostirivValidationTest' => [null, [1], 1, 1],
131  'WithWrongReturnToStockItems' => [
132  __('The "%1" product is not part of the current creditmemo.', 'sku1'), [2], 2, 1, 'sku1',
133  ],
134  ];
135  }
136 }
__()
Definition: __.php:13
testValidate( $expectedResult, $returnToStockItems, $orderItemId, $creditMemoItemId, $productSku=null)