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

Unify custom exceptions #270

Merged
merged 1 commit into from
Mar 17, 2021
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
11 changes: 9 additions & 2 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
### UPGRADE FROM 1.0.0-RC.7 TO 1.0.0-RC.8

1. The `fully_refunded` state and the `refund` transition have been removed from `sylius_order` state machine.
2. `Sylius\RefundPlugin\Provider\LabelBasedTaxRateProvider` has been changed to `Sylius\RefundPlugin\Provider\TaxRateProvider`.
3. The method `Sylius\RefundPlugin\Provider\TaxRateProviderInterface` has been changed

1. `Sylius\RefundPlugin\Provider\LabelBasedTaxRateProvider` has been changed to `Sylius\RefundPlugin\Provider\TaxRateProvider`.

1. The method `Sylius\RefundPlugin\Provider\TaxRateProviderInterface` has been changed
from `provide(OrderItemUnitInterface $orderItemUnit): ?string` to `provide(AdjustableInterface $adjustable): ?string`.

1. The suffix `Exception` has been removed from classes:
* `Sylius\RefundPlugin\Exception\InvalidRefundAmountException`
* `Sylius\RefundPlugin\Exception\OrderNotAvailableForRefundingException`
* `Sylius\RefundPlugin\Exception\UnitAlreadyRefundedException`

### UPGRADE FROM 1.0.0-RC.5 TO 1.0.0-RC.6

1. The `Version20201208105207.php` migration was added which extends existing adjustments with additional details (context). Depending on the type of adjustment, additionally defined information are:
Expand Down
6 changes: 3 additions & 3 deletions spec/CommandHandler/RefundUnitsHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\RefundPlugin\Command\RefundUnits;
use Sylius\RefundPlugin\Event\UnitsRefunded;
use Sylius\RefundPlugin\Exception\OrderNotAvailableForRefundingException;
use Sylius\RefundPlugin\Exception\OrderNotAvailableForRefunding;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Model\ShipmentRefund;
use Sylius\RefundPlugin\Refunder\RefunderInterface;
Expand Down Expand Up @@ -73,11 +73,11 @@ function it_throws_an_exception_if_order_is_not_available_for_refund(

$refundUnitsCommandValidator
->validate($refundUnitsCommand)
->willThrow(OrderNotAvailableForRefundingException::withOrderNumber('000222'))
->willThrow(OrderNotAvailableForRefunding::withOrderNumber('000222'))
;

$this
->shouldThrow(OrderNotAvailableForRefundingException::withOrderNumber('000222'))
->shouldThrow(OrderNotAvailableForRefunding::withOrderNumber('000222'))
->during('__invoke', [$refundUnitsCommand])
;
}
Expand Down
4 changes: 2 additions & 2 deletions spec/Creator/RefundCreatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PhpSpec\ObjectBehavior;
use Sylius\RefundPlugin\Creator\RefundCreatorInterface;
use Sylius\RefundPlugin\Entity\RefundInterface;
use Sylius\RefundPlugin\Exception\UnitAlreadyRefundedException;
use Sylius\RefundPlugin\Exception\UnitAlreadyRefunded;
use Sylius\RefundPlugin\Factory\RefundFactoryInterface;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Provider\RemainingTotalProviderInterface;
Expand Down Expand Up @@ -58,7 +58,7 @@ function it_throws_exception_if_unit_has_already_been_refunded(
$remainingTotalProvider->getTotalLeftToRefund(1, $refundType)->willReturn(0);

$this
->shouldThrow(UnitAlreadyRefundedException::class)
->shouldThrow(UnitAlreadyRefunded::class)
->during('__invoke', ['000222', 1, 1000, $refundType])
;
}
Expand Down
6 changes: 3 additions & 3 deletions spec/Validator/RefundAmountValidatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace spec\Sylius\RefundPlugin\Validator;

use PhpSpec\ObjectBehavior;
use Sylius\RefundPlugin\Exception\InvalidRefundAmountException;
use Sylius\RefundPlugin\Exception\InvalidRefundAmount;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Provider\RemainingTotalProviderInterface;
Expand All @@ -32,7 +32,7 @@ function it_throws_exception_if_unit_refund_total_is_bigger_than_remaining_unit_
$remainingTotalProvider->getTotalLeftToRefund(2, $refundType)->willReturn(5);

