Skip to content

Commit

Permalink
minor #307 [RefundPayment] Change order number to Order relation on R…
Browse files Browse the repository at this point in the history
…efundPayment (GSadee)

This PR was merged into the 1.0-dev branch.

Discussion
----------

Partially fixes #193

Similar to #306 

Commits
-------

91173ec [RefundPayment] Change order number to Order relation on RefundPayment
eee327c [RefundPayment] Add migration for changing the order relation
d278a5d [RefundPayment] Add note in UPGRADE file about changing the order relation
545d719 [Behat] Fix FailedRefundPaymentFactory after changing the order relation
8a526ab [RefundPayment] Fixes after static analysis
f464e20 [RefundPayment] Adjust DefaultRelatedPaymentIdProvider after changing the order relation
371a21c [RefundPayment] Adjust routing and twig after changing the order relation
4b25791 [RefundPayment] Introduce custom repository
  • Loading branch information
Zales0123 authored Jun 10, 2021
2 parents 7c0fb07 + 4b25791 commit 69d43d2
Show file tree
Hide file tree
Showing 20 changed files with 210 additions and 83 deletions.
19 changes: 17 additions & 2 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
### UPGRADE FROM 1.0.0-RC.10 TO 1.0.0-RC.11

1. `orderNumber` field on `Sylius\RefundPlugin\Entity\Refund` has been removed and replaced with relation to `Order` entity.
1. `orderNumber` field on `Sylius\RefundPlugin\Entity\Refund` and `Sylius\RefundPlugin\Entity\RefundPayment` has been removed
and replaced with relation to `Order` entity.

1. `Sylius\RefundPlugin\Entity\RefundInterface::getOrderNumber` function is left due to easier and smoother upgrades,
but is also deprecated and will be removed in the `v1.0.0` release. Use `Sylius\RefundPlugin\Entity\RefundInterface::getOrder` instead.
1. `Sylius\RefundPlugin\Creator\RefundCreator` takes `\Sylius\Component\Core\Repository\OrderRepositoryInterface`

1. `Sylius\RefundPlugin\Entity\RefundPaymentInterface::getOrderNumber` function is left due to easier and smoother upgrades,
but is also deprecated and will be removed in the `v1.0.0` release. Use `Sylius\RefundPlugin\Entity\RefundPaymentInterface::getOrder` instead.

1. `Sylius\RefundPlugin\Creator\RefundCreator` takes `Sylius\Component\Core\Repository\OrderRepositoryInterface`
as the 3rd argument.

1. `Sylius\RefundPlugin\ProcessManager\RefundPaymentProcessManager` takes `Sylius\Component\Core\Repository\OrderRepositoryInterface`
as the 4th argument.

1. `Sylius\RefundPlugin\Provider\OrderRefundedTotalProviderInterface::invoke` takes `OrderInterface $order` as an argument
instead of `string $orderNumber`

1. `Sylius\RefundPlugin\Factory\RefundPaymentFactory::createWithData` takes `OrderInterface $order` as an argument
instead of `string $orderNumber`

1. The constructor of `Sylius\RefundPlugin\Provider\DefaultRelatedPaymentIdProvider` has been removed.

### 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
10 changes: 7 additions & 3 deletions spec/Entity/RefundPaymentSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
namespace spec\Sylius\RefundPlugin\Entity;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\RefundPlugin\Entity\RefundPayment;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;

final class RefundPaymentSpec extends ObjectBehavior
{
public function let(PaymentMethodInterface $paymentMethod): void
public function let(OrderInterface $order, PaymentMethodInterface $paymentMethod): void
{
$this->beConstructedWith('000002', 100, 'USD', RefundPaymentInterface::STATE_NEW, $paymentMethod);
$this->beConstructedWith($order, 100, 'USD', RefundPaymentInterface::STATE_NEW, $paymentMethod);
}

public function it_is_initializable(): void
Expand All @@ -40,8 +41,11 @@ public function it_has_no_id_by_default(): void
$this->getId()->shouldReturn(null);
}

public function it_has_order_number(): void
public function it_has_an_order(OrderInterface $order): void
{
$order->getNumber()->willReturn('000002');

$this->getOrder()->shouldReturn($order);
$this->getOrderNumber()->shouldReturn('000002');
}

Expand Down
28 changes: 16 additions & 12 deletions spec/Factory/RefundPaymentFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace spec\Sylius\RefundPlugin\Factory;

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;
Expand All @@ -40,24 +41,27 @@ public function it_implements_refund_payment_factory_interface(): void

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

