Skip to content

Commit 5d02b19

Browse files
Add unit test for predis integration
1 parent 475d29c commit 5d02b19

File tree

1 file changed

+66
-35
lines changed

1 file changed

+66
-35
lines changed

tests/mutex/PredisMutexTest.php

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22

33
namespace malkusch\lock\mutex;
44

5+
use PHPUnit\Framework\MockObject\MockObject;
56
use PHPUnit\Framework\TestCase;
6-
use Predis\Client;
77
use Predis\ClientInterface;
8+
use Predis\PredisException;
89

910
/**
1011
* Tests for PredisMutex.
1112
*
12-
* These tests require the environment variable:
13-
*
14-
* REDIS_URIS - a comma separated list of redis:// URIs.
15-
*
16-
* @author Markus Malkusch <[email protected]>
1713
* @link bitcoin:1P5FAZ4QhXCuwYPnLZdk3PJsqePbu1UDDA Donations
1814
* @license WTFPL
1915
* @see PredisMutex
@@ -22,51 +18,58 @@
2218
class PredisMutexTest extends TestCase
2319
{
2420
/**
25-
* @var ClientInterface
21+
* @var ClientInterface|MockObject
2622
*/
27-
protected $client;
23+
private $client;
24+
25+
/**
26+
* @var PredisMutex
27+
*/
28+
private $mutex;
2829

2930
protected function setUp()
3031
{
3132
parent::setUp();
3233

33-
$config = $this->getPredisConfig();
34-
35-
$this->client = new Client($config);
34+
$this->client = $this->getMockBuilder(ClientInterface::class)
35+
->setMethods(array_merge(get_class_methods(ClientInterface::class), ["set", "eval"]))
36+
->getMock();
3637

37-
if (is_string($config)) {
38-
$this->client->flushall(); // Clear any existing locks
39-
}
38+
$this->mutex = new PredisMutex([$this->client], "test");
4039
}
4140

42-
private function getPredisConfig()
41+
/**
42+
* Tests add() fails.
43+
*
44+
* @expectedException \malkusch\lock\exception\LockAcquireException
45+
*/
46+
public function testAddFailsToSetKey()
4347
{
44-
if (getenv("REDIS_URIS") === false) {
45-
return "tcp://localhost:6379";
46-
}
47-
48-
$servers = explode(",", getenv("REDIS_URIS"));
48+
$this->client->expects($this->atLeastOnce())
49+
->method("set")
50+
->with("lock_test", $this->isType("integer"), "EX", 4, "NX")
51+
->willReturn(null);
4952

50-
return array_map(
51-
function ($redisUri) {
52-
return str_replace("redis://", "tcp://", $redisUri);
53-
},
54-
$servers
53+
$this->mutex->synchronized(
54+
function () {
55+
$this->fail("Code execution is not expected");
56+
}
5557
);
5658
}
5759

5860
/**
59-
* Tests add() fails.
61+
* Tests add() errors.
6062
*
6163
* @expectedException \malkusch\lock\exception\LockAcquireException
6264
*/
63-
public function testAddFails()
65+
public function testAddErrors()
6466
{
65-
$client = new Client("redis://127.0.0.1:12345");
67+
$this->client->expects($this->atLeastOnce())
68+
->method("set")
69+
->with("lock_test", $this->isType("integer"), "EX", 4, "NX")
70+
->willThrowException($this->createMock(PredisException::class));
6671

67-
$mutex = new PredisMutex([$client], "test");
68-
69-
$mutex->synchronized(
72+
$this->mutex->synchronized(
7073
function () {
7174
$this->fail("Code execution is not expected");
7275
}
@@ -75,11 +78,23 @@ function () {
7578

7679
public function testWorksNormally()
7780
{
78-
$mutex = new PredisMutex([$this->client], "test");
81+
$this->client->expects($this->atLeastOnce())
82+
->method("set")
83+
->with("lock_test", $this->isType("integer"), "EX", 4, "NX")
84+
->willReturnSelf();
85+
86+
$this->client->expects($this->once())
87+
->method("eval")
88+
->with($this->anything(), 1, "lock_test", $this->isType("integer"))
89+
->willReturn(true);
90+
91+
$executed = false;
7992

80-
$mutex->synchronized(function (): void {
81-
$this->expectNotToPerformAssertions();
93+
$this->mutex->synchronized(function () use (&$executed): void {
94+
$executed = true;
8295
});
96+
97+
$this->assertTrue($executed);
8398
}
8499

85100
/**
@@ -89,6 +104,22 @@ public function testWorksNormally()
89104
*/
90105
public function testEvalScriptFails()
91106
{
92-
$this->markTestIncomplete();
107+
$this->client->expects($this->atLeastOnce())
108+
->method("set")
109+
->with("lock_test", $this->isType("integer"), "EX", 4, "NX")
110+
->willReturnSelf();
111+
112+
$this->client->expects($this->once())
113+
->method("eval")
114+
->with($this->anything(), 1, "lock_test", $this->isType("integer"))
115+
->willThrowException($this->createMock(PredisException::class));
116+
117+
$executed = false;
118+
119+
$this->mutex->synchronized(function () use (&$executed): void {
120+
$executed = true;
121+
});
122+
123+
$this->assertTrue($executed);
93124
}
94125
}

0 commit comments

Comments
 (0)