Skip to content

Commit 63ebd1c

Browse files
committed
Refactor validating if refunded units belong to an order
1 parent 9e2ae27 commit 63ebd1c

16 files changed

+416
-288
lines changed

UPGRADE-1.4.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ deprecated code:
188188
public function __construct(
189189
private OrderRefundingAvailabilityCheckerInterface $orderRefundingAvailabilityChecker,
190190
private RefundAmountValidatorInterface $refundAmountValidator,
191-
+ private RefundUnitsBelongToOrderValidatorInterface $refundUnitsBelongToOrderValidator,
191+
+ private iterator $refundUnitsBelongingToOrderValidators,
192192
) {
193193
// ...
194194
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace spec\Sylius\RefundPlugin\Validator;
15+
16+
use PhpSpec\ObjectBehavior;
17+
use Sylius\RefundPlugin\Doctrine\ORM\CountRefundsBelongingToOrderQueryInterface;
18+
use Sylius\RefundPlugin\Exception\RefundUnitsNotBelongToOrder;
19+
use Sylius\RefundPlugin\Filter\UnitRefundFilterInterface;
20+
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
21+
use Sylius\RefundPlugin\Model\ShipmentRefund;
22+
use Sylius\RefundPlugin\Validator\UnitRefundsBelongingToOrderValidatorInterface;
23+
24+
final class OrderItemUnitRefundsBelongingToOrderValidatorSpec extends ObjectBehavior
25+
{
26+
function let(
27+
UnitRefundFilterInterface $unitRefundFilter,
28+
CountRefundsBelongingToOrderQueryInterface $countRefundsBelongingToOrderQuery,
29+
): void {
30+
$this->beConstructedWith($unitRefundFilter, $countRefundsBelongingToOrderQuery);
31+
}
32+
33+
function it_implements_unit_refunds_belonging_to_order_validator_interface(): void
34+
{
35+
$this->shouldHaveType(UnitRefundsBelongingToOrderValidatorInterface::class);
36+
}
37+
38+
function it_throws_an_exception_if_some_order_item_unit_refunds_do_not_belong_to_the_order(
39+
UnitRefundFilterInterface $unitRefundFilter,
40+
CountRefundsBelongingToOrderQueryInterface $countRefundsBelongingToOrderQuery,
41+
): void {
42+
$unitRefunds = [
43+
$firstOrderItemUnitRefund = new OrderItemUnitRefund(1, 3000),
44+
new ShipmentRefund(2, 5000),
45+
new ShipmentRefund(3, 8000),
46+
$secondOrderItemUnitRefund = new OrderItemUnitRefund(4, 13000),
47+
];
48+
49+
$unitRefundFilter
50+
->filterUnitRefunds($unitRefunds, OrderItemUnitRefund::class)
51+
->willReturn([
52+
$firstOrderItemUnitRefund,
53+
$secondOrderItemUnitRefund,
54+
])
55+
;
56+
57+
$countRefundsBelongingToOrderQuery->count([1, 4], '000001')->willReturn(1);
58+
59+
$this
60+
->shouldThrow(RefundUnitsNotBelongToOrder::class)
61+
->during('validateUnits', [$unitRefunds, '000001'])
62+
;
63+
}
64+
65+
function it_does_not_throw_an_exception_if_all_order_item_unit_refunds_belong_to_the_order(
66+
UnitRefundFilterInterface $unitRefundFilter,
67+
CountRefundsBelongingToOrderQueryInterface $countRefundsBelongingToOrderQuery,
68+
): void {
69+
$unitRefunds = [
70+
$firstOrderItemUnitRefund = new OrderItemUnitRefund(1, 3000),
71+
new ShipmentRefund(2, 5000),
72+
new ShipmentRefund(3, 8000),
73+
$secondOrderItemUnitRefund = new OrderItemUnitRefund(4, 13000),
74+
];
75+
76+
$unitRefundFilter
77+
->filterUnitRefunds($unitRefunds, OrderItemUnitRefund::class)
78+
->willReturn([
79+
$firstOrderItemUnitRefund,
80+
$secondOrderItemUnitRefund,
81+
])
82+
;
83+
84+
$countRefundsBelongingToOrderQuery->count([1, 4], '000001')->willReturn(2);
85+
86+
$this
87+
->shouldNotThrow()
88+
->during('validateUnits', [$unitRefunds, '000001'])
89+
;
90+
}
91+
}

spec/Validator/RefundUnitsBelongToOrderValidatorSpec.php

-142
This file was deleted.

spec/Validator/RefundUnitsCommandValidatorSpec.php

+39-11
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@
2222
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
2323
use Sylius\RefundPlugin\Model\ShipmentRefund;
2424
use Sylius\RefundPlugin\Validator\RefundAmountValidatorInterface;
25-
use Sylius\RefundPlugin\Validator\RefundUnitsBelongToOrderValidatorInterface;
25+
use Sylius\RefundPlugin\Validator\UnitRefundsBelongingToOrderValidatorInterface;
2626

2727
final class RefundUnitsCommandValidatorSpec extends ObjectBehavior
2828
{
2929
function let(
3030
OrderRefundingAvailabilityCheckerInterface $orderRefundingAvailabilityChecker,
3131
RefundAmountValidatorInterface $refundAmountValidator,
32-
RefundUnitsBelongToOrderValidatorInterface $refundUnitsBelongToOrderValidator,
32+
UnitRefundsBelongingToOrderValidatorInterface $firstUnitRefundsBelongingToOrderValidator,
33+
UnitRefundsBelongingToOrderValidatorInterface $secondUnitRefundsBelongingToOrderValidator,
3334
): void {
3435
$this->beConstructedWith(
3536
$orderRefundingAvailabilityChecker,
3637
$refundAmountValidator,
37-
$refundUnitsBelongToOrderValidator,
38+
[
39+
$firstUnitRefundsBelongingToOrderValidator,
40+
$secondUnitRefundsBelongingToOrderValidator,
41+
],
3842
);
3943
}
4044

@@ -54,7 +58,8 @@ function it_throws_exception_when_order_is_not_available_for_refund(
5458
function it_throws_exception_when_order_item_units_amount_is_not_valid(
5559
OrderRefundingAvailabilityCheckerInterface $orderRefundingAvailabilityChecker,
5660
RefundAmountValidatorInterface $refundAmountValidator,
57-
RefundUnitsBelongToOrderValidatorInterface $refundUnitsBelongToOrderValidator,
61+
UnitRefundsBelongingToOrderValidatorInterface $firstUnitRefundsBelongingToOrderValidator,
62+
UnitRefundsBelongingToOrderValidatorInterface $secondUnitRefundsBelongingToOrderValidator,
5863
): void {
5964
$orderRefundingAvailabilityChecker->__invoke('000001')->willReturn(true);
6065

@@ -67,7 +72,12 @@ function it_throws_exception_when_order_item_units_amount_is_not_valid(
6772
->willThrow(InvalidRefundAmount::class)
6873
;
6974

70-
$refundUnitsBelongToOrderValidator
75+
$firstUnitRefundsBelongingToOrderValidator
76+
->validateUnits([$orderItemUnitRefund], '000001')
77+
->shouldBeCalled()
78+
;
79+
80+
$secondUnitRefundsBelongingToOrderValidator
7181
->validateUnits([$orderItemUnitRefund], '000001')
7282
->shouldBeCalled()
7383
;
@@ -78,7 +88,8 @@ function it_throws_exception_when_order_item_units_amount_is_not_valid(
7888
function it_throws_exception_when_order_item_units_do_not_belong_to_an_order(
7989
OrderRefundingAvailabilityCheckerInterface $orderRefundingAvailabilityChecker,
8090
RefundAmountValidatorInterface $refundAmountValidator,
81-
RefundUnitsBelongToOrderValidatorInterface $refundUnitsBelongToOrderValidator,
91+
UnitRefundsBelongingToOrderValidatorInterface $firstUnitRefundsBelongingToOrderValidator,
92+
UnitRefundsBelongingToOrderValidatorInterface $secondUnitRefundsBelongingToOrderValidator,
8293
): void {
8394
$orderRefundingAvailabilityChecker->__invoke('000001')->willReturn(true);
8495

@@ -91,18 +102,24 @@ function it_throws_exception_when_order_item_units_do_not_belong_to_an_order(
91102
->shouldNotBeCalled()
92103
;
93104

94-
$refundUnitsBelongToOrderValidator
105+
$firstUnitRefundsBelongingToOrderValidator
95106
->validateUnits([$orderItemUnitRefund], '000001')
96107
->willThrow(RefundUnitsNotBelongToOrder::class)
97108
;
98109

110+
$secondUnitRefundsBelongingToOrderValidator
111+
->validateUnits([$refundUnits], '000001')
112+
->shouldNotBeCalled()
113+
;
114+
99115
$this->shouldThrow(RefundUnitsNotBelongToOrder::class)->during('validate', [$refundUnits]);
100116
}
101117

102118
function it_throws_exception_when_shipment_amount_is_not_valid(
103119
OrderRefundingAvailabilityCheckerInterface $orderRefundingAvailabilityChecker,
104120
RefundAmountValidatorInterface $refundAmountValidator,
105-
RefundUnitsBelongToOrderValidatorInterface $refundUnitsBelongToOrderValidator,
121+
UnitRefundsBelongingToOrderValidatorInterface $firstUnitRefundsBelongingToOrderValidator,
122+
UnitRefundsBelongingToOrderValidatorInterface $secondUnitRefundsBelongingToOrderValidator,
106123
): void {
107124
$orderRefundingAvailabilityChecker->__invoke('000001')->willReturn(true);
108125

@@ -115,7 +132,12 @@ function it_throws_exception_when_shipment_amount_is_not_valid(
115132
->willThrow(InvalidRefundAmount::class)
116133
;
117134

118-
$refundUnitsBelongToOrderValidator
135+
$firstUnitRefundsBelongingToOrderValidator
136+
->validateUnits([$shipmentRefund], '000001')
137+
->shouldBeCalled()
138+
;
139+
140+
$secondUnitRefundsBelongingToOrderValidator
119141
->validateUnits([$shipmentRefund], '000001')
120142
->shouldBeCalled()
121143
;
@@ -126,7 +148,8 @@ function it_throws_exception_when_shipment_amount_is_not_valid(
126148
function it_throws_exception_when_shipment_does_not_belong_to_an_order(
127149
OrderRefundingAvailabilityCheckerInterface $orderRefundingAvailabilityChecker,
128150
RefundAmountValidatorInterface $refundAmountValidator,
129-
RefundUnitsBelongToOrderValidatorInterface $refundUnitsBelongToOrderValidator,
151+
UnitRefundsBelongingToOrderValidatorInterface $firstUnitRefundsBelongingToOrderValidator,
152+
UnitRefundsBelongingToOrderValidatorInterface $secondUnitRefundsBelongingToOrderValidator,
130153
): void {
131154
$orderRefundingAvailabilityChecker->__invoke('000001')->willReturn(true);
132155

@@ -139,7 +162,12 @@ function it_throws_exception_when_shipment_does_not_belong_to_an_order(
139162
->shouldNotBeCalled()
140163
;
141164

142-
$refundUnitsBelongToOrderValidator
165+
$firstUnitRefundsBelongingToOrderValidator
166+
->validateUnits([$shipmentRefund], '000001')
167+
->shouldBeCalled()
168+
;
169+
170+
$secondUnitRefundsBelongingToOrderValidator
143171
->validateUnits([$shipmentRefund], '000001')
144172
->willThrow(RefundUnitsNotBelongToOrder::class)
145173
;

0 commit comments

Comments
 (0)