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

[RefundPayment] Make the Refund customization easier #320

Merged
merged 7 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public in spec 💃

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? public is a default one if there is not access modifier specified

{
$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
49 changes: 32 additions & 17 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,45 @@ public function it_implements_refund_payment_factory_interface(): void
$this->shouldImplement(RefundPaymentFactoryInterface::class);
}

public function it_creates_new_refund_payment(
PaymentMethodRepositoryInterface $paymentMethodRepository,
public function it_creates_a_new_refund_payment(
OrderInterface $order,
PaymentMethodInterface $paymentMethod,
OrderInterface $order
RefundPaymentInterface $refundPayment
): void {
$paymentMethodRepository->find(1)->willReturn($paymentMethod);
$refundPayment->getOrder()->willReturn($order);
$refundPayment->getAmount()->willReturn(1000);
$refundPayment->getCurrencyCode()->willReturn('USD');
$refundPayment->getState()->willReturn(RefundPaymentInterface::STATE_NEW);
$refundPayment->getPaymentMethod()->willReturn($paymentMethod);

$this
->createWithData(
$order,
1000,
'USD',
RefundPaymentInterface::STATE_NEW,
1
$paymentMethod
)
->shouldBeLike(
new RefundPayment(
$order->getWrappedObject(),
1000,
'USD',
RefundPaymentInterface::STATE_NEW,
$paymentMethod->getWrappedObject()
)
);
->shouldBeSameAs($refundPayment)
GSadee marked this conversation as resolved.
Show resolved Hide resolved
;
}

public function getMatchers(): array
{
return [
'beSameAs' => function ($subject, $key) {
if (!$subject instanceof RefundPaymentInterface || !$key instanceof RefundPaymentInterface) {
return false;
}

return
$subject->getOrder() === $key->getOrder() &&
$subject->getAmount() === $key->getAmount() &&
$subject->getCurrencyCode() === $key->getCurrencyCode() &&
$subject->getState() === $key->getState() &&
$subject->getPaymentMethod() === $key->getPaymentMethod()
;
},
];
}
}
Loading