Skip to content

Commit f13bb1a

Browse files
[Maintenance] Update provider to only return enabled PayPal method
1 parent 189c026 commit f13bb1a

File tree

3 files changed

+186
-2
lines changed

3 files changed

+186
-2
lines changed

src/Provider/PayPalPaymentMethodProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(private PaymentMethodRepositoryInterface $paymentMet
2828

2929
public function provide(): PaymentMethodInterface
3030
{
31-
$paymentMethods = $this->paymentMethodRepository->findAll();
31+
$paymentMethods = $this->paymentMethodRepository->findBy(['enabled' => true]);
3232

3333
/** @var PaymentMethodInterface $paymentMethod */
3434
foreach ($paymentMethods as $paymentMethod) {

tests/Behat/Context/Admin/ManagingPaymentMethodsContext.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use Behat\Behat\Context\Context;
1717
use Behat\Mink\Exception\ElementNotFoundException;
18-
use Sylius\Behat\Exception\NotificationExpectationMismatchException;
1918
use Sylius\Behat\NotificationType;
2019
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
2120
use Sylius\Behat\Page\Admin\PaymentMethod\CreatePageInterface;
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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\Provider;
15+
16+
use PHPUnit\Framework\Attributes\Test;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use PHPUnit\Framework\TestCase;
19+
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
20+
use Sylius\Component\Core\Model\PaymentMethodInterface;
21+
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
22+
use Sylius\PayPalPlugin\Exception\PayPalPaymentMethodNotFoundException;
23+
use Sylius\PayPalPlugin\Provider\PayPalPaymentMethodProvider;
24+
25+
final class PayPalPaymentMethodProviderTest extends TestCase
26+
{
27+
/** @var PaymentMethodRepositoryInterface<PaymentMethodInterface>&MockObject */
28+
private PaymentMethodRepositoryInterface&MockObject $paymentMethodRepository;
29+
30+
private PayPalPaymentMethodProvider $payPalPaymentMethodProvider;
31+
32+
protected function setUp(): void
33+
{
34+
parent::setUp();
35+
36+
$this->paymentMethodRepository = $this->createMock(PaymentMethodRepositoryInterface::class);
37+
$this->payPalPaymentMethodProvider = new PayPalPaymentMethodProvider($this->paymentMethodRepository);
38+
}
39+
40+
#[Test]
41+
public function it_provides_pay_pal_payment_method(): void
42+
{
43+
$payPalPaymentMethod = $this->createMock(PaymentMethodInterface::class);
44+
$payPalGatewayConfig = $this->createMock(GatewayConfigInterface::class);
45+
46+
$this->paymentMethodRepository
47+
->method('findBy')
48+
->with(['enabled' => true])
49+
->willReturn([$payPalPaymentMethod])
50+
;
51+
52+
$payPalPaymentMethod
53+
->method('getGatewayConfig')
54+
->willReturn($payPalGatewayConfig)
55+
;
56+
57+
$payPalGatewayConfig
58+
->method('getFactoryName')
59+
->willReturn('sylius_paypal')
60+
;
61+
62+
$result = $this->payPalPaymentMethodProvider->provide();
63+
64+
self::assertSame($payPalPaymentMethod, $result);
65+
}
66+
67+
#[Test]
68+
public function it_provides_first_pay_pal_payment_method_when_multiple_exist(): void
69+
{
70+
$firstPayPalPaymentMethod = $this->createMock(PaymentMethodInterface::class);
71+
$secondPayPalPaymentMethod = $this->createMock(PaymentMethodInterface::class);
72+
$firstPayPalGatewayConfig = $this->createMock(GatewayConfigInterface::class);
73+
$secondPayPalGatewayConfig = $this->createMock(GatewayConfigInterface::class);
74+
75+
$this->paymentMethodRepository
76+
->method('findBy')
77+
->with(['enabled' => true])
78+
->willReturn([$firstPayPalPaymentMethod, $secondPayPalPaymentMethod])
79+
;
80+
81+
$firstPayPalPaymentMethod
82+
->method('getGatewayConfig')
83+
->willReturn($firstPayPalGatewayConfig)
84+
;
85+
86+
$firstPayPalGatewayConfig
87+
->method('getFactoryName')
88+
->willReturn('sylius_paypal')
89+
;
90+
91+
$secondPayPalPaymentMethod
92+
->method('getGatewayConfig')
93+
->willReturn($secondPayPalGatewayConfig)
94+
;
95+
96+
$secondPayPalGatewayConfig
97+
->method('getFactoryName')
98+
->willReturn('sylius_paypal')
99+
;
100+
101+
$result = $this->payPalPaymentMethodProvider->provide();
102+
103+
self::assertSame($firstPayPalPaymentMethod, $result);
104+
}
105+
106+
#[Test]
107+
public function it_provides_pay_pal_payment_method_when_other_payment_methods_exist(): void
108+
{
109+
$otherPaymentMethod = $this->createMock(PaymentMethodInterface::class);
110+
$payPalPaymentMethod = $this->createMock(PaymentMethodInterface::class);
111+
$otherGatewayConfig = $this->createMock(GatewayConfigInterface::class);
112+
$payPalGatewayConfig = $this->createMock(GatewayConfigInterface::class);
113+
114+
$this->paymentMethodRepository
115+
->method('findBy')
116+
->with(['enabled' => true])
117+
->willReturn([$otherPaymentMethod, $payPalPaymentMethod])
118+
;
119+
120+
$otherPaymentMethod
121+
->method('getGatewayConfig')
122+
->willReturn($otherGatewayConfig)
123+
;
124+
125+
$otherGatewayConfig
126+
->method('getFactoryName')
127+
->willReturn('other')
128+
;
129+
130+
$payPalPaymentMethod
131+
->method('getGatewayConfig')
132+
->willReturn($payPalGatewayConfig)
133+
;
134+
135+
$payPalGatewayConfig
136+
->method('getFactoryName')
137+
->willReturn('sylius_paypal')
138+
;
139+
140+
$result = $this->payPalPaymentMethodProvider->provide();
141+
142+
self::assertSame($payPalPaymentMethod, $result);
143+
}
144+
145+
#[Test]
146+
public function it_throws_exception_when_no_enabled_payment_methods_exist(): void
147+
{
148+
$this->paymentMethodRepository
149+
->method('findBy')
150+
->with(['enabled' => true])
151+
->willReturn([])
152+
;
153+
154+
$this->expectException(PayPalPaymentMethodNotFoundException::class);
155+
156+
$this->payPalPaymentMethodProvider->provide();
157+
}
158+
159+
#[Test]
160+
public function it_throws_exception_when_no_pay_pal_payment_method_exists(): void
161+
{
162+
$otherPaymentMethod = $this->createMock(PaymentMethodInterface::class);
163+
$otherGatewayConfig = $this->createMock(GatewayConfigInterface::class);
164+
165+
$this->paymentMethodRepository
166+
->method('findBy')
167+
->with(['enabled' => true])
168+
->willReturn([$otherPaymentMethod])
169+
;
170+
171+
$otherPaymentMethod
172+
->method('getGatewayConfig')
173+
->willReturn($otherGatewayConfig)
174+
;
175+
176+
$otherGatewayConfig
177+
->method('getFactoryName')
178+
->willReturn('other')
179+
;
180+
181+
$this->expectException(PayPalPaymentMethodNotFoundException::class);
182+
183+
$this->payPalPaymentMethodProvider->provide();
184+
}
185+
}

0 commit comments

Comments
 (0)