Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
TYPO3
Extensions
lock
Commits
4368db6a
Commit
4368db6a
authored
Nov 09, 2017
by
Daniel Hürtgen
Browse files
[FEATURE] LockFactory implemented
parent
7ba1c2b4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Classes/LockFactory.php
0 → 100644
View file @
4368db6a
<?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
;
}
}
Tests/Unit/LockFactoryTest.php
0 → 100644
View file @
4368db6a
<?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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment