Skip to content

Commit

Permalink
[ECP-9177] Write unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Can Demiralp committed Jan 7, 2025
1 parent 423770e commit b62a1e8
Show file tree
Hide file tree
Showing 4 changed files with 391 additions and 0 deletions.
101 changes: 101 additions & 0 deletions Test/Unit/Cron/CleanupNotificationsTest.php
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 Test/Unit/Cron/Providers/ProcessedOldNotificationsProviderTest.php
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()
);
}
}
41 changes: 41 additions & 0 deletions Test/Unit/Helper/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,45 @@ public function testGetIsCvcRequiredForRecurringCardPayments()
$result = $this->configHelper->getIsCvcRequiredForRecurringCardPayments($storeId);
$this->assertEquals($expectedResult, $result);
}

public function testGetIsWebhookCleanupEnabled()
{
$storeId = PHP_INT_MAX;

$path = sprintf(
"%s/%s/%s",
Config::XML_PAYMENT_PREFIX,
Config::XML_ADYEN_ABSTRACT_PREFIX,
Config::XML_CLEANUP_OLD_WEBHOOKS
);

$this->scopeConfigMock->expects($this->once())
->method('isSetFlag')
->with($this->equalTo($path), $this->equalTo(ScopeInterface::SCOPE_STORE), $this->equalTo($storeId))
->willReturn(true);

$result = $this->configHelper->getIsWebhookCleanupEnabled($storeId);
$this->assertTrue($result);
}

public function testGetRequiredDaysForOldWebhooks()
{
$storeId = PHP_INT_MAX;

$path = sprintf(
"%s/%s/%s",
Config::XML_PAYMENT_PREFIX,
Config::XML_ADYEN_ABSTRACT_PREFIX,
Config::XML_REQUIRED_DAYS_OLD_WEBHOOKS
);

$this->scopeConfigMock->expects($this->once())
->method('getValue')
->with($this->equalTo($path), $this->equalTo(ScopeInterface::SCOPE_STORE), $this->equalTo($storeId))
->willReturn("90");

$result = $this->configHelper->getRequiredDaysForOldWebhooks($storeId);
$this->assertIsInt($result);
$this->assertEquals(90, $result);
}
}
102 changes: 102 additions & 0 deletions Test/Unit/Model/AdyenNotificationRepositoryTest.php
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);
}
}

0 comments on commit b62a1e8

Please sign in to comment.