-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Can Demiralp
committed
Jan 7, 2025
1 parent
423770e
commit b62a1e8
Showing
4 changed files
with
391 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
/** | ||
* | ||
* Adyen Payment module (https://www.adyen.com/) | ||
* | ||
* Copyright (c) 2025 Adyen N.V. (https://www.adyen.com/) | ||
* See LICENSE.txt for license details. | ||
* | ||
* Author: Adyen <[email protected]> | ||
*/ | ||
|
||
namespace Adyen\Payment\Test\Cron; | ||
|
||
use Adyen\Payment\Api\Repository\AdyenNotificationRepositoryInterface; | ||
use Adyen\Payment\Cron\CleanupNotifications; | ||
use Adyen\Payment\Cron\Providers\NotificationsProviderInterface; | ||
use Adyen\Payment\Helper\Config; | ||
use Adyen\Payment\Logger\AdyenLogger; | ||
use Adyen\Payment\Model\Notification; | ||
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase; | ||
use Magento\Store\Api\Data\StoreInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
|
||
class CleanupNotificationsTest extends AbstractAdyenTestCase | ||
{ | ||
protected ?CleanupNotifications $cleanupNotifications; | ||
protected AdyenLogger|MockObject $adyenLoggerMock; | ||
protected Config|MockObject $configHelperMock; | ||
protected StoreManagerInterface|MockObject $storeManagerMock; | ||
protected AdyenNotificationRepositoryInterface|MockObject $adyenNotificationRepositoryMock; | ||
protected NotificationsProviderInterface|MockObject $notificationsProvider; | ||
protected array $providers; | ||
|
||
const STORE_ID = PHP_INT_MAX; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->adyenLoggerMock = $this->createMock(AdyenLogger::class); | ||
$this->configHelperMock = $this->createMock(Config::class); | ||
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class); | ||
$this->adyenNotificationRepositoryMock = | ||
$this->createMock(AdyenNotificationRepositoryInterface::class); | ||
$this->notificationsProvider = $this->createMock(NotificationsProviderInterface::class); | ||
|
||
$this->providers[] = $this->notificationsProvider; | ||
|
||
$storeMock = $this->createMock(StoreInterface::class); | ||
$storeMock->method('getId')->willReturn(self::STORE_ID); | ||
$this->storeManagerMock->method('getStore')->willReturn($storeMock); | ||
|
||
$this->cleanupNotifications = new CleanupNotifications( | ||
$this->providers, | ||
$this->adyenLoggerMock, | ||
$this->configHelperMock, | ||
$this->storeManagerMock, | ||
$this->adyenNotificationRepositoryMock | ||
); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$this->cleanupNotifications = null; | ||
} | ||
|
||
public function testExecuteConfigEnabled() | ||
{ | ||
$this->configHelperMock->expects($this->once()) | ||
->method('getIsWebhookCleanupEnabled') | ||
->with(self::STORE_ID) | ||
->willReturn(true); | ||
|
||
$notificationMock = $this->createMock(Notification::class); | ||
$providerResult[] = $notificationMock; | ||
|
||
$this->notificationsProvider->method('provide')->willReturn($providerResult); | ||
|
||
$this->adyenNotificationRepositoryMock->expects($this->once()) | ||
->method('delete') | ||
->with($notificationMock) | ||
->willReturn(true); | ||
|
||
$this->adyenLoggerMock->expects($this->once())->method('addAdyenDebug'); | ||
|
||
$this->cleanupNotifications->execute(); | ||
} | ||
|
||
public function testExecuteConfigDisabled() | ||
{ | ||
$this->configHelperMock->expects($this->once()) | ||
->method('getIsWebhookCleanupEnabled') | ||
->with(self::STORE_ID) | ||
->willReturn(false); | ||
|
||
$this->notificationsProvider->expects($this->never())->method('provide'); | ||
$this->adyenNotificationRepositoryMock->expects($this->never())->method('delete'); | ||
$this->adyenLoggerMock->expects($this->once())->method('addAdyenDebug'); | ||
|
||
$this->cleanupNotifications->execute(); | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
Test/Unit/Cron/Providers/ProcessedOldNotificationsProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?php | ||
/** | ||
* | ||
* Adyen Payment module (https://www.adyen.com/) | ||
* | ||
* Copyright (c) 2025 Adyen N.V. (https://www.adyen.com/) | ||
* See LICENSE.txt for license details. | ||
* | ||
* Author: Adyen <[email protected]> | ||
*/ | ||
|
||
namespace Adyen\Payment\Test\Cron\Providers; | ||
|
||
use Adyen\Payment\Api\Repository\AdyenNotificationRepositoryInterface; | ||
use Adyen\Payment\Cron\Providers\ProcessedOldNotificationsProvider; | ||
use Adyen\Payment\Helper\Config; | ||
use Adyen\Payment\Logger\AdyenLogger; | ||
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase; | ||
use Magento\Framework\Api\SearchCriteria; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Framework\Api\SearchResultsInterface; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Store\Api\Data\StoreInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
|
||
class ProcessedOldNotificationsProviderTest extends AbstractAdyenTestCase | ||
{ | ||
protected ?ProcessedOldNotificationsProvider $notificationsProvider; | ||
protected AdyenNotificationRepositoryInterface|MockObject $adyenNotificationRepositoryMock; | ||
protected SearchCriteriaBuilder|MockObject $searchCriteriaBuilderMock; | ||
protected Config|MockObject $configHelperMock; | ||
protected StoreManagerInterface|MockObject $storeManagerMock; | ||
protected AdyenLogger|MockObject $adyenLoggerMock; | ||
|
||
const STORE_ID = PHP_INT_MAX; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->adyenNotificationRepositoryMock = | ||
$this->createMock(AdyenNotificationRepositoryInterface::class); | ||
$this->searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class); | ||
$this->configHelperMock = $this->createMock(Config::class); | ||
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class); | ||
$this->adyenLoggerMock = $this->createMock(AdyenLogger::class); | ||
|
||
$storeMock = $this->createMock(StoreInterface::class); | ||
$storeMock->method('getId')->willReturn(self::STORE_ID); | ||
$this->storeManagerMock->method('getStore')->willReturn($storeMock); | ||
|
||
$this->notificationsProvider = new ProcessedOldNotificationsProvider( | ||
$this->adyenNotificationRepositoryMock, | ||
$this->searchCriteriaBuilderMock, | ||
$this->configHelperMock, | ||
$this->storeManagerMock, | ||
$this->adyenLoggerMock | ||
); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$this->notificationsProvider = null; | ||
} | ||
|
||
public function testProvideSuccess() | ||
{ | ||
$expiryDays = 90; | ||
|
||
$this->configHelperMock->expects($this->once()) | ||
->method('getRequiredDaysForOldWebhooks') | ||
->with(self::STORE_ID) | ||
->willReturn($expiryDays); | ||
|
||
$dateMock = date('Y-m-d H:i:s', time() - $expiryDays * 24 * 60 * 60); | ||
|
||
$this->searchCriteriaBuilderMock->expects($this->exactly(3)) | ||
->method('addFilter') | ||
->withConsecutive( | ||
['done', 1, 'eq'], | ||
['processing', 0, 'eq'], | ||
['created_at', $dateMock, 'lteq'] | ||
) | ||
->willReturnSelf(); | ||
|
||
$searchCriteriaMock = $this->createMock(SearchCriteria::class); | ||
|
||
$this->searchCriteriaBuilderMock->expects($this->once()) | ||
->method('create') | ||
->willReturn($searchCriteriaMock); | ||
|
||
$searchResultsMock = $this->createMock(SearchResultsInterface::class); | ||
$searchResultsMock->expects($this->once()) | ||
->method('getItems') | ||
->willReturn([]); | ||
|
||
$this->adyenNotificationRepositoryMock->expects($this->once()) | ||
->method('getList') | ||
->with($searchCriteriaMock) | ||
->willReturn($searchResultsMock); | ||
|
||
$this->notificationsProvider->provide(); | ||
} | ||
|
||
public function testProvideFailure() | ||
{ | ||
$expiryDays = 90; | ||
|
||
$this->configHelperMock->expects($this->once()) | ||
->method('getRequiredDaysForOldWebhooks') | ||
->with(self::STORE_ID) | ||
->willReturn($expiryDays); | ||
|
||
$dateMock = date('Y-m-d H:i:s', time() - $expiryDays * 24 * 60 * 60); | ||
|
||
$this->searchCriteriaBuilderMock->expects($this->exactly(3)) | ||
->method('addFilter') | ||
->withConsecutive( | ||
['done', 1, 'eq'], | ||
['processing', 0, 'eq'], | ||
['created_at', $dateMock, 'lteq'] | ||
) | ||
->willReturnSelf(); | ||
|
||
$searchCriteriaMock = $this->createMock(SearchCriteria::class); | ||
|
||
$this->searchCriteriaBuilderMock->expects($this->once()) | ||
->method('create') | ||
->willReturn($searchCriteriaMock); | ||
|
||
$this->adyenNotificationRepositoryMock->expects($this->once()) | ||
->method('getList') | ||
->willThrowException(new LocalizedException(__('mock error message'))); | ||
|
||
$this->adyenLoggerMock->expects($this->once())->method('error'); | ||
|
||
$result = $this->notificationsProvider->provide(); | ||
$this->assertEmpty($result); | ||
} | ||
|
||
public function testGetProviderName() | ||
{ | ||
$this->assertEquals( | ||
'Adyen processed old webhook notifications', | ||
$this->notificationsProvider->getProviderName() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
/** | ||
* | ||
* Adyen Payment module (https://www.adyen.com/) | ||
* | ||
* Copyright (c) 2025 Adyen N.V. (https://www.adyen.com/) | ||
* See LICENSE.txt for license details. | ||
* | ||
* Author: Adyen <[email protected]> | ||
*/ | ||
|
||
namespace Adyen\Payment\Test\Helper\Unit\Model; | ||
|
||
use Adyen\Payment\Model\AdyenNotificationRepository; | ||
use Adyen\Payment\Model\Notification as NotificationEntity; | ||
use Adyen\Payment\Model\ResourceModel\Notification; | ||
use Adyen\Payment\Model\ResourceModel\Notification\Collection; | ||
use Adyen\Payment\Model\ResourceModel\Notification\CollectionFactory; | ||
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase; | ||
use Magento\Framework\Api\Search\SearchResultFactory; | ||
use Magento\Framework\Api\Search\SearchResultInterface; | ||
use Magento\Framework\Api\SearchCriteria\CollectionProcessor; | ||
use Magento\Framework\Api\SearchCriteriaInterface; | ||
use Magento\Framework\Api\SearchResultsInterface; | ||
use Magento\Framework\ObjectManagerInterface; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
|
||
class AdyenNotificationRepositoryTest extends AbstractAdyenTestCase | ||
{ | ||
protected ?AdyenNotificationRepository $adyenNotificationRepository; | ||
protected SearchResultFactory|MockObject $searchResultFactoryMock; | ||
protected CollectionFactory|MockObject $collectionFactoryMock; | ||
protected CollectionProcessor|MockObject $collectionProcessorMock; | ||
protected ObjectManagerInterface|MockObject $objectManagerMock; | ||
|
||
const RESOURCE_MODEL = 'Adyen\Payment\Model\ResourceModel\Notification'; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->searchResultFactoryMock = $this->createMock(SearchResultFactory::class); | ||
$this->collectionFactoryMock = $this->createGeneratedMock( | ||
CollectionFactory::class, | ||
['create'] | ||
); | ||
$this->collectionProcessorMock = $this->createMock(CollectionProcessor::class); | ||
$this->objectManagerMock = $this->createMock(ObjectManagerInterface::class); | ||
|
||
$this->adyenNotificationRepository = new AdyenNotificationRepository( | ||
$this->searchResultFactoryMock, | ||
$this->collectionFactoryMock, | ||
$this->collectionProcessorMock, | ||
$this->objectManagerMock, | ||
self::RESOURCE_MODEL | ||
); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$this->adyenNotificationRepository = null; | ||
} | ||
|
||
public function testGetList() | ||
{ | ||
$searchResult = $this->createMock(SearchResultInterface::class); | ||
$searchResult->expects($this->once())->method('setItems'); | ||
$searchResult->expects($this->once())->method('setTotalCount'); | ||
|
||
$this->searchResultFactoryMock->expects($this->once()) | ||
->method('create') | ||
->willReturn($searchResult); | ||
|
||
$collection = $this->createMock(Collection::class); | ||
$this->collectionFactoryMock->expects($this->once()) | ||
->method('create') | ||
->willReturn($collection); | ||
|
||
$searchCriteria = $this->createMock(SearchCriteriaInterface::class); | ||
$this->collectionProcessorMock->expects($this->once()) | ||
->method('process') | ||
->with($searchCriteria, $collection); | ||
|
||
|
||
$result = $this->adyenNotificationRepository->getList($searchCriteria); | ||
$this->assertInstanceOf(SearchResultsInterface::class, $result); | ||
} | ||
|
||
public function testDelete() | ||
{ | ||
$entityMock = $this->createMock(NotificationEntity::class); | ||
|
||
$resourceModelMock = $this->createMock(Notification::class); | ||
$resourceModelMock->expects($this->once())->method('delete')->with($entityMock); | ||
|
||
$this->objectManagerMock->expects($this->once()) | ||
->method('get') | ||
->willReturn($resourceModelMock); | ||
|
||
$result = $this->adyenNotificationRepository->delete($entityMock); | ||
|
||
$this->assertTrue($result); | ||
} | ||
} |