|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Tests\Sylius\PayPalPlugin\Unit\Resolver; |
| 15 | + |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface; |
| 18 | +use Sylius\Component\Core\Model\ChannelInterface; |
| 19 | +use Sylius\Component\Core\Model\PaymentMethodInterface; |
| 20 | +use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface; |
| 21 | +use Sylius\PayPalPlugin\DependencyInjection\SyliusPayPalExtension; |
| 22 | +use Sylius\PayPalPlugin\Resolver\PayPalPaymentMethodsResolver; |
| 23 | + |
| 24 | +final class PayPalPaymentMethodsResolverTest extends TestCase |
| 25 | +{ |
| 26 | + public function test_returns_sorted_paypal_methods_from_channel(): void |
| 27 | + { |
| 28 | + $repository = $this->createMock(PaymentMethodRepositoryInterface::class); |
| 29 | + $channel = $this->createMock(ChannelInterface::class); |
| 30 | + |
| 31 | + $paypalGatewayLow = $this->createMock(GatewayConfigInterface::class); |
| 32 | + $paypalGatewayLow->method('getFactoryName')->willReturn(SyliusPayPalExtension::PAYPAL_FACTORY_NAME); |
| 33 | + |
| 34 | + $paypalMethodLow = $this->createMock(PaymentMethodInterface::class); |
| 35 | + $paypalMethodLow->method('getGatewayConfig')->willReturn($paypalGatewayLow); |
| 36 | + $paypalMethodLow->method('getPosition')->willReturn(10); |
| 37 | + |
| 38 | + $paypalGatewayHigh = $this->createMock(GatewayConfigInterface::class); |
| 39 | + $paypalGatewayHigh->method('getFactoryName')->willReturn(SyliusPayPalExtension::PAYPAL_FACTORY_NAME); |
| 40 | + |
| 41 | + $paypalMethodHigh = $this->createMock(PaymentMethodInterface::class); |
| 42 | + $paypalMethodHigh->method('getGatewayConfig')->willReturn($paypalGatewayHigh); |
| 43 | + $paypalMethodHigh->method('getPosition')->willReturn(5); |
| 44 | + |
| 45 | + $otherGateway = $this->createMock(GatewayConfigInterface::class); |
| 46 | + $otherGateway->method('getFactoryName')->willReturn('other_factory'); |
| 47 | + |
| 48 | + $otherMethod = $this->createMock(PaymentMethodInterface::class); |
| 49 | + $otherMethod->method('getGatewayConfig')->willReturn($otherGateway); |
| 50 | + |
| 51 | + $repository |
| 52 | + ->expects($this->once()) |
| 53 | + ->method('findEnabledForChannel') |
| 54 | + ->with($channel) |
| 55 | + ->willReturn([$paypalMethodLow, $otherMethod, $paypalMethodHigh]); |
| 56 | + |
| 57 | + $resolver = new PayPalPaymentMethodsResolver($repository); |
| 58 | + |
| 59 | + $result = $resolver->getInChannel($channel); |
| 60 | + |
| 61 | + $this->assertSame([$paypalMethodHigh, $paypalMethodLow], $result); |
| 62 | + } |
| 63 | + |
| 64 | + public function test_returns_empty_array_when_channel_has_no_paypal_methods(): void |
| 65 | + { |
| 66 | + $repository = $this->createMock(PaymentMethodRepositoryInterface::class); |
| 67 | + $channel = $this->createMock(ChannelInterface::class); |
| 68 | + |
| 69 | + $nonPaypalGateway = $this->createMock(GatewayConfigInterface::class); |
| 70 | + $nonPaypalGateway->method('getFactoryName')->willReturn('non_paypal'); |
| 71 | + |
| 72 | + $nonPaypalMethod = $this->createMock(PaymentMethodInterface::class); |
| 73 | + $nonPaypalMethod->method('getGatewayConfig')->willReturn($nonPaypalGateway); |
| 74 | + |
| 75 | + $repository |
| 76 | + ->expects($this->once()) |
| 77 | + ->method('findEnabledForChannel') |
| 78 | + ->with($channel) |
| 79 | + ->willReturn([$nonPaypalMethod]); |
| 80 | + |
| 81 | + $resolver = new PayPalPaymentMethodsResolver($repository); |
| 82 | + |
| 83 | + $this->assertSame([], $resolver->getInChannel($channel)); |
| 84 | + } |
| 85 | + |
| 86 | + public function test_returns_empty_array_when_channel_has_no_payment_methods(): void |
| 87 | + { |
| 88 | + $repository = $this->createMock(PaymentMethodRepositoryInterface::class); |
| 89 | + $channel = $this->createMock(ChannelInterface::class); |
| 90 | + |
| 91 | + $repository |
| 92 | + ->expects($this->once()) |
| 93 | + ->method('findEnabledForChannel') |
| 94 | + ->with($channel) |
| 95 | + ->willReturn([]); |
| 96 | + |
| 97 | + $resolver = new PayPalPaymentMethodsResolver($repository); |
| 98 | + |
| 99 | + $this->assertSame([], $resolver->getInChannel($channel)); |
| 100 | + } |
| 101 | + |
| 102 | + public function test_skips_methods_without_gateway_configuration(): void |
| 103 | + { |
| 104 | + $repository = $this->createMock(PaymentMethodRepositoryInterface::class); |
| 105 | + $channel = $this->createMock(ChannelInterface::class); |
| 106 | + |
| 107 | + $paypalGateway = $this->createMock(GatewayConfigInterface::class); |
| 108 | + $paypalGateway->method('getFactoryName')->willReturn(SyliusPayPalExtension::PAYPAL_FACTORY_NAME); |
| 109 | + |
| 110 | + $paypalMethod = $this->createMock(PaymentMethodInterface::class); |
| 111 | + $paypalMethod->method('getGatewayConfig')->willReturn($paypalGateway); |
| 112 | + $paypalMethod->method('getPosition')->willReturn(1); |
| 113 | + |
| 114 | + $methodWithoutGateway = $this->createMock(PaymentMethodInterface::class); |
| 115 | + $methodWithoutGateway->method('getGatewayConfig')->willReturn(null); |
| 116 | + |
| 117 | + $repository |
| 118 | + ->expects($this->once()) |
| 119 | + ->method('findEnabledForChannel') |
| 120 | + ->with($channel) |
| 121 | + ->willReturn([$methodWithoutGateway, $paypalMethod]); |
| 122 | + |
| 123 | + $resolver = new PayPalPaymentMethodsResolver($repository); |
| 124 | + |
| 125 | + $this->assertSame([$paypalMethod], $resolver->getInChannel($channel)); |
| 126 | + } |
| 127 | +} |
0 commit comments