test: add tests for bundle config

This commit is contained in:
Francis Hilaire 2026-06-22 15:24:36 +02:00
parent 95c0bb039f
commit 368f9f5333
No known key found for this signature in database
GPG key ID: 3392F830BF33D421
5 changed files with 264 additions and 2 deletions

View file

@ -29,7 +29,6 @@ use Webmozart\Assert\Assert;
/** @experimental */
final class PayumPayResponseProvider implements PayResponseProviderInterface
{
/**
* @param array<string, string> $afterPayUrlParameters
*/

View file

@ -0,0 +1,186 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Tests\Sylius\Bundle\PayumBundle\OrderPay\Provider;
use Payum\Core\Payum;
use Payum\Core\Security\GenericTokenFactoryInterface;
use Payum\Core\Security\TokenInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sylius\Bundle\CoreBundle\OrderPay\Resolver\PaymentToPayResolverInterface;
use Sylius\Bundle\PayumBundle\Model\GatewayConfig;
use Sylius\Bundle\PayumBundle\OrderPay\Provider\PayumPayResponseProvider;
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
final class PayumPayResponseProviderTest extends TestCase
{
private MockObject&Payum $payum;
private MockObject&PaymentToPayResolverInterface $paymentToPayResolver;
private GenericTokenFactoryInterface&MockObject $tokenFactory;
private PayumPayResponseProvider $provider;
protected function setUp(): void
{
$this->payum = $this->createMock(Payum::class);
$this->paymentToPayResolver = $this->createMock(PaymentToPayResolverInterface::class);
$this->tokenFactory = $this->createMock(GenericTokenFactoryInterface::class);
$this->provider = new PayumPayResponseProvider(
$this->payum,
$this->paymentToPayResolver,
'app_shop_order_after_pay',
['tokenValue' => 'fixed-token-value'],
);
}
public function testItCreatesCaptureTokenWithConfiguredAfterPayRouteAndParameters(): void
{
$requestConfiguration = $this->createStub(RequestConfiguration::class);
$order = $this->createOrder();
$payment = $this->createPaymentWithGatewayConfig(['use_authorize' => false]);
$token = $this->createMock(TokenInterface::class);
$this->paymentToPayResolver
->expects(self::once())
->method('getPayment')
->with($order)
->willReturn($payment)
;
$this->payum->expects(self::once())->method('getTokenFactory')->willReturn($this->tokenFactory);
$this->tokenFactory
->expects(self::once())
->method('createCaptureToken')
->with(
'offline',
$payment,
'app_shop_order_after_pay',
['tokenValue' => 'fixed-token-value'],
)
->willReturn($token)
;
$this->tokenFactory->expects(self::never())->method('createAuthorizeToken');
$token->expects(self::once())->method('getTargetUrl')->willReturn('/payum/capture');
$response = $this->provider->getResponse($requestConfiguration, $order);
self::assertInstanceOf(RedirectResponse::class, $response);
self::assertSame('/payum/capture', $response->getTargetUrl());
}
public function testItCreatesAuthorizeTokenWithConfiguredAfterPayRouteAndParameters(): void
{
$requestConfiguration = $this->createStub(RequestConfiguration::class);
$order = $this->createOrder();
$payment = $this->createPaymentWithGatewayConfig(['use_authorize' => true]);
$token = $this->createMock(TokenInterface::class);
$this->paymentToPayResolver
->expects(self::once())
->method('getPayment')
->with($order)
->willReturn($payment)
;
$this->payum->expects(self::once())->method('getTokenFactory')->willReturn($this->tokenFactory);
$this->tokenFactory
->expects(self::once())
->method('createAuthorizeToken')
->with(
'offline',
$payment,
'app_shop_order_after_pay',
['tokenValue' => 'fixed-token-value'],
)
->willReturn($token)
;
$this->tokenFactory->expects(self::never())->method('createCaptureToken');
$token->expects(self::once())->method('getTargetUrl')->willReturn('/payum/authorize');
$response = $this->provider->getResponse($requestConfiguration, $order);
self::assertInstanceOf(RedirectResponse::class, $response);
self::assertSame('/payum/authorize', $response->getTargetUrl());
}
public function testItSupportsOrdersWithPayumEnabledPayment(): void
{
$requestConfiguration = $this->createStub(RequestConfiguration::class);
$order = $this->createOrder();
$payment = $this->createPaymentWithGatewayConfig([]);
$this->paymentToPayResolver
->expects(self::once())
->method('getPayment')
->with($order)
->willReturn($payment)
;
self::assertTrue($this->provider->supports($requestConfiguration, $order));
}
public function testItDoesNotSupportOrdersWithPayumDisabledPayment(): void
{
$requestConfiguration = $this->createStub(RequestConfiguration::class);
$order = $this->createOrder();
$gatewayConfig = new GatewayConfig();
$gatewayConfig->setUsePayum(false);
$payment = $this->createPayment($gatewayConfig);
$this->paymentToPayResolver
->expects(self::once())
->method('getPayment')
->with($order)
->willReturn($payment)
;
self::assertFalse($this->provider->supports($requestConfiguration, $order));
}
private function createOrder(): MockObject&OrderInterface
{
$order = $this->createMock(OrderInterface::class);
$order->method('getId')->willReturn(1);
return $order;
}
/**
* @param array<string, mixed> $config
*/
private function createPaymentWithGatewayConfig(array $config): MockObject&PaymentInterface
{
$gatewayConfig = new GatewayConfig();
$gatewayConfig->setGatewayName('offline');
$gatewayConfig->setConfig($config);
return $this->createPayment($gatewayConfig);
}
private function createPayment(GatewayConfig $gatewayConfig): MockObject&PaymentInterface
{
$paymentMethod = $this->createMock(PaymentMethodInterface::class);
$paymentMethod->method('getGatewayConfig')->willReturn($gatewayConfig);
$payment = $this->createMock(PaymentInterface::class);
$payment->method('getMethod')->willReturn($paymentMethod);
return $payment;
}
}

View file

@ -95,7 +95,9 @@ final class Configuration implements ConfigurationInterface
->defaultValue('sylius_shop_order_after_pay')
->end()
->arrayNode('payum_after_pay_route_parameters')
->addDefaultsIfNotSet()
->defaultValue([])
->useAttributeAsKey('name')
->scalarPrototype()->end()
->end()
->scalarNode('final_route')
->defaultValue('sylius_shop_order_thank_you')

View file

@ -149,6 +149,55 @@ final class ConfigurationTest extends TestCase
]]);
}
#[Test]
public function it_has_default_configuration_for_order_pay_node(): void
{
$this->assertProcessedConfigurationEquals(
[[]],
['order_pay' => [
'payment_request_pay_route' => 'sylius_shop_payment_request_pay',
'payment_request_pay_route_parameters' => ['hash' => 'paymentRequest.getHash()'],
'after_pay_route' => 'sylius_shop_order_after_pay',
'after_pay_route_parameters' => ['hash' => 'paymentRequest.getHash()'],
'payum_after_pay_route' => 'sylius_shop_order_after_pay',
'payum_after_pay_route_parameters' => [],
'final_route' => 'sylius_shop_order_thank_you',
'final_route_parameters' => [],
'retry_route' => 'sylius_shop_order_show',
'retry_route_parameters' => ['tokenValue' => 'order.getTokenValue()'],
]],
'order_pay',
);
}
#[Test]
public function its_payum_after_pay_redirect_is_configurable(): void
{
$this->assertProcessedConfigurationEquals(
[[
'order_pay' => [
'payum_after_pay_route' => 'app_shop_order_after_pay',
'payum_after_pay_route_parameters' => ['tokenValue' => 'fixed-token-value'],
],
]],
[
'order_pay' => [
'payum_after_pay_route' => 'app_shop_order_after_pay',
'payum_after_pay_route_parameters' => ['tokenValue' => 'fixed-token-value'],
'payment_request_pay_route' => 'sylius_shop_payment_request_pay',
'payment_request_pay_route_parameters' => ['hash' => 'paymentRequest.getHash()'],
'after_pay_route' => 'sylius_shop_order_after_pay',
'after_pay_route_parameters' => ['hash' => 'paymentRequest.getHash()'],
'final_route' => 'sylius_shop_order_thank_you',
'final_route_parameters' => [],
'retry_route' => 'sylius_shop_order_show',
'retry_route_parameters' => ['tokenValue' => 'order.getTokenValue()'],
],
],
'order_pay',
);
}
protected function getConfiguration(): Configuration
{
return new Configuration();

View file

@ -116,6 +116,32 @@ final class SyliusShopExtensionTest extends AbstractExtensionTestCase
$this->assertContainerBuilderHasParameter('sylius_shop.firewall_context_name', 'myshopfirewall');
}
#[Test]
public function it_configures_default_order_pay_parameters(): void
{
$this->load([]);
$this->assertContainerBuilderHasParameter('sylius_shop.order_pay.payum_after_pay_route', 'sylius_shop_order_after_pay');
$this->assertContainerBuilderHasParameter('sylius_shop.order_pay.payum_after_pay_route_parameters', []);
}
#[Test]
public function it_configures_order_pay_parameters(): void
{
$this->load([
'order_pay' => [
'payum_after_pay_route' => 'app_shop_order_after_pay',
'payum_after_pay_route_parameters' => ['tokenValue' => 'fixed-token-value'],
],
]);
$this->assertContainerBuilderHasParameter('sylius_shop.order_pay.payum_after_pay_route', 'app_shop_order_after_pay');
$this->assertContainerBuilderHasParameter(
'sylius_shop.order_pay.payum_after_pay_route_parameters',
['tokenValue' => 'fixed-token-value'],
);
}
#[Test]
public function it_prepends_sylius_theme_configuration_with_channel_based_context(): void
{