Skip to content

Commit c23ec18

Browse files
author
Can Demiralp
committed
[ECP-9489] Write unit tests
1 parent dbfe659 commit c23ec18

File tree

1 file changed

+248
-0
lines changed

1 file changed

+248
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
<?php
2+
/**
3+
*
4+
* Adyen Payment module (https://www.adyen.com/)
5+
*
6+
* Copyright (c) 2025 Adyen N.V. (https://www.adyen.com/)
7+
* See LICENSE.txt for license details.
8+
*
9+
* Author: Adyen <magento@adyen.com>
10+
*/
11+
12+
namespace Adyen\Payment\Test\Helper\Unit\Model;
13+
14+
use Adyen\AdyenException;
15+
use Adyen\Payment\Api\Data\InvoiceInterface;
16+
use Adyen\Payment\Api\Data\NotificationInterface;
17+
use Adyen\Payment\Model\AdyenInvoiceRepository;
18+
use Adyen\Payment\Model\Invoice;
19+
use Adyen\Payment\Model\ResourceModel\Creditmemo\Collection as CreditmemoCollection;
20+
use Adyen\Payment\Model\ResourceModel\Invoice\Collection as InvoiceCollection;
21+
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
22+
use Magento\Framework\Api\Search\SearchResultInterface;
23+
use Magento\Framework\Api\SearchCriteriaInterface;
24+
use Magento\Framework\Api\SearchResultsInterface;
25+
use Magento\Framework\Exception\AlreadyExistsException;
26+
use Magento\Framework\Exception\LocalizedException;
27+
use PHPUnit\Framework\MockObject\MockObject;
28+
use Adyen\Payment\Model\ResourceModel\Invoice\CollectionFactory;
29+
use Adyen\Payment\Model\ResourceModel\Invoice\Invoice as InvoiceResourceModel;
30+
use Adyen\Payment\Model\InvoiceFactory;
31+
use Magento\Framework\Api\Search\SearchResultFactory;
32+
use Magento\Framework\Api\SearchCriteria\CollectionProcessor;
33+
use Magento\Framework\Api\SearchCriteriaBuilder;
34+
35+
class AdyenInvoiceRepositoryTest extends AbstractAdyenTestCase
36+
{
37+
private ?AdyenInvoiceRepository $adyenInvoiceRepository;
38+
private SearchResultFactory|MockObject $searchResultsFactoryMock;
39+
private CollectionFactory|MockObject $collectionFactoryMock;
40+
private CollectionProcessor|MockObject $collectionProcessorMock;
41+
private InvoiceResourceModel|MockObject $resourceModelMock;
42+
private InvoiceFactory|MockObject $adyenInvoiceFactoryMock;
43+
private SearchCriteriaBuilder|MockObject $searchCriteriaBuilderMock;
44+
45+
/**
46+
* @return void
47+
*/
48+
protected function setUp(): void
49+
{
50+
$this->searchResultsFactoryMock = $this->createMock(SearchResultFactory::class);
51+
$this->collectionFactoryMock = $this->createGeneratedMock(CollectionFactory::class, [
52+
'create'
53+
]);
54+
$this->collectionProcessorMock = $this->createMock(CollectionProcessor::class);
55+
$this->resourceModelMock = $this->createMock(InvoiceResourceModel::class);
56+
$this->adyenInvoiceFactoryMock = $this->createGeneratedMock(InvoiceFactory::class, [
57+
'create'
58+
]);
59+
$this->searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class);
60+
61+
$this->adyenInvoiceRepository = new AdyenInvoiceRepository(
62+
$this->searchResultsFactoryMock,
63+
$this->collectionFactoryMock,
64+
$this->collectionProcessorMock,
65+
$this->resourceModelMock,
66+
$this->adyenInvoiceFactoryMock,
67+
$this->searchCriteriaBuilderMock
68+
);
69+
}
70+
71+
/**
72+
* @return void
73+
*/
74+
protected function tearDown(): void
75+
{
76+
$this->adyenInvoiceRepository = null;
77+
}
78+
79+
/**
80+
* @return void
81+
*/
82+
public function testGetList()
83+
{
84+
$searchResultMock = $this->createPartialMock(SearchResultInterface::class, []);
85+
$this->searchResultsFactoryMock->expects($this->once())
86+
->method('create')
87+
->willReturn($searchResultMock);
88+
89+
$collectionMock = $this->createMock(InvoiceCollection::class);
90+
$collectionResult[] = $this->createMock(Invoice::class);
91+
$collectionMock->method('getItems')->willReturn($collectionResult);
92+
$collectionMock->method('getSize')->willReturn(count($collectionResult));
93+
$this->collectionFactoryMock->expects($this->once())
94+
->method('create')
95+
->willReturn($collectionMock);
96+
97+
$searchCriteriaMock = $this->createMock(SearchCriteriaInterface::class);
98+
99+
$this->collectionProcessorMock->expects($this->once())
100+
->method('process')
101+
->with($searchCriteriaMock, $collectionMock);
102+
103+
$searchResultMock->expects($this->once())
104+
->method('setItems')
105+
->with($collectionResult);
106+
$searchResultMock->expects($this->once())
107+
->method('setTotalCount')
108+
->with(count($collectionResult));
109+
110+
$result = $this->adyenInvoiceRepository->getList($searchCriteriaMock);
111+
112+
$this->assertInstanceOf(SearchResultsInterface::class, $result);
113+
}
114+
115+
/**
116+
* @return void
117+
* @throws AlreadyExistsException
118+
*/
119+
public function testSave()
120+
{
121+
$invoiceMock = $this->createMock(Invoice::class);
122+
123+
$this->resourceModelMock->expects($this->once())
124+
->method('save')
125+
->with($invoiceMock)
126+
->willReturnSelf();
127+
128+
$result = $this->adyenInvoiceRepository->save($invoiceMock);
129+
$this->assertInstanceOf(InvoiceInterface::class, $result);
130+
}
131+
132+
/**
133+
* @return void
134+
*/
135+
public function testGetByAdyenOrderPaymentId()
136+
{
137+
$adyenOrderPaymentId = 1;
138+
139+
$searchCriteriaMock = $this->createMock(SearchCriteriaInterface::class);
140+
$this->searchCriteriaBuilderMock->expects($this->once())
141+
->method('addFilter')
142+
->with(InvoiceInterface::ADYEN_ORDER_PAYMENT_ID, $adyenOrderPaymentId)
143+
->willReturnSelf();
144+
145+
$this->searchCriteriaBuilderMock->expects($this->once())
146+
->method('create')
147+
->willReturn($searchCriteriaMock);
148+
149+
$collectionResult[] = $this->createMock(Invoice::class);
150+
151+
$searchResultMock = $this->createMock(SearchResultInterface::class);
152+
$searchResultMock->expects($this->once())
153+
->method('getItems')
154+
->willReturn($collectionResult);
155+
156+
$this->searchResultsFactoryMock->expects($this->once())
157+
->method('create')
158+
->willReturn($searchResultMock);
159+
160+
$collectionMock = $this->createMock(CreditmemoCollection::class);
161+
$this->collectionFactoryMock->expects($this->once())
162+
->method('create')
163+
->willReturn($collectionMock);
164+
165+
$result = $this->adyenInvoiceRepository->getByAdyenOrderPaymentId($adyenOrderPaymentId);
166+
$this->assertIsArray($result);
167+
}
168+
169+
/**
170+
* @return array[]
171+
*/
172+
private static function webhookTestDataProvider(): array
173+
{
174+
return [
175+
[
176+
'eventCode' => 'CAPTURE',
177+
'isExpectedType' => true,
178+
'invoiceId' => "1",
179+
'isResultValid' => true
180+
],
181+
[
182+
'eventCode' => 'REFUND',
183+
'isExpectedType' => false,
184+
'invoiceId' => "1",
185+
'isResultValid' => true
186+
],
187+
[
188+
'eventCode' => 'CAPTURE',
189+
'isExpectedType' => true,
190+
'invoiceId' => "",
191+
'isResultValid' => false
192+
]
193+
];
194+
}
195+
196+
/**
197+
* @dataProvider webhookTestDataProvider
198+
*
199+
* @param $eventCode
200+
* @param $isExpectedType
201+
* @param $invoiceId
202+
* @param $isResultValid
203+
* @return void
204+
* @throws AdyenException
205+
* @throws LocalizedException
206+
*/
207+
public function testGetByRefundWebhook($eventCode, $isExpectedType, $invoiceId, $isResultValid)
208+
{
209+
$notificationPspreference = 'xyz_12345';
210+
211+
$notification = $this->createMock(NotificationInterface::class);
212+
$notification->method('getEventCode')->willReturn($eventCode);
213+
$notification->method('getPspreference')->willReturn($notificationPspreference);
214+
215+
if (!$isExpectedType) {
216+
$this->expectException(AdyenException::class);
217+
218+
// No result required, assert exception
219+
$this->adyenInvoiceRepository->getByCaptureWebhook($notification);
220+
} else {
221+
$this->resourceModelMock->expects($this->once())
222+
->method('getIdByPspreference')
223+
->with($notificationPspreference)
224+
->willReturn($invoiceId);
225+
226+
if ($isResultValid) {
227+
$invoiceMock = $this->createMock(Invoice::class);
228+
$this->adyenInvoiceFactoryMock->expects($this->once())
229+
->method('create')
230+
->willReturn($invoiceMock);
231+
232+
$this->resourceModelMock->expects($this->once())
233+
->method('load')
234+
->with($invoiceMock, $invoiceId, InvoiceInterface::ENTITY_ID)
235+
->willReturnSelf();
236+
237+
$result = $this->adyenInvoiceRepository->getByCaptureWebhook($notification);
238+
$this->assertInstanceOf(InvoiceInterface::class, $result);
239+
} else {
240+
$this->assertNull($this->adyenInvoiceRepository->getByCaptureWebhook($notification));
241+
}
242+
}
243+
}
244+
}
245+
246+
247+
248+

0 commit comments

Comments
 (0)