Skip to content

Commit

Permalink
[RefundPayment] Adjust DefaultRelatedPaymentIdProvider after changing…
Browse files Browse the repository at this point in the history
… the order relation
  • Loading branch information
GSadee committed Jun 9, 2021
1 parent bb82f87 commit 78a3224
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 48 deletions.
2 changes: 2 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
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
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
22 changes: 2 additions & 20 deletions src/Provider/DefaultRelatedPaymentIdProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,19 @@

namespace Sylius\RefundPlugin\Provider;

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;

final class DefaultRelatedPaymentIdProvider implements RelatedPaymentIdProviderInterface
{
/** @var OrderRepositoryInterface */
private $orderRepository;

public function __construct(OrderRepositoryInterface $orderRepository)
{
$this->orderRepository = $orderRepository;
}

public function getForRefundPayment(RefundPaymentInterface $refundPayment): int
{
$orderNumber = $refundPayment->getOrderNumber();
/** @var OrderInterface|null $order */
$order = $this->orderRepository->findOneByNumber($orderNumber);

if ($order === null) {
throw OrderNotFound::withNumber($orderNumber);
}

$order = $refundPayment->getOrder();
$payment = $order->getLastPayment(PaymentInterface::STATE_COMPLETED);

if ($payment === null) {
throw CompletedPaymentNotFound::withNumber($orderNumber);
throw CompletedPaymentNotFound::withNumber((string) $order->getNumber());
}

return $payment->getId();
Expand Down
1 change: 0 additions & 1 deletion src/Resources/config/services/providers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
id="Sylius\RefundPlugin\Provider\RelatedPaymentIdProviderInterface"
class="Sylius\RefundPlugin\Provider\DefaultRelatedPaymentIdProvider"
>
<argument type="service" id="sylius.repository.order" />
</service>

<service
Expand Down

0 comments on commit 78a3224

Please sign in to comment.