Skip to content

Commit

Permalink
refactor #320 [RefundPayment] Make the Refund customization easier (G…
Browse files Browse the repository at this point in the history
…Sadee)

This PR was merged into the 1.0-dev branch.

Discussion
----------



Commits
-------

7cc7471 [RefundPayment] Refactor factory to make it easier to overwrite
6cda314 Fix FailedRefundPaymentFactory after refactoring
36f0c60 [RefundPayment] CS fixes
60001b0 Refactor RefundUnitsCommandCreator by extracting converter
c2ea3bc [Behat] Try to fix the problem with randomly failing scenario by using sleep
7c40e8c [RefundPayment] Add additional note to UPGRADE file about changing factory
51c4cb9 [RefundPayment] Minor fixes after PR review
  • Loading branch information
Zales0123 authored Jun 24, 2021
2 parents dcf22f3 + 51c4cb9 commit 07ca76d
Show file tree
Hide file tree
Showing 20 changed files with 338 additions and 149 deletions.
53 changes: 53 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,59 @@
1. `sylius_refund_plugin.block_event_listener.account.order_show` and `sylius_refund_plugin.block_event_listener.order_show.credit_memos`
listeners have been replaced by `sylius_ui` configuration

1. `Sylius\RefundPlugin\Factory\RefundPaymentFactoryInterface` and `Sylius\RefundPlugin\Factory\RefundPaymentFactory`
service definitions have been removed and replaced by default resource factory `sylius_refund.factory.refund_payment`

1. The constructor of `Sylius\RefundPlugin\Factory\RefundPaymentFactory` has been changed:

```diff
- public function __construct(PaymentMethodRepositoryInterface $paymentMethodRepository)
+ public function __construct(string $className)
{
- $this->paymentMethodRepository = $paymentMethodRepository;
+ $this->className = $className;
}
```

1. The `createWithData` method of `Sylius\RefundPlugin\Factory\RefundPaymentFactoryInterface` has been changed:

```diff
public function createWithData(
OrderInterface $order,
int $amount,
string $currencyCode,
string $state,
- int $paymentMethodId
+ PaymentMethodInterface $paymentMethod
): RefundPaymentInterface;
```

1. The definition of `Sylius\RefundPlugin\ProcessManager\RefundPaymentProcessManager` has been changed:

