Skip to content

Commit b146173

Browse files
PRE-3278: adding Unit testing for the plugin
1 parent a22903a commit b146173

13 files changed

Lines changed: 2790 additions & 3 deletions

.phpunit.result.cache

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

src/PaymentProcessing/AbortPaymentProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace PayPlug\SyliusPayPlugPlugin\PaymentProcessing;
66

77
use Payplug\Exception\HttpException;
8-
use PayPlug\SyliusPayPlugPlugin\ApiClient\PayPlugApiClientFactory;
8+
use PayPlug\SyliusPayPlugPlugin\ApiClient\PayPlugApiClientFactoryInterface;
99
use Sylius\Component\Core\Model\PaymentInterface;
1010
use Sylius\Component\Payment\PaymentTransitions;
1111
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
@@ -16,7 +16,7 @@
1616
class AbortPaymentProcessor
1717
{
1818
public function __construct(
19-
private PayPlugApiClientFactory $payplugApiClientFactory,
19+
private PayPlugApiClientFactoryInterface $payplugApiClientFactory,
2020
) {
2121
}
2222

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\PayPlug\SyliusPayPlugPlugin\PHPUnit\Action;
6+
7+
use ArrayObject;
8+
use PayPlug\SyliusPayPlugPlugin\Action\ConvertPaymentAction;
9+
use PayPlug\SyliusPayPlugPlugin\ApiClient\PayPlugApiClientInterface;
10+
use PayPlug\SyliusPayPlugPlugin\Creator\PayPlugPaymentDataCreator;
11+
use Payum\Core\Request\Convert;
12+
use PHPUnit\Framework\MockObject\MockObject;
13+
use PHPUnit\Framework\TestCase;
14+
use Sylius\Component\Core\Model\PaymentInterface;
15+
16+
final class ConvertPaymentActionTest extends TestCase
17+
{
18+
private PayPlugPaymentDataCreator&MockObject $paymentDataCreator;
19+
20+
private PayPlugApiClientInterface&MockObject $apiClient;
21+
22+
private ConvertPaymentAction $action;
23+
24+
protected function setUp(): void
25+
{
26+
$this->paymentDataCreator = $this->createMock(PayPlugPaymentDataCreator::class);
27+
$this->apiClient = $this->createMock(PayPlugApiClientInterface::class);
28+
29+
$this->action = new ConvertPaymentAction($this->paymentDataCreator);
30+
$this->action->setApi($this->apiClient);
31+
}
32+
33+
// -------------------------------------------------------------------------
34+
// supports()
35+
// -------------------------------------------------------------------------
36+
37+
/**
38+
* Passes a Convert request with a PaymentInterface source and 'array' as the target format.
39+
* Verifies supports() returns true.
40+
*/
41+
public function testSupports_withConvertRequestAndPaymentSource_returnsTrue(): void
42+
{
43+
$request = $this->createMock(Convert::class);
44+
$request->method('getSource')->willReturn($this->createMock(PaymentInterface::class));
45+
$request->method('getTo')->willReturn('array');
46+
47+
self::assertTrue($this->action->supports($request));
48+
}
49+
50+
/**
51+
* Source is a plain stdClass instead of a PaymentInterface.
52+
* Verifies supports() returns false.
53+
*/
54+
public function testSupports_withConvertRequestButNonPaymentSource_returnsFalse(): void
55+
{
56+
$request = $this->createMock(Convert::class);
57+
$request->method('getSource')->willReturn(new \stdClass());
58+
$request->method('getTo')->willReturn('array');
59+
60+
self::assertFalse($this->action->supports($request));
61+
}
62+
63+
/**
64+
* Source is a valid PaymentInterface but the target format is 'json' instead of 'array'.
65+
* Verifies supports() returns false.
66+
*/
67+
public function testSupports_withConvertRequestButWrongTo_returnsFalse(): void
68+
{
69+
$request = $this->createMock(Convert::class);
70+
$request->method('getSource')->willReturn($this->createMock(PaymentInterface::class));
71+
$request->method('getTo')->willReturn('json');
72+
73+
self::assertFalse($this->action->supports($request));
74+
}
75+
76+
// -------------------------------------------------------------------------
77+
// execute() — delegates to creator and sets result
78+
// -------------------------------------------------------------------------
79+
80+
/**
81+
* Calls execute() and verifies the action delegates to PayPlugPaymentDataCreator::create() exactly once.
82+
* The ArrayObject returned by the creator is converted to an array and passed to setResult().
83+
*/
84+
public function testExecute_delegatesToCreatorAndSetsResult(): void
85+
{
86+
$payment = $this->createMock(PaymentInterface::class);
87+
88+
$createdDetails = new ArrayObject([
89+
'amount' => 1500,
90+
'currency' => 'EUR',
91+
]);
92+
93+
$this->paymentDataCreator
94+
->expects(self::once())
95+
->method('create')
96+
->with($payment)
97+
->willReturn($createdDetails)
98+
;
99+
100+
$request = $this->createMock(Convert::class);
101+
$request->method('getSource')->willReturn($payment);
102+
$request->method('getTo')->willReturn('array');
103+
$request->expects(self::once())
104+
->method('setResult')
105+
->with(['amount' => 1500, 'currency' => 'EUR'])
106+
;
107+
108+
$this->action->execute($request);
109+
}
110+
111+
// -------------------------------------------------------------------------
112+
// execute() — result contains all fields from creator
113+
// -------------------------------------------------------------------------
114+
115+
/**
116+
* The creator returns an ArrayObject with multiple nested fields (amount, payment_method, metadata).
117+
* Verifies all fields are present and correctly typed in the array passed to setResult().
118+
*/
119+
public function testExecute_passesAllCreatorFieldsToResult(): void
120+
{
121+
$payment = $this->createMock(PaymentInterface::class);
122+
123+
$createdDetails = new ArrayObject([
124+
'amount' => 2000,
125+
'currency' => 'EUR',
126+
'payment_method' => 'bancontact',
127+
'metadata' => ['customer_id' => 42, 'order_number' => 'ORD-99'],
128+
]);
129+
130+
$this->paymentDataCreator->method('create')->willReturn($createdDetails);
131+
132+
$capturedResult = null;
133+
$request = $this->createMock(Convert::class);
134+
$request->method('getSource')->willReturn($payment);
135+
$request->method('getTo')->willReturn('array');
136+
$request->method('setResult')->willReturnCallback(function (array $result) use (&$capturedResult) {
137+
$capturedResult = $result;
138+
});
139+
140+
$this->action->execute($request);
141+
142+
self::assertSame(2000, $capturedResult['amount']);
143+
self::assertSame('bancontact', $capturedResult['payment_method']);
144+
self::assertSame(42, $capturedResult['metadata']['customer_id']);
145+
}
146+
}

0 commit comments

Comments
 (0)