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
7a302aab
Commit
7a302aab
authored
Nov 10, 2017
by
Daniel Hürtgen
Browse files
[FEATURE] LockBuilder::buildPhpRedisLock() implemented
parent
0dee446b
Changes
3
Hide whitespace changes
Inline
Side-by-side
.gitlab-ci.yml
View file @
7a302aab
...
...
@@ -35,7 +35,12 @@ lint:cgl:
.test
:
&test
stage
:
test
services
:
-
redis:latest
before_script
:
-
apk add --no-cache --virtual .build-deps autoconf g++ make
-
pecl install -f redis
-
docker-php-ext-enable redis
-
wget https://composer.github.io/installer.sig -O - -q | tr -d '\n' > installer.sig
-
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
-
php -r "if (hash_file('SHA384', 'composer-setup.php') === file_get_contents('installer.sig')) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
...
...
Classes/Builder/LockBuilder.php
View file @
7a302aab
...
...
@@ -41,6 +41,52 @@ class LockBuilder implements SingletonInterface
return
$lock
;
}
/**
* @param array $configuration
*
* @return Lock\PhpRedisLock
* @throws InvalidConfigurationException
* @throws LockCreateException
*/
public
function
buildPhpRedisLock
(
array
$configuration
)
{
if
(
!
extension_loaded
(
'redis'
))
{
throw
new
LockCreateException
(
'PHP extension "redis" not loaded'
,
1510321193
);
}
$host
=
isset
(
$configuration
[
'host'
])
?
(
string
)
$configuration
[
'host'
]
:
null
;
if
(
empty
(
$host
))
{
throw
new
InvalidConfigurationException
(
$configuration
,
'Missing or empty redis host'
,
1510321408
);
}
$port
=
isset
(
$configuration
[
'port'
])
?
(
int
)
$configuration
[
'port'
]
:
6379
;
$timeout
=
isset
(
$configuration
[
'timeout'
])
?
(
float
)
$configuration
[
'timeout'
]
:
0.0
;
$password
=
isset
(
$configuration
[
'password'
])
?
(
string
)
$configuration
[
'password'
]
:
null
;
$database
=
isset
(
$configuration
[
'database'
])
?
(
int
)
$configuration
[
'database'
]
:
0
;
/** @var \Redis $redis */
$errorReporting
=
error_reporting
();
error_reporting
(
0
);
$redis
=
GeneralUtility
::
makeInstance
(
\
Redis
::
class
);
if
(
!
$redis
->
connect
(
$host
,
$port
,
$timeout
))
{
throw
new
LockCreateException
(
sprintf
(
'Could not connect to redis host "%s" on port %d'
,
$host
,
$port
),
1510321516
);
};
if
(
!
empty
(
$password
))
{
if
(
!
$redis
->
auth
(
$password
))
{
throw
new
LockCreateException
(
'Authentication with redis host failed'
,
1510321753
);
};
}
if
(
!
$redis
->
select
(
$database
))
{
throw
new
LockCreateException
(
'Switch redis database to %d failed'
,
1510321791
);
};
error_reporting
(
$errorReporting
);
$lock
=
GeneralUtility
::
makeInstance
(
Lock\PhpRedisLock
::
class
,
$redis
);
return
$lock
;
}
/**
* @param string $className
* @param array $configuration
...
...
Tests/Unit/Builder/LockBuilderTest.php
View file @
7a302aab
...
...
@@ -127,4 +127,82 @@ class LockBuilderTest extends UnitTestCase
$builder
->
buildFlockLock
([]);
}
/**
* @test
*/
public
function
itBuildsAPhpRedisLock
()
{
$configuration
=
[
'host'
=>
'redis'
,
];
$builder
=
new
LockBuilder
();
$lock
=
$builder
->
buildPhpRedisLock
(
$configuration
);
$this
->
assertInstanceOf
(
Lock\PhpRedisLock
::
class
,
$lock
);
}
/**
* @test
* @expectedException \Higidi\Lock\Builder\Exception\InvalidConfigurationException
* @expectedExceptionCode 1510321408
*/
public
function
itThrowsAInvalidConfigurationExceptionOnBuildAPhpRedisLockWithMissingHostConfiguration
()
{
$builder
=
new
LockBuilder
();
$builder
->
buildPhpRedisLock
([]);
}
/**
* @test
* @expectedException \Higidi\Lock\Builder\Exception\LockCreateException
* @expectedExceptionCode 1510321516
*/
public
function
itThrowsALockCreateExceptionOnBuildPhpRedisLockIfCanNotConnectToRedis
()
{
$configuration
=
[
'host'
=>
'1.2.3.4'
,
'port'
=>
1
,
];
$builder
=
new
LockBuilder
();
$builder
->
buildPhpRedisLock
(
$configuration
);
}
/**
* @test
* @expectedException \Higidi\Lock\Builder\Exception\LockCreateException
* @expectedExceptionCode 1510321753
*/
public
function
itThrowsALockCreateExceptionOnBuildPhpRedisLockIfCanNotAuthWithRedis
()
{
$configuration
=
[
'host'
=>
'redis'
,
'password'
=>
'invalidPassword'
,
];
$builder
=
new
LockBuilder
();
$builder
->
buildPhpRedisLock
(
$configuration
);
}
/**
* @test
* @expectedException \Higidi\Lock\Builder\Exception\LockCreateException
* @expectedExceptionCode 1510321791
*/
public
function
itThrowsALockCreateExceptionOnBuildPhpRedisLockIfCanNotSelectRedisDatabase
()
{
$configuration
=
[
'host'
=>
'redis'
,
'database'
=>
-
1
,
];
$builder
=
new
LockBuilder
();
$builder
->
buildPhpRedisLock
(
$configuration
);
}
}
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