Skip to content

Commit 368f9f5

Browse files
committed
test: add tests for bundle config
1 parent 95c0bb0 commit 368f9f5

5 files changed

Lines changed: 264 additions & 2 deletions

File tree

src/Sylius/Bundle/PayumBundle/OrderPay/Provider/PayumPayResponseProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
/** @experimental */
3030
final class PayumPayResponseProvider implements PayResponseProviderInterface
3131
{
32-
3332
/**
3433
* @param array<string, string> $afterPayUrlParameters
3534
*/
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
}

src/Sylius/Bundle/ShopBundle/DependencyInjection/Configuration.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ public function getConfigTreeBuilder(): TreeBuilder
9595
->defaultValue('sylius_shop_order_after_pay')
9696
->end()
9797
->arrayNode('payum_after_pay_route_parameters')
98-
->addDefaultsIfNotSet()
98+
->defaultValue([])
99+
->useAttributeAsKey('name')
100+
->scalarPrototype()->end()
99101
->end()
100102
->scalarNode('final_route')
101103
->defaultValue('sylius_shop_order_thank_you')

src/Sylius/Bundle/ShopBundle/tests/DependencyInjection/ConfigurationTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,55 @@ public function its_checkout_route_map_route_cannot_be_empty(): void
149149
]]);
150150
}
151151

152+
#[Test]
153+
public function it_has_default_configuration_for_order_pay_node(): void
154+
{
155+
$this->assertProcessedConfigurationEquals(
156+
[[]],
157+
['order_pay' => [
158+
'payment_request_pay_route' => 'sylius_shop_payment_request_pay',
159+
'payment_request_pay_route_parameters' => ['hash' => 'paymentRequest.getHash()'],
160+
'after_pay_route' => 'sylius_shop_order_after_pay',
161+
'after_pay_route_parameters' => ['hash' => 'paymentRequest.getHash()'],
162+
'payum_after_pay_route' => 'sylius_shop_order_after_pay',
163+
'payum_after_pay_route_parameters' => [],
164+
'final_route' => 'sylius_shop_order_thank_you',
165+
'final_route_parameters' => [],
166+
'retry_route' => 'sylius_shop_order_show',
167+
'retry_route_parameters' => ['tokenValue' => 'order.getTokenValue()'],
168+
]],
169+
'order_pay',
170+
);
171+
}
172+
173+
#[Test]
174+
public function its_payum_after_pay_redirect_is_configurable(): void
175+
{
176+
$this->assertProcessedConfigurationEquals(
177+
[[
178+
'order_pay' => [
179+
'payum_after_pay_route' => 'app_shop_order_after_pay',
180+
'payum_after_pay_route_parameters' => ['tokenValue' => 'fixed-token-value'],
181+
],
182+
]],
183+
[
184+
'order_pay' => [
185+
'payum_after_pay_route' => 'app_shop_order_after_pay',
186+
'payum_after_pay_route_parameters' => ['tokenValue' => 'fixed-token-value'],
187+
'payment_request_pay_route' => 'sylius_shop_payment_request_pay',
188+
'payment_request_pay_route_parameters' => ['hash' => 'paymentRequest.getHash()'],
189+
'after_pay_route' => 'sylius_shop_order_after_pay',
190+
'after_pay_route_parameters' => ['hash' => 'paymentRequest.getHash()'],
191+
'final_route' => 'sylius_shop_order_thank_you',
192+
'final_route_parameters' => [],
193+
'retry_route' => 'sylius_shop_order_show',
194+
'retry_route_parameters' => ['tokenValue' => 'order.getTokenValue()'],
195+
],
196+
],
197+
'order_pay',
198+
);
199+
}
200+
152201
protected function getConfiguration(): Configuration
153202
{
154203
return new Configuration();

src/Sylius/Bundle/ShopBundle/tests/DependencyInjection/SyliusShopExtensionTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,32 @@ public function it_configures_firewall_context_parameter_for_user_registration_l
116116
$this->assertContainerBuilderHasParameter('sylius_shop.firewall_context_name', 'myshopfirewall');
117117
}
118118

119+
#[Test]
120+
public function it_configures_default_order_pay_parameters(): void
121+
{
122+
$this->load([]);
123+
124+
$this->assertContainerBuilderHasParameter('sylius_shop.order_pay.payum_after_pay_route', 'sylius_shop_order_after_pay');
125+
$this->assertContainerBuilderHasParameter('sylius_shop.order_pay.payum_after_pay_route_parameters', []);
126+
}
127+
128+
#[Test]
129+
public function it_configures_order_pay_parameters(): void
130+
{
131+
$this->load([
132+
'order_pay' => [
133+
'payum_after_pay_route' => 'app_shop_order_after_pay',
134+
'payum_after_pay_route_parameters' => ['tokenValue' => 'fixed-token-value'],
135+
],
136+
]);
137+
138+
$this->assertContainerBuilderHasParameter('sylius_shop.order_pay.payum_after_pay_route', 'app_shop_order_after_pay');
139+
$this->assertContainerBuilderHasParameter(
140+
'sylius_shop.order_pay.payum_after_pay_route_parameters',
141+
['tokenValue' => 'fixed-token-value'],
142+
);
143+
}
144+
119145
#[Test]
120146
public function it_prepends_sylius_theme_configuration_with_channel_based_context(): void
121147
{

0 commit comments

Comments
 (0)