|
| 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\Bundle\PayumBundle\OrderPay\Provider; |
| 15 | + |
| 16 | +use Payum\Core\Payum; |
| 17 | +use Payum\Core\Security\GenericTokenFactoryInterface; |
| 18 | +use Payum\Core\Security\TokenInterface; |
| 19 | +use PHPUnit\Framework\MockObject\MockObject; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use Sylius\Bundle\CoreBundle\OrderPay\Resolver\PaymentToPayResolverInterface; |
| 22 | +use Sylius\Bundle\PayumBundle\Model\GatewayConfig; |
| 23 | +use Sylius\Bundle\PayumBundle\OrderPay\Provider\PayumPayResponseProvider; |
| 24 | +use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration; |
| 25 | +use Sylius\Component\Core\Model\OrderInterface; |
| 26 | +use Sylius\Component\Core\Model\PaymentInterface; |
| 27 | +use Sylius\Component\Core\Model\PaymentMethodInterface; |
| 28 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 29 | + |
| 30 | +final class PayumPayResponseProviderTest extends TestCase |
| 31 | +{ |
| 32 | + private MockObject&Payum $payum; |
| 33 | + |
| 34 | + private MockObject&PaymentToPayResolverInterface $paymentToPayResolver; |
| 35 | + |
| 36 | + private GenericTokenFactoryInterface&MockObject $tokenFactory; |
| 37 | + |
| 38 | + private PayumPayResponseProvider $provider; |
| 39 | + |
| 40 | + protected function setUp(): void |
| 41 | + { |
| 42 | + $this->payum = $this->createMock(Payum::class); |
| 43 | + $this->paymentToPayResolver = $this->createMock(PaymentToPayResolverInterface::class); |
| 44 | + $this->tokenFactory = $this->createMock(GenericTokenFactoryInterface::class); |
| 45 | + |
| 46 | + $this->provider = new PayumPayResponseProvider( |
| 47 | + $this->payum, |
| 48 | + $this->paymentToPayResolver, |
| 49 | + 'app_shop_order_after_pay', |
| 50 | + ['tokenValue' => 'fixed-token-value'], |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + public function testItCreatesCaptureTokenWithConfiguredAfterPayRouteAndParameters(): void |
| 55 | + { |
| 56 | + $requestConfiguration = $this->createStub(RequestConfiguration::class); |
| 57 | + $order = $this->createOrder(); |
| 58 | + $payment = $this->createPaymentWithGatewayConfig(['use_authorize' => false]); |
| 59 | + $token = $this->createMock(TokenInterface::class); |
| 60 | + |
| 61 | + $this->paymentToPayResolver |
| 62 | + ->expects(self::once()) |
| 63 | + ->method('getPayment') |
| 64 | + ->with($order) |
| 65 | + ->willReturn($payment) |
| 66 | + ; |
| 67 | + $this->payum->expects(self::once())->method('getTokenFactory')->willReturn($this->tokenFactory); |
| 68 | + $this->tokenFactory |
| 69 | + ->expects(self::once()) |
| 70 | + ->method('createCaptureToken') |
| 71 | + ->with( |
| 72 | + 'offline', |
| 73 | + $payment, |
| 74 | + 'app_shop_order_after_pay', |
| 75 | + ['tokenValue' => 'fixed-token-value'], |
| 76 | + ) |
| 77 | + ->willReturn($token) |
| 78 | + ; |
| 79 | + $this->tokenFactory->expects(self::never())->method('createAuthorizeToken'); |
| 80 | + $token->expects(self::once())->method('getTargetUrl')->willReturn('/payum/capture'); |
| 81 | + |
| 82 | + $response = $this->provider->getResponse($requestConfiguration, $order); |
| 83 | + |
| 84 | + self::assertInstanceOf(RedirectResponse::class, $response); |
| 85 | + self::assertSame('/payum/capture', $response->getTargetUrl()); |
| 86 | + } |
| 87 | + |
| 88 | + public function testItCreatesAuthorizeTokenWithConfiguredAfterPayRouteAndParameters(): void |
| 89 | + { |
| 90 | + $requestConfiguration = $this->createStub(RequestConfiguration::class); |
| 91 | + $order = $this->createOrder(); |
| 92 | + $payment = $this->createPaymentWithGatewayConfig(['use_authorize' => true]); |
| 93 | + $token = $this->createMock(TokenInterface::class); |
| 94 | + |
| 95 | + $this->paymentToPayResolver |
| 96 | + ->expects(self::once()) |
| 97 | + ->method('getPayment') |
| 98 | + ->with($order) |
| 99 | + ->willReturn($payment) |
| 100 | + ; |
| 101 | + $this->payum->expects(self::once())->method('getTokenFactory')->willReturn($this->tokenFactory); |
| 102 | + $this->tokenFactory |
| 103 | + ->expects(self::once()) |
| 104 | + ->method('createAuthorizeToken') |
| 105 | + ->with( |
| 106 | + 'offline', |
| 107 | + $payment, |
| 108 | + 'app_shop_order_after_pay', |
| 109 | + ['tokenValue' => 'fixed-token-value'], |
| 110 | + ) |
| 111 | + ->willReturn($token) |
| 112 | + ; |
| 113 | + $this->tokenFactory->expects(self::never())->method('createCaptureToken'); |
| 114 | + $token->expects(self::once())->method('getTargetUrl')->willReturn('/payum/authorize'); |
| 115 | + |
| 116 | + $response = $this->provider->getResponse($requestConfiguration, $order); |
| 117 | + |
| 118 | + self::assertInstanceOf(RedirectResponse::class, $response); |
| 119 | + self::assertSame('/payum/authorize', $response->getTargetUrl()); |
| 120 | + } |
| 121 | + |
| 122 | + public function testItSupportsOrdersWithPayumEnabledPayment(): void |
| 123 | + { |
| 124 | + $requestConfiguration = $this->createStub(RequestConfiguration::class); |
| 125 | + $order = $this->createOrder(); |
| 126 | + $payment = $this->createPaymentWithGatewayConfig([]); |
| 127 | + |
| 128 | + $this->paymentToPayResolver |
| 129 | + ->expects(self::once()) |
| 130 | + ->method('getPayment') |
| 131 | + ->with($order) |
| 132 | + ->willReturn($payment) |
| 133 | + ; |
| 134 | + |
| 135 | + self::assertTrue($this->provider->supports($requestConfiguration, $order)); |
| 136 | + } |
| 137 | + |
| 138 | + public function testItDoesNotSupportOrdersWithPayumDisabledPayment(): void |
| 139 | + { |
| 140 | + $requestConfiguration = $this->createStub(RequestConfiguration::class); |
| 141 | + $order = $this->createOrder(); |
| 142 | + $gatewayConfig = new GatewayConfig(); |
| 143 | + $gatewayConfig->setUsePayum(false); |
| 144 | + $payment = $this->createPayment($gatewayConfig); |
| 145 | + |
| 146 | + $this->paymentToPayResolver |
| 147 | + ->expects(self::once()) |
| 148 | + ->method('getPayment') |
| 149 | + ->with($order) |
| 150 | + ->willReturn($payment) |
| 151 | + ; |
| 152 | + |
| 153 | + self::assertFalse($this->provider->supports($requestConfiguration, $order)); |
| 154 | + } |
| 155 | + |
| 156 | + private function createOrder(): MockObject&OrderInterface |
| 157 | + { |
| 158 | + $order = $this->createMock(OrderInterface::class); |
| 159 | + $order->method('getId')->willReturn(1); |
| 160 | + |
| 161 | + return $order; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @param array<string, mixed> $config |
| 166 | + */ |
| 167 | + private function createPaymentWithGatewayConfig(array $config): MockObject&PaymentInterface |
| 168 | + { |
| 169 | + $gatewayConfig = new GatewayConfig(); |
| 170 | + $gatewayConfig->setGatewayName('offline'); |
| 171 | + $gatewayConfig->setConfig($config); |
| 172 | + |
| 173 | + return $this->createPayment($gatewayConfig); |
| 174 | + } |
| 175 | + |
| 176 | + private function createPayment(GatewayConfig $gatewayConfig): MockObject&PaymentInterface |
| 177 | + { |
| 178 | + $paymentMethod = $this->createMock(PaymentMethodInterface::class); |
| 179 | + $paymentMethod->method('getGatewayConfig')->willReturn($gatewayConfig); |
| 180 | + |
| 181 | + $payment = $this->createMock(PaymentInterface::class); |
| 182 | + $payment->method('getMethod')->willReturn($paymentMethod); |
| 183 | + |
| 184 | + return $payment; |
| 185 | + } |
| 186 | +} |
0 commit comments