$this
->shouldThrow(InvalidRefundAmountException::class)
->shouldThrow(InvalidRefundAmount::class)
->during('validateUnits', [[$correctOrderItemUnitRefund], $refundType])
;
}
Expand All @@ -43,7 +43,7 @@ function it_throws_exception_if_total_of_at_least_one_unit_is_below_zero(): void
$correctOrderItemUnitRefund = new OrderItemUnitRefund(2, 10);

$this
->shouldThrow(InvalidRefundAmountException::class)
->shouldThrow(InvalidRefundAmount::class)
->during(
'validateUnits',
[
Expand Down
14 changes: 7 additions & 7 deletions spec/Validator/RefundUnitsCommandValidatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use PhpSpec\ObjectBehavior;
use Sylius\RefundPlugin\Checker\OrderRefundingAvailabilityCheckerInterface;
use Sylius\RefundPlugin\Command\RefundUnits;
use Sylius\RefundPlugin\Exception\InvalidRefundAmountException;
use Sylius\RefundPlugin\Exception\OrderNotAvailableForRefundingException;
use Sylius\RefundPlugin\Exception\InvalidRefundAmount;
use Sylius\RefundPlugin\Exception\OrderNotAvailableForRefunding;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\ShipmentRefund;
Expand All @@ -31,7 +31,7 @@ function it_throws_exception_when_order_is_not_available_for_refund(
$refundUnits = new RefundUnits('000001', [], [], 1, '');

$this
->shouldThrow(OrderNotAvailableForRefundingException::class)
->shouldThrow(OrderNotAvailableForRefunding::class)
->during('validate', [$refundUnits])
;
}
Expand All @@ -48,10 +48,10 @@ function it_throws_exception_when_order_item_units_are_not_valid(

$refundAmountValidator
->validateUnits([$orderItemUnitRefund], RefundType::orderItemUnit())
->willThrow(InvalidRefundAmountException::class)
->willThrow(InvalidRefundAmount::class)
;

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

function it_throws_exception_when_shipment_is_not_valid(
Expand All @@ -68,9 +68,9 @@ function it_throws_exception_when_shipment_is_not_valid(

$refundAmountValidator
->validateUnits([$shipmentRefund], RefundType::shipment())
->willThrow(InvalidRefundAmountException::class)
->willThrow(InvalidRefundAmount::class)
;

$this->shouldThrow(InvalidRefundAmountException::class)->during('validate', [$refundUnits]);
$this->shouldThrow(InvalidRefundAmount::class)->during('validate', [$refundUnits]);
}
}
6 changes: 3 additions & 3 deletions src/Action/Admin/RefundUnitsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Psr\Log\LoggerInterface;
use Sylius\RefundPlugin\Creator\RefundUnitsCommandCreatorInterface;
use Sylius\RefundPlugin\Exception\InvalidRefundAmountException;
use Sylius\RefundPlugin\Exception\InvalidRefundAmount;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -52,7 +52,7 @@ public function __invoke(Request $request): Response
$this->commandBus->dispatch($this->commandCreator->fromRequest($request));

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

$this->logger->error($exception->getMessage());
Expand All @@ -72,7 +72,7 @@ public function __invoke(Request $request): Response

private function provideErrorMessage(\Throwable $previousException): void
{
if ($previousException instanceof InvalidRefundAmountException) {
if ($previousException instanceof InvalidRefundAmount) {
$this->session->getFlashBag()->add('error', $previousException->getMessage());

return;
Expand Down
4 changes: 2 additions & 2 deletions src/Creator/RefundCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Sylius\RefundPlugin\Creator;

use Doctrine\Persistence\ObjectManager;
use Sylius\RefundPlugin\Exception\UnitAlreadyRefundedException;
use Sylius\RefundPlugin\Exception\UnitAlreadyRefunded;
use Sylius\RefundPlugin\Factory\RefundFactoryInterface;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Provider\RemainingTotalProviderInterface;
Expand Down Expand Up @@ -36,7 +36,7 @@ public function __invoke(string $orderNumber, int $unitId, int $amount, RefundTy
$remainingTotal = $this->remainingTotalProvider->getTotalLeftToRefund($unitId, $refundType);

if ($remainingTotal === 0) {
throw UnitAlreadyRefundedException::withIdAndOrderNumber($unitId, $orderNumber);
throw UnitAlreadyRefunded::withIdAndOrderNumber($unitId, $orderNumber);
}

$refund = $this->refundFactory->createWithData($orderNumber, $unitId, $amount, $refundType);
Expand Down
4 changes: 2 additions & 2 deletions src/Creator/RefundUnitsCommandCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Sylius\RefundPlugin\Calculator\UnitRefundTotalCalculatorInterface;
use Sylius\RefundPlugin\Command\RefundUnits;
use Sylius\RefundPlugin\Exception\InvalidRefundAmountException;
use Sylius\RefundPlugin\Exception\InvalidRefundAmount;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\ShipmentRefund;
Expand All @@ -32,7 +32,7 @@ public function fromRequest(Request $request): RefundUnits
$shipments = $this->filterEmptyRefundUnits($request->request->get('sylius_refund_shipments', []));

if (count($units) === 0 && count($shipments) === 0) {
throw InvalidRefundAmountException::withValidationConstraint('sylius_refund.at_least_one_unit_should_be_selected_to_refund');
throw InvalidRefundAmount::withValidationConstraint('sylius_refund.at_least_one_unit_should_be_selected_to_refund');
}

return new RefundUnits(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Sylius\RefundPlugin\Exception;

final class InvalidRefundAmountException extends \InvalidArgumentException
final class InvalidRefundAmount extends \InvalidArgumentException
{
public static function withValidationConstraint(string $validationConstraint): self
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Sylius\RefundPlugin\Exception;

final class OrderNotAvailableForRefundingException extends \InvalidArgumentException
final class OrderNotAvailableForRefunding extends \InvalidArgumentException
{
public static function withOrderNumber(string $orderNumber): self
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Sylius\RefundPlugin\Exception;

final class UnitAlreadyRefundedException extends \InvalidArgumentException
final class UnitAlreadyRefunded extends \InvalidArgumentException
{
public static function withIdAndOrderNumber(int $unitId, string $orderNumber): self
{
Expand Down
6 changes: 3 additions & 3 deletions src/Validator/RefundAmountValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Sylius\RefundPlugin\Validator;

use Sylius\RefundPlugin\Exception\InvalidRefundAmountException;
use Sylius\RefundPlugin\Exception\InvalidRefundAmount;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Model\UnitRefundInterface;
use Sylius\RefundPlugin\Provider\RemainingTotalProviderInterface;
Expand All @@ -27,15 +27,15 @@ public function validateUnits(array $unitRefunds, RefundType $refundType): void
/** @var UnitRefundInterface $unitRefund */
foreach ($unitRefunds as $unitRefund) {
if ($unitRefund->total() <= 0) {
throw InvalidRefundAmountException::withValidationConstraint(
throw InvalidRefundAmount::withValidationConstraint(
RefundUnitsValidationConstraintMessages::REFUND_AMOUNT_MUST_BE_GREATER_THAN_ZERO
);
}

$unitRefundedTotal = $this->remainingTotalProvider->getTotalLeftToRefund($unitRefund->id(), $refundType);

if ($unitRefund->total() > $unitRefundedTotal) {
throw InvalidRefundAmountException::withValidationConstraint(
throw InvalidRefundAmount::withValidationConstraint(
RefundUnitsValidationConstraintMessages::REFUND_AMOUNT_MUST_BE_LESS_THAN_AVAILABLE
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Validator/RefundUnitsCommandValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Sylius\RefundPlugin\Checker\OrderRefundingAvailabilityCheckerInterface;
use Sylius\RefundPlugin\Command\RefundUnits;
use Sylius\RefundPlugin\Exception\OrderNotAvailableForRefundingException;
use Sylius\RefundPlugin\Exception\OrderNotAvailableForRefunding;
use Sylius\RefundPlugin\Model\RefundType;

final class RefundUnitsCommandValidator implements RefundUnitsCommandValidatorInterface
Expand All @@ -28,7 +28,7 @@ public function __construct(
public function validate(RefundUnits $command): void
{
if (!$this->orderRefundingAvailabilityChecker->__invoke($command->orderNumber())) {
throw OrderNotAvailableForRefundingException::withOrderNumber($command->orderNumber());
throw OrderNotAvailableForRefunding::withOrderNumber($command->orderNumber());
}

$this->refundAmountValidator->validateUnits($command->units(), RefundType::orderItemUnit());
Expand Down