Skip to content

Commit a211ad8

Browse files
committed
MAGETWO-54680: Unit tests
1 parent 2164122 commit a211ad8

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

app/code/Magento/Sales/Test/Unit/Model/Order/Creditmemo/Validation/QuantityValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function testValidateWithoutOrder()
9999
->willReturn(null);
100100
$creditmemoMock->expects($this->never())->method('getItems');
101101
$this->assertEquals(
102-
[__('Order Id is required for shipment document')],
102+
[__('Order Id is required for creditmemo document')],
103103
$this->validator->validate($creditmemoMock)
104104
);
105105
}

app/code/Magento/Sales/Test/Unit/Model/RefundOrderTest.php

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
use Magento\Sales\Model\Order;
1717
use Magento\Sales\Model\Order\Config as OrderConfig;
1818
use Magento\Sales\Model\Order\Creditmemo\CreditmemoValidatorInterface;
19+
use Magento\Sales\Model\Order\Creditmemo\Item\Validation\CreationQuantityValidator;
1920
use Magento\Sales\Model\Order\CreditmemoDocumentFactory;
2021
use Magento\Sales\Model\Order\OrderStateResolverInterface;
2122
use Magento\Sales\Model\Order\OrderValidatorInterface;
2223
use Magento\Sales\Model\Order\PaymentAdapterInterface;
2324
use Magento\Sales\Model\Order\Creditmemo\NotifierInterface;
2425
use Magento\Sales\Model\RefundOrder;
2526
use Psr\Log\LoggerInterface;
27+
use Magento\Sales\Api\Data\CreditmemoItemCreationInterface;
2628

2729
/**
2830
* Class RefundOrderTest
@@ -116,78 +118,80 @@ class RefundOrderTest extends \PHPUnit_Framework_TestCase
116118
*/
117119
private $loggerMock;
118120