$this->createWithData(
'0002',
1000,
'USD',
RefundPaymentInterface::STATE_NEW,
1
)->shouldBeLike(
new RefundPayment(
'0002',
$this
->createWithData(
$order,
1000,
'USD',
RefundPaymentInterface::STATE_NEW,
$paymentMethod->getWrappedObject()
1
)
);
->shouldBeLike(
new RefundPayment(
$order->getWrappedObject(),
1000,
'USD',
RefundPaymentInterface::STATE_NEW,
$paymentMethod->getWrappedObject()
)
);
}
}
14 changes: 11 additions & 3 deletions spec/ProcessManager/RefundPaymentProcessManagerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use Doctrine\ORM\EntityManagerInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;
use Sylius\RefundPlugin\Event\RefundPaymentGenerated;
use Sylius\RefundPlugin\Event\UnitsRefunded;
Expand All @@ -33,13 +35,15 @@ public function let(
OrderFullyRefundedStateResolverInterface $orderFullyRefundedStateResolver,
RelatedPaymentIdProviderInterface $relatedPaymentIdProvider,
RefundPaymentFactoryInterface $refundPaymentFactory,
OrderRepositoryInterface $orderRepository,
EntityManagerInterface $entityManager,
MessageBusInterface $eventBus
): void {
$this->beConstructedWith(
$orderFullyRefundedStateResolver,
$relatedPaymentIdProvider,
$refundPaymentFactory,
$orderRepository,
$entityManager,
$eventBus
);
Expand All @@ -54,12 +58,16 @@ public function it_reacts_on_units_refunded_event_and_creates_refund_payment(
OrderFullyRefundedStateResolverInterface $orderFullyRefundedStateResolver,
RelatedPaymentIdProviderInterface $relatedPaymentIdProvider,
RefundPaymentFactoryInterface $refundPaymentFactory,
OrderRepositoryInterface $orderRepository,
EntityManagerInterface $entityManager,
MessageBusInterface $eventBus,
RefundPaymentInterface $refundPayment,
MessageBusInterface $eventBus
OrderInterface $order
): void {
$orderRepository->findOneByNumber('000222')->willReturn($order);

$refundPaymentFactory->createWithData(
'000222',
$order,
1000,
'USD',
RefundPaymentInterface::STATE_NEW,
Expand All @@ -72,7 +80,7 @@ public function it_reacts_on_units_refunded_event_and_creates_refund_payment(
$orderFullyRefundedStateResolver->resolve('000222')->shouldBeCalled();

$refundPayment->getId()->willReturn(10);
$refundPayment->getOrderNumber()->willReturn('000222');
$refundPayment->getOrder()->willReturn($order);
$refundPayment->getAmount()->willReturn(1000);

$relatedPaymentIdProvider->getForRefundPayment($refundPayment)->willReturn(3);
Expand Down
30 changes: 3 additions & 27 deletions spec/Provider/DefaultRelatedPaymentIdProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,23 @@
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;
use Sylius\RefundPlugin\Exception\CompletedPaymentNotFound;
use Sylius\RefundPlugin\Exception\OrderNotFound;
use Sylius\RefundPlugin\Provider\RelatedPaymentIdProviderInterface;

final class DefaultRelatedPaymentIdProviderSpec extends ObjectBehavior
{
public function let(OrderRepositoryInterface $orderRepository): void
{
$this->beConstructedWith($orderRepository);
}

public function it_implements_related_payment_id_provider_interface(): void
{
$this->shouldImplement(RelatedPaymentIdProviderInterface::class);
}

public function it_provides_id_of_last_completed_payment_from_refund_payment_order(
OrderRepositoryInterface $orderRepository,
RefundPaymentInterface $refundPayment,
OrderInterface $order,
PaymentInterface $payment
): void {
$refundPayment->getOrderNumber()->willReturn('000333');

$orderRepository->findOneByNumber('000333')->willReturn($order);
$refundPayment->getOrder()->willReturn($order);

$order->getLastPayment(PaymentInterface::STATE_COMPLETED)->willReturn($payment);

Expand All @@ -51,26 +41,12 @@ public function it_provides_id_of_last_completed_payment_from_refund_payment_ord
$this->getForRefundPayment($refundPayment)->shouldReturn(4);
}

public function it_throws_exception_if_there_is_no_order_with_given_number(
OrderRepositoryInterface $orderRepository,
RefundPaymentInterface $refundPayment
): void {
$refundPayment->getOrderNumber()->willReturn('000666');
$orderRepository->findOneByNumber('000666')->willReturn(null);

$this
->shouldThrow(OrderNotFound::withNumber('000666'))
->during('getForRefundPayment', [$refundPayment])
;
}

public function it_throws_exception_if_order_has_no_completed_payments(
OrderRepositoryInterface $orderRepository,
RefundPaymentInterface $refundPayment,
OrderInterface $order
): void {
$refundPayment->getOrderNumber()->willReturn('000666');
$orderRepository->findOneByNumber('000666')->willReturn($order);
$refundPayment->getOrder()->willReturn($order);
$order->getNumber()->willReturn('000666');

$order->getLastPayment(PaymentInterface::STATE_COMPLETED)->willReturn(null);

Expand Down
31 changes: 31 additions & 0 deletions src/Doctrine/ORM/RefundPaymentRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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 Sylius\RefundPlugin\Doctrine\ORM;

use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use Sylius\RefundPlugin\Repository\RefundPaymentRepositoryInterface;

class RefundPaymentRepository extends EntityRepository implements RefundPaymentRepositoryInterface
{
public function findByOrderNumber(string $orderNumber): array
{
return $this->createQueryBuilder('o')
->innerJoin('o.order', 'ord')
->andWhere('ord.number = :orderNumber')
->setParameter('orderNumber', $orderNumber)
->getQuery()
->getResult()
;
}
}
16 changes: 11 additions & 5 deletions src/Entity/RefundPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@

namespace Sylius\RefundPlugin\Entity;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;

class RefundPayment implements RefundPaymentInterface
{
/** @var int|null */
protected $id;

/** @var string */
protected $orderNumber;
/** @var OrderInterface */
protected $order;

/** @var int */
protected $amount;
Expand All @@ -36,13 +37,13 @@ class RefundPayment implements RefundPaymentInterface
protected $paymentMethod;

public function __construct(
string $orderNumber,
OrderInterface $order,
int $amount,
string $currencyCode,
string $state,
PaymentMethodInterface $paymentMethod
) {
$this->orderNumber = $orderNumber;
$this->order = $order;
$this->amount = $amount;
$this->currencyCode = $currencyCode;
$this->state = $state;
Expand All @@ -54,9 +55,14 @@ public function getId(): ?int
return $this->id;
}

public function getOrder(): OrderInterface
{
return $this->order;
}

public function getOrderNumber(): string
{
return $this->orderNumber;
return (string) $this->getOrder()->getNumber();
}

public function getAmount(): int
Expand Down
4 changes: 4 additions & 0 deletions src/Entity/RefundPaymentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sylius\RefundPlugin\Entity;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Resource\Model\ResourceInterface;

Expand All @@ -22,6 +23,9 @@ interface RefundPaymentInterface extends ResourceInterface

public const STATE_COMPLETED = 'completed';

public function getOrder(): OrderInterface;

/** @deprecated this function is deprecated and will be removed in v1.0. Use RefundPaymentInterface::getOrder() instead */
public function getOrderNumber(): string;

public function getAmount(): int;
Expand Down
5 changes: 3 additions & 2 deletions src/Factory/RefundPaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sylius\RefundPlugin\Factory;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Sylius\RefundPlugin\Entity\RefundPayment;
Expand All @@ -29,7 +30,7 @@ public function __construct(PaymentMethodRepositoryInterface $paymentMethodRepos
}

public function createWithData(
string $orderNumber,
OrderInterface $order,
int $amount,
string $currencyCode,
string $state,
Expand All @@ -38,6 +39,6 @@ public function createWithData(
/** @var PaymentMethodInterface $paymentMethod */
$paymentMethod = $this->paymentMethodRepository->find($paymentMethodId);

return new RefundPayment($orderNumber, $amount, $currencyCode, $state, $paymentMethod);
return new RefundPayment($order, $amount, $currencyCode, $state, $paymentMethod);
}
}
3 changes: 2 additions & 1 deletion src/Factory/RefundPaymentFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

namespace Sylius\RefundPlugin\Factory;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;

interface RefundPaymentFactoryInterface
{
public function createWithData(
string $orderNumber,
OrderInterface $order,
int $amount,
string $currencyCode,
string $state,
Expand Down
Loading

0 comments on commit 69d43d2

Please sign in to comment.