Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate if refunded units belong to an order #407

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions UPGRADE-1.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,15 @@ deprecated code:
- public function validateUnits(array $unitRefunds, RefundTypeInterface $refundType): void;
+ public function validateUnits(array $unitRefunds): void;
```

16. The constructor of the `Sylius\RefundPlugin\Validator\RefundUnitsCommandValidator` has been changed:

```diff
public function __construct(
private OrderRefundingAvailabilityCheckerInterface $orderRefundingAvailabilityChecker,
private RefundAmountValidatorInterface $refundAmountValidator,
+ private iterator $refundUnitsBelongingToOrderValidators,
) {
// ...
}
```
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ parameters:
- '/Method Sylius\\RefundPlugin\\Entity\\CustomerBillingDataInterface::id\(\) has no return type specified./'
- '/Method Sylius\\RefundPlugin\\Entity\\CustomerBillingDataInterface::setId\(\) has parameter \$id with no type specified./'
- '/Method Sylius\\RefundPlugin\\[a-zA-Z\\]+::getFlashBag\(\) should return Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface but returns Symfony\\Component\\HttpFoundation\\Session\\SessionBagInterface\./'
- '/Call to an undefined method [a-zA-Z\\]+::createQueryBuilder\(\)./'
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\RefundPlugin\Validator;

use PhpSpec\ObjectBehavior;
use Sylius\RefundPlugin\Doctrine\ORM\CountRefundsBelongingToOrderQueryInterface;
use Sylius\RefundPlugin\Exception\RefundUnitsNotBelongToOrder;
use Sylius\RefundPlugin\Filter\UnitRefundFilterInterface;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Model\ShipmentRefund;
use Sylius\RefundPlugin\Validator\UnitRefundsBelongingToOrderValidatorInterface;

final class OrderItemUnitRefundsBelongingToOrderValidatorSpec extends ObjectBehavior
{
function let(
UnitRefundFilterInterface $unitRefundFilter,
CountRefundsBelongingToOrderQueryInterface $countRefundsBelongingToOrderQuery,
): void {
$this->beConstructedWith($unitRefundFilter, $countRefundsBelongingToOrderQuery);
}

function it_implements_unit_refunds_belonging_to_order_validator_interface(): void
{
$this->shouldHaveType(UnitRefundsBelongingToOrderValidatorInterface::class);
}

function it_throws_an_exception_if_some_order_item_unit_refunds_do_not_belong_to_the_order(
UnitRefundFilterInterface $unitRefundFilter,
CountRefundsBelongingToOrderQueryInterface $countRefundsBelongingToOrderQuery,
): void {
$unitRefunds = [
$firstOrderItemUnitRefund = new OrderItemUnitRefund(1, 3000),
new ShipmentRefund(2, 5000),
new ShipmentRefund(3, 8000),
$secondOrderItemUnitRefund = new OrderItemUnitRefund(4, 13000),
];

$unitRefundFilter
->filterUnitRefunds($unitRefunds, OrderItemUnitRefund::class)
->willReturn([
$firstOrderItemUnitRefund,
$secondOrderItemUnitRefund,
])
;

$countRefundsBelongingToOrderQuery->count([1, 4], '000001')->willReturn(1);

$this
->shouldThrow(RefundUnitsNotBelongToOrder::class)
->during('validateUnits', [$unitRefunds, '000001'])
;
}

function it_does_not_throw_an_exception_if_all_order_item_unit_refunds_belong_to_the_order(
UnitRefundFilterInterface $unitRefundFilter,
CountRefundsBelongingToOrderQueryInterface $countRefundsBelongingToOrderQuery,
): void {
$unitRefunds = [
$firstOrderItemUnitRefund = new OrderItemUnitRefund(1, 3000),
new ShipmentRefund(2, 5000),
new ShipmentRefund(3, 8000),
$secondOrderItemUnitRefund = new OrderItemUnitRefund(4, 13000),
];

$unitRefundFilter
->filterUnitRefunds($unitRefunds, OrderItemUnitRefund::class)
->willReturn([
$firstOrderItemUnitRefund,
$secondOrderItemUnitRefund,
])
;

$countRefundsBelongingToOrderQuery->count([1, 4], '000001')->willReturn(2);

$this
->shouldNotThrow()
->during('validateUnits', [$unitRefunds, '000001'])
;
}
}
101 changes: 98 additions & 3 deletions spec/Validator/RefundUnitsCommandValidatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,28 @@
use Sylius\RefundPlugin\Command\RefundUnits;
use Sylius\RefundPlugin\Exception\InvalidRefundAmount;
use Sylius\RefundPlugin\Exception\OrderNotAvailableForRefunding;
use Sylius\RefundPlugin\Exception\RefundUnitsNotBelongToOrder;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Model\ShipmentRefund;
use Sylius\RefundPlugin\Validator\RefundAmountValidatorInterface;
use Sylius\RefundPlugin\Validator\UnitRefundsBelongingToOrderValidatorInterface;

final class RefundUnitsCommandValidatorSpec extends ObjectBehavior
{
function let(
OrderRefundingAvailabilityCheckerInterface $orderRefundingAvailabilityChecker,
RefundAmountValidatorInterface $refundAmountValidator,
UnitRefundsBelongingToOrderValidatorInterface $firstUnitRefundsBelongingToOrderValidator,
UnitRefundsBelongingToOrderValidatorInterface $secondUnitRefundsBelongingToOrderValidator,
): void {
$this->beConstructedWith($orderRefundingAvailabilityChecker, $refundAmountValidator);
$this->beConstructedWith(
$orderRefundingAvailabilityChecker,
$refundAmountValidator,
[
$firstUnitRefundsBelongingToOrderValidator,
$secondUnitRefundsBelongingToOrderValidator,
],
);
}

function it_throws_exception_when_order_is_not_available_for_refund(
Expand All @@ -44,9 +55,11 @@ function it_throws_exception_when_order_is_not_available_for_refund(
;
}

function it_throws_exception_when_order_item_units_are_not_valid(
function it_throws_exception_when_order_item_units_amount_is_not_valid(
OrderRefundingAvailabilityCheckerInterface $orderRefundingAvailabilityChecker,
RefundAmountValidatorInterface $refundAmountValidator,
UnitRefundsBelongingToOrderValidatorInterface $firstUnitRefundsBelongingToOrderValidator,
UnitRefundsBelongingToOrderValidatorInterface $secondUnitRefundsBelongingToOrderValidator,
): void {
$orderRefundingAvailabilityChecker->__invoke('000001')->willReturn(true);

Expand All @@ -59,12 +72,54 @@ function it_throws_exception_when_order_item_units_are_not_valid(
->willThrow(InvalidRefundAmount::class)
;

$firstUnitRefundsBelongingToOrderValidator
->validateUnits([$orderItemUnitRefund], '000001')
->shouldBeCalled()
;

$secondUnitRefundsBelongingToOrderValidator
->validateUnits([$orderItemUnitRefund], '000001')
->shouldBeCalled()
;

$this->shouldThrow(InvalidRefundAmount::class)->during('validate', [$refundUnits]);
}

function it_throws_exception_when_shipment_is_not_valid(
function it_throws_exception_when_order_item_units_do_not_belong_to_an_order(
OrderRefundingAvailabilityCheckerInterface $orderRefundingAvailabilityChecker,
RefundAmountValidatorInterface $refundAmountValidator,
UnitRefundsBelongingToOrderValidatorInterface $firstUnitRefundsBelongingToOrderValidator,
UnitRefundsBelongingToOrderValidatorInterface $secondUnitRefundsBelongingToOrderValidator,
): void {
$orderRefundingAvailabilityChecker->__invoke('000001')->willReturn(true);

$orderItemUnitRefund = new OrderItemUnitRefund(1, 10);

$refundUnits = new RefundUnits('000001', [$orderItemUnitRefund], 1, '');

$refundAmountValidator
->validateUnits([$orderItemUnitRefund])
->shouldNotBeCalled()
;

$firstUnitRefundsBelongingToOrderValidator
->validateUnits([$orderItemUnitRefund], '000001')
->willThrow(RefundUnitsNotBelongToOrder::class)
;

$secondUnitRefundsBelongingToOrderValidator
->validateUnits([$refundUnits], '000001')
->shouldNotBeCalled()
;

$this->shouldThrow(RefundUnitsNotBelongToOrder::class)->during('validate', [$refundUnits]);
}

function it_throws_exception_when_shipment_amount_is_not_valid(
OrderRefundingAvailabilityCheckerInterface $orderRefundingAvailabilityChecker,
RefundAmountValidatorInterface $refundAmountValidator,
UnitRefundsBelongingToOrderValidatorInterface $firstUnitRefundsBelongingToOrderValidator,
UnitRefundsBelongingToOrderValidatorInterface $secondUnitRefundsBelongingToOrderValidator,
): void {
$orderRefundingAvailabilityChecker->__invoke('000001')->willReturn(true);

Expand All @@ -77,6 +132,46 @@ function it_throws_exception_when_shipment_is_not_valid(
->willThrow(InvalidRefundAmount::class)
;

$firstUnitRefundsBelongingToOrderValidator
->validateUnits([$shipmentRefund], '000001')
->shouldBeCalled()
;

$secondUnitRefundsBelongingToOrderValidator
->validateUnits([$shipmentRefund], '000001')
->shouldBeCalled()
;

$this->shouldThrow(InvalidRefundAmount::class)->during('validate', [$refundUnits]);
}

function it_throws_exception_when_shipment_does_not_belong_to_an_order(
OrderRefundingAvailabilityCheckerInterface $orderRefundingAvailabilityChecker,
RefundAmountValidatorInterface $refundAmountValidator,
UnitRefundsBelongingToOrderValidatorInterface $firstUnitRefundsBelongingToOrderValidator,
UnitRefundsBelongingToOrderValidatorInterface $secondUnitRefundsBelongingToOrderValidator,
): void {
$orderRefundingAvailabilityChecker->__invoke('000001')->willReturn(true);

$shipmentRefund = new ShipmentRefund(1, 10);

$refundUnits = new RefundUnits('000001', [$shipmentRefund], 1, '');

$refundAmountValidator
->validateUnits([$shipmentRefund])
->shouldNotBeCalled()
;

$firstUnitRefundsBelongingToOrderValidator
->validateUnits([$shipmentRefund], '000001')
->shouldBeCalled()
;

$secondUnitRefundsBelongingToOrderValidator
->validateUnits([$shipmentRefund], '000001')
->willThrow(RefundUnitsNotBelongToOrder::class)
;

$this->shouldThrow(RefundUnitsNotBelongToOrder::class)->during('validate', [$refundUnits]);
}
}
91 changes: 91 additions & 0 deletions spec/Validator/ShipmentRefundsBelongingToOrderValidatorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\RefundPlugin\Validator;

use PhpSpec\ObjectBehavior;
use Sylius\RefundPlugin\Doctrine\ORM\CountRefundsBelongingToOrderQueryInterface;
use Sylius\RefundPlugin\Exception\RefundUnitsNotBelongToOrder;
use Sylius\RefundPlugin\Filter\UnitRefundFilterInterface;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Model\ShipmentRefund;
use Sylius\RefundPlugin\Validator\UnitRefundsBelongingToOrderValidatorInterface;

final class OrderItemUnitRefundsBelongingToOrderValidatorSpec extends ObjectBehavior
{
function let(
UnitRefundFilterInterface $unitRefundFilter,
CountRefundsBelongingToOrderQueryInterface $countRefundsBelongingToOrderQuery,
): void {
$this->beConstructedWith($unitRefundFilter, $countRefundsBelongingToOrderQuery);
}

function it_implements_unit_refunds_belonging_to_order_validator_interface(): void
{
$this->shouldHaveType(UnitRefundsBelongingToOrderValidatorInterface::class);
}

function it_throws_an_exception_if_some_order_item_unit_refunds_do_not_belong_to_the_order(
UnitRefundFilterInterface $unitRefundFilter,
CountRefundsBelongingToOrderQueryInterface $countRefundsBelongingToOrderQuery,
): void {
$unitRefunds = [
new OrderItemUnitRefund(1, 3000),
$firstShipmentRefund = new ShipmentRefund(2, 5000),
$secondShipmentRefund = new ShipmentRefund(3, 8000),
new OrderItemUnitRefund(4, 13000),
];

$unitRefundFilter
->filterUnitRefunds($unitRefunds, ShipmentRefund::class)
->willReturn([
$firstShipmentRefund,
$secondShipmentRefund,
])
;

$countRefundsBelongingToOrderQuery->count([2, 3], '000001')->willReturn(1);

$this
->shouldThrow(RefundUnitsNotBelongToOrder::class)
->during('validateUnits', [$unitRefunds, '000001'])
;
}

function it_does_not_throw_an_exception_if_all_order_item_unit_refunds_belong_to_the_order(
UnitRefundFilterInterface $unitRefundFilter,
CountRefundsBelongingToOrderQueryInterface $countRefundsBelongingToOrderQuery,
): void {
$unitRefunds = [
new OrderItemUnitRefund(1, 3000),
$firstShipmentRefund = new ShipmentRefund(2, 5000),
$secondShipmentRefund = new ShipmentRefund(3, 8000),
new OrderItemUnitRefund(4, 13000),
];

$unitRefundFilter
->filterUnitRefunds($unitRefunds, ShipmentRefund::class)
->willReturn([
$firstShipmentRefund,
$secondShipmentRefund,
])
;

$countRefundsBelongingToOrderQuery->count([2, 3], '000001')->willReturn(2);

$this
->shouldNotThrow()
->during('validateUnits', [$unitRefunds, '000001'])
;
}
}
3 changes: 2 additions & 1 deletion src/Action/Admin/RefundUnitsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sylius\RefundPlugin\Creator\RefundUnitsCommandCreatorInterface;
use Sylius\RefundPlugin\Creator\RequestCommandCreatorInterface;
use Sylius\RefundPlugin\Exception\InvalidRefundAmount;
use Sylius\RefundPlugin\Exception\RefundUnitsNotBelongToOrder;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
Expand Down Expand Up @@ -62,7 +63,7 @@ public function __invoke(Request $request): Response
$this->commandBus->dispatch($this->commandCreator->fromRequest($request));

$this->getFlashBag()->add('success', 'sylius_refund.units_successfully_refunded');
} catch (InvalidRefundAmount $exception) {
} catch (InvalidRefundAmount|RefundUnitsNotBelongToOrder $exception) {
$this->getFlashBag()->add('error', $exception->getMessage());

$this->logger->error($exception->getMessage());
Expand Down
Loading