|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\Refunding\Provider; |
| 6 | + |
| 7 | +use CommerceWeavers\SyliusTpayPlugin\Refunding\Provider\TpayAwareRefundPaymentMethodsProvider; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 10 | +use Prophecy\Prophecy\ObjectProphecy; |
| 11 | +use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface; |
| 12 | +use Sylius\Component\Core\Model\OrderInterface; |
| 13 | +use Sylius\Component\Core\Model\PaymentInterface; |
| 14 | +use Sylius\Component\Core\Model\PaymentMethodInterface; |
| 15 | +use Sylius\RefundPlugin\Provider\RefundPaymentMethodsProviderInterface; |
| 16 | + |
| 17 | + |
| 18 | +final class TpayAwareRefundPaymentMethodsProviderTest extends TestCase |
| 19 | +{ |
| 20 | + use ProphecyTrait; |
| 21 | + |
| 22 | + private RefundPaymentMethodsProviderInterface|ObjectProphecy $inner; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + $this->inner = $this->prophesize(RefundPaymentMethodsProviderInterface::class); |
| 27 | + } |
| 28 | + |
| 29 | + public function test_it_returns_inner_methods_when_no_last_completed_payment(): void |
| 30 | + { |
| 31 | + $order = $this->prophesize(OrderInterface::class); |
| 32 | + $order->getLastPayment(PaymentInterface::STATE_COMPLETED)->willReturn(null); |
| 33 | + |
| 34 | + $methods = [$this->createMethod('methodA'), $this->createMethod('methodB')]; |
| 35 | + $this->inner->findForOrder($order->reveal())->willReturn($methods); |
| 36 | + |
| 37 | + $provider = new TpayAwareRefundPaymentMethodsProvider($this->inner->reveal()); |
| 38 | + |
| 39 | + self::assertSame($methods, $provider->findForOrder($order->reveal())); |
| 40 | + } |
| 41 | + |
| 42 | + public function test_it_returns_inner_methods_when_last_payment_has_transaction_id(): void |
| 43 | + { |
| 44 | + $order = $this->prophesize(OrderInterface::class); |
| 45 | + $payment = $this->prophesize(PaymentInterface::class); |
| 46 | + $order->getLastPayment(PaymentInterface::STATE_COMPLETED)->willReturn($payment->reveal()); |
| 47 | + |
| 48 | + $payment->getDetails()->willReturn(['tpay' => ['transaction_id' => 'tr_123']]); |
| 49 | + |
| 50 | + $methods = [$this->createMethod('methodA'), $this->createMethod('methodB')]; |
| 51 | + $this->inner->findForOrder($order->reveal())->willReturn($methods); |
| 52 | + |
| 53 | + $provider = new TpayAwareRefundPaymentMethodsProvider($this->inner->reveal()); |
| 54 | + |
| 55 | + self::assertSame($methods, $provider->findForOrder($order->reveal())); |
| 56 | + } |
| 57 | + |
| 58 | + public function test_it_filters_out_tpay_redirect_when_no_transaction_id(): void |
| 59 | + { |
| 60 | + $order = $this->prophesize(OrderInterface::class); |
| 61 | + $payment = $this->prophesize(PaymentInterface::class); |
| 62 | + $order->getLastPayment(PaymentInterface::STATE_COMPLETED)->willReturn($payment->reveal()); |
| 63 | + |
| 64 | + $payment->getDetails()->willReturn(['tpay' => []]); |
| 65 | + |
| 66 | + $tpayRedirectMethod = $this->createMethod('tpay_redirect'); |
| 67 | + $otherMethod = $this->createMethod('foo'); |
| 68 | + |
| 69 | + $this->inner->findForOrder($order->reveal()) |
| 70 | + ->willReturn([$tpayRedirectMethod, $otherMethod]); |
| 71 | + |
| 72 | + $provider = new TpayAwareRefundPaymentMethodsProvider($this->inner->reveal()); |
| 73 | + |
| 74 | + self::assertSame([$otherMethod], $provider->findForOrder($order->reveal())); |
| 75 | + } |
| 76 | + |
| 77 | + private function createMethod(string $factoryName): PaymentMethodInterface |
| 78 | + { |
| 79 | + $gatewayConfig = $this->prophesize(GatewayConfigInterface::class); |
| 80 | + $gatewayConfig->getFactoryName()->willReturn($factoryName); |
| 81 | + |
| 82 | + $method = $this->prophesize(PaymentMethodInterface::class); |
| 83 | + $method->getGatewayConfig()->willReturn($gatewayConfig->reveal()); |
| 84 | + |
| 85 | + return $method->reveal(); |
| 86 | + } |
| 87 | +} |
0 commit comments