1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Mutex Library. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Tests\AMF\Mutex; |
11
|
|
|
|
12
|
|
|
use AMF\Mutex\MutexInterface; |
13
|
|
|
use AMF\Mutex\MutexAdapter; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Test suite for adapter of mutex. |
17
|
|
|
* |
18
|
|
|
* @author Amine Fattouch <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class MutexAdapterTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \Phake_IMock |
24
|
|
|
*/ |
25
|
|
|
protected $mutex; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Phake_IMock |
29
|
|
|
*/ |
30
|
|
|
protected $mutexAdapter; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
public function setUp() |
36
|
|
|
{ |
37
|
|
|
$this->mutex = \Phake::partialMock(MutexInterface::class); |
38
|
|
|
$this->mutexAdapter = \Phake::partialMock(MutexAdapter::class, $this->mutex, 10, 0); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @test |
43
|
|
|
*/ |
44
|
|
|
public function shouldAcquireMutex() |
45
|
|
|
{ |
46
|
|
|
\Phake::when($this->mutexAdapter)->generateKey($this->anything())->thenReturn('worker_test'); |
47
|
|
|
\Phake::when($this->mutex)->acquire($this->anything(), $this->anything())->thenReturn('worker_test'); |
48
|
|
|
|
49
|
|
|
$this->assertEquals('worker_test', $this->mutexAdapter->acquire('test')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @test |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function shouldThrowAnExceptionIfMutexIsAlreadyAcquired() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$this->setExpectedExceptionRegExp(\RuntimeException::class); |
58
|
|
|
|
59
|
|
|
\Phake::when($this->mutexAdapter)->generateKey($this->anything())->thenReturn('worker_test'); |
60
|
|
|
\Phake::when($this->mutex)->acquire($this->anything(), $this->anything())->thenReturn(null); |
61
|
|
|
|
62
|
|
|
$this->mutexAdapter->acquire('test'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function shouldReleaseMutex() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
\Phake::when($this->mutexAdapter)->generateKey('test')->thenReturn('worker_test'); |
71
|
|
|
\Phake::when($this->mutex)->release($this->anything(), $this->anything())->thenReturn(true); |
72
|
|
|
|
73
|
|
|
$this->assertNull($this->mutexAdapter->release('worker_test')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @test |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function shouldThrowAnExceptionIfCantReleaseTheMutex() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$this->setExpectedExceptionRegExp(\RuntimeException::class); |
82
|
|
|
|
83
|
|
|
\Phake::when($this->mutexAdapter)->generateKey($this->anything())->thenReturn('worker_test'); |
84
|
|
|
\Phake::when($this->mutex)->release($this->anything())->thenReturn(false); |
85
|
|
|
|
86
|
|
|
$this->mutexAdapter->release('test'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.