Commit 4368db6a authored by Daniel Hürtgen's avatar Daniel Hürtgen

[FEATURE] LockFactory implemented

parent 7ba1c2b4
<?php
namespace Higidi\Lock;
use Higidi\Lock\Configuration\Configuration;
use TYPO3\CMS\Core\Locking\Exception\LockCreateException;
use TYPO3\CMS\Core\Locking\LockFactory as CoreLockFactory;
use TYPO3\CMS\Core\Locking\LockingStrategyInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Factory class creates locks.
*/
class LockFactory extends CoreLockFactory
{
/**
* @var Configuration
*/
protected $configuration;
/**
* @param Configuration|null $configuration The configuration to use
*/
public function __construct(Configuration $configuration = null)
{
if (null === $configuration) {
$configuration = GeneralUtility::makeInstance(Configuration::class);
}
$this->configuration = $configuration;
}
/**
* Get the configuration.
*
* @return Configuration
*/
public function getConfiguration()
{
return $this->configuration;
}
/**
* Get best matching locking method
*
* @param string $id ID to identify this lock in the system
* @param int $capabilities LockingStrategyInterface::LOCK_CAPABILITY_* elements combined with bit-wise OR
*
* @return LockingStrategyInterface Class name for a locking method
* @throws LockCreateException if no locker could be created with the requested capabilities
*/
public function createLocker($id, $capabilities = LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE)
{
if (! $this->configuration->isActive()) {
return parent::createLocker($id, $capabilities);
}
$className = $this->configuration->getStrategy();
$locker = GeneralUtility::makeInstance($className, $id);
return $locker;
}
}
<?php
namespace Higidi\Lock\Tests\Unit;
use Higidi\Lock\Configuration\Configuration;
use Higidi\Lock\LockFactory;
use Higidi\Lock\Strategy\MutexAdapterStrategy;
use Nimut\TestingFramework\TestCase\UnitTestCase;
use TYPO3\CMS\Core\Locking as CoreLocking;
use TYPO3\CMS\Core\Locking\LockFactory as CoreLockFactory;
/**
* Test case for "\Higidi\Lock\LockFactory".
*
* @covers \Higidi\Lock\LockFactory
*/
class LockFactoryTest extends UnitTestCase
{
/**
* @return array
*/
public function coreLockingStrategyDataProvider()
{
return [
'simple_strategy' => [CoreLocking\SimpleLockStrategy::class],
'file_lock_strategy' => [CoreLocking\FileLockStrategy::class],
'semaphore_strategy' => [CoreLocking\SemaphoreLockStrategy::class],
];
}
/**
* @test
*/
public function itExtendsTheCoreLockFactory()
{
$sut = new LockFactory();
$this->assertInstanceOf(CoreLockFactory::class, $sut);
}
/**
* @test
*/
public function itCreatesADefaultConfigurationIfNotPassed()
{
$sut = new LockFactory();
$this->assertInstanceOf(Configuration::class, $sut->getConfiguration());
}
/**
* @test
*/
public function itHoldsAConfiguration()
{
$configuration = $this->prophesize(Configuration::class);
$sut = new LockFactory($configuration->reveal());
$this->assertSame($configuration->reveal(), $sut->getConfiguration());
}
/**
* @test
*/
public function itOnlyOperatesIfIsActive()
{
$configuration = $this->prophesize(Configuration::class);
$configuration
->isActive()
->willReturn(false);
$sut = new LockFactory($configuration->reveal());
$locker = $sut->createLocker('blafoo');
$this->assertNotInstanceOf(MutexAdapterStrategy::class, $locker);
}
/**
* @test
* @dataProvider coreLockingStrategyDataProvider
*
* @param string $strategy
*/
public function itCreatesSpecificLockerIfConfigurationIsSet($strategy)
{
$configuration = $this->prophesize(Configuration::class);
$configuration
->isActive()
->willReturn(true);
$configuration
->getStrategy()
->willReturn($strategy);
$configuration
->isMutexStrategy()
->willReturn(false);
$sut = new LockFactory($configuration->reveal());
$locker = $sut->createLocker('blafoo');
$this->assertInstanceOf($strategy, $locker);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment