Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: event execution lock #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions Tests/Unit/Service/EventExecutionServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace Crossmedia\Fourallportal\Tests\Service;

use Crossmedia\Fourallportal\Domain\Repository\EventRepository;
use Crossmedia\Fourallportal\Domain\Repository\ModuleRepository;
use Crossmedia\Fourallportal\Domain\Repository\ServerRepository;
use Crossmedia\Fourallportal\Service\EventExecutionService;
use Crossmedia\Fourallportal\Service\LoggingService;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Locking\Exception as LockingException;
use TYPO3\CMS\Core\Locking\Exception\LockAcquireException;
use TYPO3\CMS\Core\Locking\LockFactory;
use TYPO3\CMS\Core\Locking\LockingStrategyInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;
use TYPO3\CMS\Scheduler\Domain\Repository\SchedulerTaskRepository;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

class EventExecutionServiceTest extends UnitTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->resetSingletonInstances = true;
}

public static function lockDataProvider(): \Traversable
{
yield "Lock can be acquired, method will return true" => [
'acquireResult' => true,
'exception' => null,
'expectedResult' => true,
];
yield "Lock can not be acquired, method will return false" => [
'acquireResult' => false,
'exception' => null,
'expectedResult' => false,
];
yield "Acquire throws exception, method will return false" => [
'acquireResult' => false,
'exception' => new LockAcquireException(),
'expectedResult' => false,
];
}

#[Test]
#[DataProvider('lockDataProvider')]
public function verifyLockBehaviour(bool $acquireResult, LockingException|null $exception, bool $expectedResult): void
{
$serverRepositoryMock = $this->createMock(ServerRepository::class);
$eventRepositoryMock = $this->createMock(EventRepository::class);
$moduleRepositoryMock = $this->createMock(ModuleRepository::class);
$loggingServiceMock = $this->createMock(LoggingService::class);
$persistenceManagerInterfaceMock = $this->createMock(PersistenceManagerInterface::class);
$connectionPoolMock = $this->createMock(ConnectionPool::class);
$schedulerTaskRepositoryMock = $this->createMock(SchedulerTaskRepository::class);
$extensionConfigurationMock = $this->createMock(ExtensionConfiguration::class);

$lockerMock = $this->createMock(LockingStrategyInterface::class);
if ($exception !== null) {
$lockerMock->expects(self::once())
->method('acquire')
->with(LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE)
->willThrowException($exception);
} else {
$lockerMock->expects(self::once())
->method('acquire')
->with(LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE)
->willReturn($acquireResult);
}
$lockFactoryMock = $this->createMock(LockFactory::class);
$lockFactoryMock->expects(self::once())
->method('createLocker')
->with('4ap_sync', LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE)
->willReturn($lockerMock);
GeneralUtility::setSingletonInstance(LockFactory::class, $lockFactoryMock);

$eventExecutionService = new EventExecutionService(
$serverRepositoryMock,
$eventRepositoryMock,
$moduleRepositoryMock,
$loggingServiceMock,
$persistenceManagerInterfaceMock,
$connectionPoolMock,
$schedulerTaskRepositoryMock,
$extensionConfigurationMock,
);

self::assertEquals(
$expectedResult,
$eventExecutionService->lock()
);
}

public static function unlockDataProvider(): \Traversable
{
yield "Lock can be released, method will return true" => [
'released' => true,
'exception' => null,
'expectedResult' => true,
];
yield "Lock can not be released, method will return false" => [
'released' => false,
'exception' => null,
'expectedResult' => false,
];
yield "Release the lock will throws exception, method will return false" => [
'released' => false,
'exception' => new LockingException(),
'expectedResult' => false,
];
}

#[Test]
#[DataProvider('unlockDataProvider')]
public function verifyUnLockBehaviour(bool $released, LockingException|null $exception, bool $expectedResult): void
{
$serverRepositoryMock = $this->createMock(ServerRepository::class);
$eventRepositoryMock = $this->createMock(EventRepository::class);
$moduleRepositoryMock = $this->createMock(ModuleRepository::class);
$loggingServiceMock = $this->createMock(LoggingService::class);
$persistenceManagerInterfaceMock = $this->createMock(PersistenceManagerInterface::class);
$connectionPoolMock = $this->createMock(ConnectionPool::class);
$schedulerTaskRepositoryMock = $this->createMock(SchedulerTaskRepository::class);
$extensionConfigurationMock = $this->createMock(ExtensionConfiguration::class);

$lockerMock = $this->createMock(LockingStrategyInterface::class);
if ($exception !== null) {
$lockerMock->expects(self::once())
->method('release')
->willThrowException($exception);
} else {
$lockerMock->expects(self::once())
->method('release')
->willReturn($released);
}

$lockFactoryMock = $this->createMock(LockFactory::class);
$lockFactoryMock->expects(self::once())
->method('createLocker')
->with('4ap_sync', LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE)
->willReturn($lockerMock);
GeneralUtility::setSingletonInstance(LockFactory::class, $lockFactoryMock);

$eventExecutionService = new EventExecutionService(
$serverRepositoryMock,
$eventRepositoryMock,
$moduleRepositoryMock,
$loggingServiceMock,
$persistenceManagerInterfaceMock,
$connectionPoolMock,
$schedulerTaskRepositoryMock,
$extensionConfigurationMock,
);

self::assertEquals(
$expectedResult,
$eventExecutionService->unlock()
);
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"ext-curl": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.5"
"phpunit/phpunit": "^10.5",
"typo3/testing-framework": "^8.0.1"
},
"autoload": {
"psr-4": {
Expand Down