```diff
<service id="Sylius\RefundPlugin\ProcessManager\RefundPaymentProcessManager">
<argument type="service" id="Sylius\RefundPlugin\StateResolver\OrderFullyRefundedStateResolverInterface" />
<argument type="service" id="Sylius\RefundPlugin\Provider\RelatedPaymentIdProviderInterface" />
- <argument type="service" id="Sylius\RefundPlugin\Factory\RefundPaymentFactoryInterface" />
+ <argument type="service" id="sylius_refund.factory.refund_payment" />
<argument type="service" id="sylius.repository.order" />
+ <argument type="service" id="sylius.repository.payment_method" />
<argument type="service" id="doctrine.orm.default_entity_manager" />
<argument type="service" id="sylius.event_bus" />
<tag name="sylius_refund.units_refunded.process_step" priority="50" />
</service>

1. The constructor of `Sylius\RefundPlugin\Creator\RefundUnitsCommandCreator` has been changed:

```diff
- public function __construct(UnitRefundTotalCalculatorInterface $unitRefundTotalCalculator)
+ public function __construct(RefundUnitsConverterInterface $refundUnitsConverter)
{
- $this->unitRefundTotalCalculator = $unitRefundTotalCalculator;
+ $this->refundUnitsConverter = $refundUnitsConverter;
}
```

### UPGRADE FROM 1.0.0-RC.9 TO 1.0.0-RC.10

1. Support for Sylius 1.8 has been dropped, upgrade your application to [Sylius 1.9](https://github.com/Sylius/Sylius/blob/master/UPGRADE-1.9.md)
Expand Down
4 changes: 2 additions & 2 deletions features/seeing_credit_memos_on_admin_order_view.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ Feature: Seeing credit memos in admin order view
And the customer chose "Galaxy Post" shipping method with "Space money" payment
And the order "#00000022" is already paid
And I am logged in as an administrator

@ui
Scenario: Having all credit memos listed on the order page in ascending order
Given the "#00000022" order's shipping cost already has a refund of "$4.50" with "Space money" payment
And the 1st "PHP T-Shirt" product from order "#00000022" has a refund of "$5.50" with "Space money" payment
And the 1st "PHP T-Shirt" product from order "#00000022" has a refund of "$5.50" with "Space money" payment done later
When I view the summary of the order "#00000022"
Then I should see the credit memo with "$4.50" total as 1st in the list
And I should see the credit memo with "$5.50" total as 2nd in the list
71 changes: 71 additions & 0 deletions spec/Converter/RefundUnitsConverterSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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\Converter;

use PhpSpec\ObjectBehavior;
use Sylius\RefundPlugin\Calculator\UnitRefundTotalCalculatorInterface;
use Sylius\RefundPlugin\Converter\RefundUnitsConverterInterface;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Model\RefundType;

final class RefundUnitsConverterSpec extends ObjectBehavior
{
public function let(UnitRefundTotalCalculatorInterface $unitRefundTotalCalculator): void
{
$this->beConstructedWith($unitRefundTotalCalculator);
}

public function it_implements_refund_units_converter_interface(): void
{
$this->shouldImplement(RefundUnitsConverterInterface::class);
}

public function it_converts_refund_units_from_request_with_full_prices_to_models(
UnitRefundTotalCalculatorInterface $unitRefundTotalCalculator
): void {
$unitRefundTotalCalculator->calculateForUnitWithIdAndType(1, RefundType::orderItemUnit(), null)->willReturn(1000);
$unitRefundTotalCalculator->calculateForUnitWithIdAndType(2, RefundType::orderItemUnit(), null)->willReturn(3000);

$this
->convert(
[
1 => ['full' => 'on'],
2 => ['full' => 'on'],
],
RefundType::orderItemUnit(),
OrderItemUnitRefund::class
)
->shouldBeLike([new OrderItemUnitRefund(1, 1000), new OrderItemUnitRefund(2, 3000)])
;
}

public function it_converts_refund_units_from_request_with_partial_prices_to_models(
UnitRefundTotalCalculatorInterface $unitRefundTotalCalculator
): void {
$unitRefundTotalCalculator->calculateForUnitWithIdAndType(1, RefundType::orderItemUnit(), 10.00)->willReturn(1000);
$unitRefundTotalCalculator->calculateForUnitWithIdAndType(2, RefundType::orderItemUnit(), null)->willReturn(3000);

$this
->convert(
[
1 => ['amount' => '10.00'],
2 => ['full' => 'on'],
],
RefundType::orderItemUnit(),
OrderItemUnitRefund::class
)
->shouldBeLike([new OrderItemUnitRefund(1, 1000), new OrderItemUnitRefund(2, 3000)])
;
}
}
78 changes: 36 additions & 42 deletions spec/Creator/RefundUnitsCommandCreatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
namespace spec\Sylius\RefundPlugin\Creator;

use PhpSpec\ObjectBehavior;
use Sylius\RefundPlugin\Calculator\UnitRefundTotalCalculatorInterface;
use Sylius\RefundPlugin\Command\RefundUnits;
use Sylius\RefundPlugin\Converter\RefundUnitsConverterInterface;
use Sylius\RefundPlugin\Creator\RefundUnitsCommandCreatorInterface;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Model\RefundType;
Expand All @@ -25,21 +25,24 @@

final class RefundUnitsCommandCreatorSpec extends ObjectBehavior
{
public function let(
UnitRefundTotalCalculatorInterface $unitRefundTotalCalculator
): void {
$this->beConstructedWith($unitRefundTotalCalculator);
public function let(RefundUnitsConverterInterface $refundUnitsConverter): void
{
$this->beConstructedWith($refundUnitsConverter);
}

public function it_implements_refund_units_command_creator_interface(): void
{
$this->shouldImplement(RefundUnitsCommandCreatorInterface::class);
}

public function it_creates_refund_units_command_from_request_with_full_prices(
UnitRefundTotalCalculatorInterface $unitRefundTotalCalculator,
public function it_creates_refund_units_command_from_request(
RefundUnitsConverterInterface $refundUnitsConverter,
Request $request
): void {
$firstUnitRefund = new OrderItemUnitRefund(1, 1000);
$secondUnitRefund = new OrderItemUnitRefund(2, 3000);
$shipmentRefund = new ShipmentRefund(1, 5000);

$request->attributes = new ParameterBag(['orderNumber' => '00001111']);
$request->request = new ParameterBag([
'sylius_refund_units' => [
Expand All @@ -53,54 +56,45 @@ public function it_creates_refund_units_command_from_request_with_full_prices(
'sylius_refund_comment' => 'Comment',
]);

$unitRefundTotalCalculator->calculateForUnitWithIdAndType(1, RefundType::orderItemUnit(), null)->willReturn(1000);
$unitRefundTotalCalculator->calculateForUnitWithIdAndType(2, RefundType::orderItemUnit(), null)->willReturn(3000);
$unitRefundTotalCalculator->calculateForUnitWithIdAndType(1, RefundType::shipment(), null)->willReturn(5000);
$refundUnitsConverter
->convert(
[
1 => ['full' => 'on'],
2 => ['full' => 'on'],
],
RefundType::orderItemUnit(),
OrderItemUnitRefund::class
)
->willReturn([$firstUnitRefund, $secondUnitRefund])
;
$refundUnitsConverter
->convert(
[1 => ['full' => 'on']],
RefundType::shipment(),
ShipmentRefund::class
)
->willReturn([$shipmentRefund])
;

$this->fromRequest($request)->shouldReturnCommand(new RefundUnits(
'00001111',
[new OrderItemUnitRefund(1, 1000), new OrderItemUnitRefund(2, 3000)],
[new ShipmentRefund(1, 5000)],
[$firstUnitRefund, $secondUnitRefund],
[$shipmentRefund],
1,
'Comment'
));
}

public function it_creates_refund_units_command_from_request_with_partial_prices(
UnitRefundTotalCalculatorInterface $unitRefundTotalCalculator,
public function it_throws_exception_if_there_is_no_units_nor_shipments_provided(
RefundUnitsConverterInterface $refundUnitsConverter,
Request $request
): void {
$request->attributes = new ParameterBag(['orderNumber' => '00001111']);
$request->request = new ParameterBag([
'sylius_refund_units' => [
1 => ['amount' => '10.00'],
2 => ['full' => 'on'],
],
'sylius_refund_shipments' => [
1 => ['amount' => '5.00'],
],
'sylius_refund_payment_method' => 1,
'sylius_refund_comment' => 'Comment',
]);

$unitRefundTotalCalculator->calculateForUnitWithIdAndType(1, RefundType::orderItemUnit(), 10.00)->willReturn(1000);
$unitRefundTotalCalculator->calculateForUnitWithIdAndType(2, RefundType::orderItemUnit(), null)->willReturn(3000);
$unitRefundTotalCalculator->calculateForUnitWithIdAndType(1, RefundType::shipment(), 5.00)->willReturn(500);

$this->fromRequest($request)->shouldReturnCommand(new RefundUnits(
'00001111',
[new OrderItemUnitRefund(1, 1000), new OrderItemUnitRefund(2, 3000)],
[new ShipmentRefund(1, 500)],
1,
'Comment'
));
}

public function it_throws_exception_if_there_is_no_units_nor_shipments_provided(Request $request): void
{
$request->attributes = new ParameterBag(['orderNumber' => '00001111']);
$request->request = new ParameterBag(['sylius_refund_payment_method' => 1]);

$refundUnitsConverter->convert([], RefundType::orderItemUnit(), OrderItemUnitRefund::class)->willReturn([]);
$refundUnitsConverter->convert([], RefundType::shipment(), ShipmentRefund::class)->willReturn([]);

$this
->shouldThrow(\InvalidArgumentException::class)
->during('fromRequest', [$request])
Expand Down
33 changes: 14 additions & 19 deletions spec/Factory/RefundPaymentFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Sylius\RefundPlugin\Entity\RefundPayment;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;
use Sylius\RefundPlugin\Factory\RefundPaymentFactory;
use Sylius\RefundPlugin\Factory\RefundPaymentFactoryInterface;

final class RefundPaymentFactorySpec extends ObjectBehavior
{
public function let(PaymentMethodRepositoryInterface $paymentMethodRepository): void
public function let(): void
{
$this->beConstructedWith($paymentMethodRepository);
$this->beConstructedWith(RefundPayment::class);
}

public function it_is_initializable(): void
Expand All @@ -39,29 +38,25 @@ public function it_implements_refund_payment_factory_interface(): void
$this->shouldImplement(RefundPaymentFactoryInterface::class);
}

public function it_creates_new_refund_payment(
PaymentMethodRepositoryInterface $paymentMethodRepository,
PaymentMethodInterface $paymentMethod,
OrderInterface $order
public function it_creates_a_new_refund_payment(
OrderInterface $order,
PaymentMethodInterface $paymentMethod
): void {
$paymentMethodRepository->find(1)->willReturn($paymentMethod);

$this
->createWithData(
$order,
1000,
'USD',
RefundPaymentInterface::STATE_NEW,
1
$paymentMethod
)
->shouldBeLike(
new RefundPayment(
$order->getWrappedObject(),
1000,
'USD',
RefundPaymentInterface::STATE_NEW,
$paymentMethod->getWrappedObject()
)
);
->shouldBeLike(new RefundPayment(
$order->getWrappedObject(),
1000,
'USD',
RefundPaymentInterface::STATE_NEW,
$paymentMethod->getWrappedObject()
))
;
}
}
20 changes: 12 additions & 8 deletions spec/ProcessManager/RefundPaymentProcessManagerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use Doctrine\ORM\EntityManagerInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;
use Sylius\RefundPlugin\Event\RefundPaymentGenerated;
use Sylius\RefundPlugin\Event\UnitsRefunded;
Expand All @@ -36,6 +38,7 @@ public function let(
RelatedPaymentIdProviderInterface $relatedPaymentIdProvider,
RefundPaymentFactoryInterface $refundPaymentFactory,
OrderRepositoryInterface $orderRepository,
PaymentMethodRepositoryInterface $paymentMethodRepository,
EntityManagerInterface $entityManager,
MessageBusInterface $eventBus
): void {
Expand All @@ -44,6 +47,7 @@ public function let(
$relatedPaymentIdProvider,
$refundPaymentFactory,
$orderRepository,
$paymentMethodRepository,
$entityManager,
$eventBus
);
Expand All @@ -59,20 +63,20 @@ public function it_reacts_on_units_refunded_event_and_creates_refund_payment(
RelatedPaymentIdProviderInterface $relatedPaymentIdProvider,
RefundPaymentFactoryInterface $refundPaymentFactory,
OrderRepositoryInterface $orderRepository,
PaymentMethodRepositoryInterface $paymentMethodRepository,
EntityManagerInterface $entityManager,
MessageBusInterface $eventBus,
RefundPaymentInterface $refundPayment,
OrderInterface $order
OrderInterface $order,
PaymentMethodInterface $paymentMethod
): void {
$orderRepository->findOneByNumber('000222')->willReturn($order);
$paymentMethodRepository->find(1)->willReturn($paymentMethod);

$refundPaymentFactory->createWithData(
$order,
1000,
'USD',
RefundPaymentInterface::STATE_NEW,
1
)->willReturn($refundPayment);
$refundPaymentFactory
->createWithData($order, 1000, 'USD', RefundPaymentInterface::STATE_NEW, $paymentMethod)
->willReturn($refundPayment)
;

$entityManager->persist($refundPayment)->shouldBeCalled();
$entityManager->flush()->shouldBeCalled();
Expand Down
Loading

0 comments on commit 07ca76d

Please sign in to comment.