121+
/**
122+
* @var Order\Creditmemo\ItemCreationValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
123+
*/
124+
private $itemCreationValidatorMock;
125+
126+
/**
127+
* @var CreditmemoItemCreationInterface|\PHPUnit_Framework_MockObject_MockObject
128+
*/
129+
private $creditmemoItemCreationMock;
130+
119131
protected function setUp()
120132
{
121133
$this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
122134
->disableOriginalConstructor()
123135
->getMock();
124-
125136
$this->orderRepositoryMock = $this->getMockBuilder(OrderRepositoryInterface::class)
126137
->disableOriginalConstructor()
127138
->getMock();
128-
129139
$this->creditmemoDocumentFactoryMock = $this->getMockBuilder(CreditmemoDocumentFactory::class)
130140
->disableOriginalConstructor()
131141
->getMock();
132-
133142
$this->creditmemoValidatorMock = $this->getMockBuilder(CreditmemoValidatorInterface::class)
134143
->disableOriginalConstructor()
135144
->getMock();
136-
137145
$this->orderValidatorMock = $this->getMockBuilder(OrderValidatorInterface::class)
138146
->disableOriginalConstructor()
139147
->getMock();
140-
141148
$this->paymentAdapterMock = $this->getMockBuilder(PaymentAdapterInterface::class)
142149
->disableOriginalConstructor()
143150
->getMock();
144-
145151
$this->orderStateResolverMock = $this->getMockBuilder(OrderStateResolverInterface::class)
146152
->disableOriginalConstructor()
147153
->getMockForAbstractClass();
148-
149154
$this->configMock = $this->getMockBuilder(OrderConfig::class)
150155
->disableOriginalConstructor()
151156
->getMock();
152-
153157
$this->creditmemoRepositoryMock = $this->getMockBuilder(CreditmemoRepositoryInterface::class)
154158
->disableOriginalConstructor()
155159
->getMockForAbstractClass();
156-
157160
$this->notifierMock = $this->getMockBuilder(NotifierInterface::class)
158161
->disableOriginalConstructor()
159162
->getMockForAbstractClass();
160-
161163
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
162164
->disableOriginalConstructor()
163165
->getMockForAbstractClass();
164-
165166
$this->creditmemoCommentCreationMock = $this->getMockBuilder(CreditmemoCommentCreationInterface::class)
166167
->disableOriginalConstructor()
167168
->getMockForAbstractClass();
168-
169169
$this->creditmemoCreationArgumentsMock = $this->getMockBuilder(CreditmemoCreationArgumentsInterface::class)
170170
->disableOriginalConstructor()
171171
->getMockForAbstractClass();
172-
173172
$this->orderMock = $this->getMockBuilder(OrderInterface::class)
174173
->disableOriginalConstructor()
175174
->getMockForAbstractClass();
176-
177175
$this->creditmemoMock = $this->getMockBuilder(CreditmemoInterface::class)
178176
->disableOriginalConstructor()
179177
->getMockForAbstractClass();
180-
181178
$this->adapterInterface = $this->getMockBuilder(AdapterInterface::class)
182179
->disableOriginalConstructor()
183180
->getMockForAbstractClass();
181+
$this->itemCreationValidatorMock = $this->getMockBuilder(Order\Creditmemo\ItemCreationValidatorInterface::class)
182+
->disableOriginalConstructor()
183+
->getMockForAbstractClass();
184+
$this->creditmemoItemCreationMock = $this->getMockBuilder(CreditmemoItemCreationInterface::class)
185+
->disableOriginalConstructor()
186+
->getMockForAbstractClass();
184187

185188
$this->refundOrder = new RefundOrder(
186189
$this->resourceConnectionMock,
187190
$this->orderStateResolverMock,
188191
$this->orderRepositoryMock,
189192
$this->orderValidatorMock,
190193
$this->creditmemoValidatorMock,
194+
$this->itemCreationValidatorMock,
191195
$this->creditmemoRepositoryMock,
192196
$this->paymentAdapterMock,
193197
$this->creditmemoDocumentFactoryMock,
@@ -200,17 +204,16 @@ protected function setUp()
200204
/**
201205
* @dataProvider dataProvider
202206
*/
203-
public function testOrderCreditmemo($orderId, $items, $notify, $appendComment)
207+
public function testOrderCreditmemo($orderId, $notify, $appendComment)
204208
{
209+
$items = [$this->creditmemoItemCreationMock];
205210
$this->resourceConnectionMock->expects($this->once())
206211
->method('getConnection')
207212
->with('sales')
208213
->willReturn($this->adapterInterface);
209-
210214
$this->orderRepositoryMock->expects($this->once())
211215
->method('get')
212216
->willReturn($this->orderMock);
213-
214217
$this->creditmemoDocumentFactoryMock->expects($this->once())
215218
->method('createFromOrder')
216219
->with(
@@ -220,7 +223,6 @@ public function testOrderCreditmemo($orderId, $items, $notify, $appendComment)
220223
($appendComment && $notify),
221224
$this->creditmemoCreationArgumentsMock
222225
)->willReturn($this->creditmemoMock);
223-
224226
$this->creditmemoValidatorMock->expects($this->once())
225227
->method('validate')
226228
->with($this->creditmemoMock)
@@ -229,12 +231,17 @@ public function testOrderCreditmemo($orderId, $items, $notify, $appendComment)
229231
->method('validate')
230232
->with($this->orderMock)
231233
->willReturn([]);
232-
234+
$this->itemCreationValidatorMock->expects($this->once())
235+
->method('validate')
236+
->with(
237+
reset($items),
238+
[CreationQuantityValidator::class],
239+
$this->orderMock
240+
)->willReturn([]);
233241
$this->paymentAdapterMock->expects($this->once())
234242
->method('refund')
235243
->with($this->creditmemoMock, $this->orderMock)
236244
->willReturn($this->orderMock);
237-
238245
$this->orderStateResolverMock->expects($this->once())
239246
->method('getStateForOrder')
240247
->with($this->orderMock, [])
@@ -303,7 +310,7 @@ public function testOrderCreditmemo($orderId, $items, $notify, $appendComment)
303310
public function testDocumentValidationException()
304311
{
305312
$orderId = 1;
306-
$items = [1 => 2];
313+
$items = [$this->creditmemoItemCreationMock];
307314
$notify = true;
308315
$appendComment = true;
309316
$errorMessages = ['error1', 'error2'];
@@ -330,6 +337,10 @@ public function testDocumentValidationException()
330337
->method('validate')
331338
->with($this->orderMock)
332339
->willReturn([]);
340+
$this->itemCreationValidatorMock->expects($this->once())
341+
->method('validate')
342+
->with(reset($items), [CreationQuantityValidator::class], $this->orderMock)
343+
->willReturn([]);
333344

334345
$this->assertEquals(
335346
$errorMessages,
@@ -350,18 +361,16 @@ public function testDocumentValidationException()
350361
public function testCouldNotCreditmemoException()
351362
{
352363
$orderId = 1;
353-
$items = [1 => 2];
364+
$items = [$this->creditmemoItemCreationMock];
354365
$notify = true;
355366
$appendComment = true;
356367
$this->resourceConnectionMock->expects($this->once())
357368
->method('getConnection')
358369
->with('sales')
359370
->willReturn($this->adapterInterface);
360-
361371
$this->orderRepositoryMock->expects($this->once())
362372
->method('get')
363373
->willReturn($this->orderMock);
364-
365374
$this->creditmemoDocumentFactoryMock->expects($this->once())
366375
->method('createFromOrder')
367376
->with(
@@ -371,7 +380,10 @@ public function testCouldNotCreditmemoException()
371380
($appendComment && $notify),
372381
$this->creditmemoCreationArgumentsMock
373382
)->willReturn($this->creditmemoMock);
374-
383+
$this->itemCreationValidatorMock->expects($this->once())
384+
->method('validate')
385+
->with(reset($items), [CreationQuantityValidator::class], $this->orderMock)
386+
->willReturn([]);
375387
$this->creditmemoValidatorMock->expects($this->once())
376388
->method('validate')
377389
->with($this->creditmemoMock)
@@ -381,16 +393,13 @@ public function testCouldNotCreditmemoException()
381393
->with($this->orderMock)
382394
->willReturn([]);
383395
$e = new \Exception();
384-
385396
$this->paymentAdapterMock->expects($this->once())
386397
->method('refund')
387398
->with($this->creditmemoMock, $this->orderMock)
388399
->willThrowException($e);
389-
390400
$this->loggerMock->expects($this->once())
391401
->method('critical')
392402
->with($e);
393-
394403
$this->adapterInterface->expects($this->once())
395404
->method('rollBack');
396405

@@ -407,8 +416,8 @@ public function testCouldNotCreditmemoException()
407416
public function dataProvider()
408417
{
409418
return [
410-
'TestWithNotifyTrue' => [1, [1 => 2], true, true],
411-
'TestWithNotifyFalse' => [1, [1 => 2], false, true],
419+
'TestWithNotifyTrue' => [1, true, true],
420+
'TestWithNotifyFalse' => [1, false, true],
412421
];
413422
}
414423
}

0 commit comments

Comments
 (0)