5
5
use Illuminate \Console \CacheCommandMutex ;
6
6
use Illuminate \Console \Command ;
7
7
use Illuminate \Contracts \Cache \Factory ;
8
+ use Illuminate \Contracts \Cache \LockProvider ;
8
9
use Illuminate \Contracts \Cache \Repository ;
9
10
use Mockery as m ;
11
+ use Mockery \MockInterface ;
10
12
use PHPUnit \Framework \TestCase ;
11
13
12
14
class CacheCommandMutexTest extends TestCase
@@ -35,16 +37,22 @@ protected function setUp(): void
35
37
{
36
38
$ this ->cacheFactory = m::mock (Factory::class);
37
39
$ this ->cacheRepository = m::mock (Repository::class);
38
- $ this ->cacheFactory ->shouldReceive ('store ' )->andReturn ($ this ->cacheRepository );
39
40
$ this ->mutex = new CacheCommandMutex ($ this ->cacheFactory );
40
41
$ this ->command = new class extends Command
41
42
{
42
43
protected $ name = 'command-name ' ;
43
44
};
44
45
}
45
46
47
+ protected function tearDown (): void
48
+ {
49
+ m::close ();
50
+ parent ::tearDown ();
51
+ }
52
+
46
53
public function testCanCreateMutex ()
47
54
{
55
+ $ this ->mockUsingCacheStore ();
48
56
$ this ->cacheRepository ->shouldReceive ('add ' )
49
57
->andReturn (true )
50
58
->once ();
@@ -55,6 +63,7 @@ public function testCanCreateMutex()
55
63
56
64
public function testCannotCreateMutexIfAlreadyExist ()
57
65
{
66
+ $ this ->mockUsingCacheStore ();
58
67
$ this ->cacheRepository ->shouldReceive ('add ' )
59
68
->andReturn (false )
60
69
->once ();
@@ -65,6 +74,31 @@ public function testCannotCreateMutexIfAlreadyExist()
65
74
66
75
public function testCanCreateMutexWithCustomConnection ()
67
76
{
77
+ $ this ->mockUsingCacheStore ();
78
+ $ this ->cacheRepository ->shouldReceive ('getStore ' )
79
+ ->with ('test ' )
80
+ ->andReturn ($ this ->cacheRepository );
81
+ $ this ->cacheRepository ->shouldReceive ('add ' )
82
+ ->andReturn (false )
83
+ ->once ();
84
+ $ this ->mutex ->useStore ('test ' );
85
+
86
+ $ this ->mutex ->create ($ this ->command );
87
+ }
88
+
89
+ public function testCanCreateMutexWithLockProvider ()
90
+ {
91
+ $ lock = $ this ->mockUsingLockProvider ();
92
+ $ this ->acquireLockExpectations ($ lock , true );
93
+
94
+ $ actual = $ this ->mutex ->create ($ this ->command );
95
+
96
+ $ this ->assertTrue ($ actual );
97
+ }
98
+
99
+ public function testCanCreateMutexWithCustomLockProviderConnection ()
100
+ {
101
+ $ this ->mockUsingCacheStore ();
68
102
$ this ->cacheRepository ->shouldReceive ('getStore ' )
69
103
->with ('test ' )
70
104
->andReturn ($ this ->cacheRepository );
@@ -75,4 +109,55 @@ public function testCanCreateMutexWithCustomConnection()
75
109
76
110
$ this ->mutex ->create ($ this ->command );
77
111
}
112
+
113
+ public function testCannotCreateMutexIfAlreadyExistWithLockProvider ()
114
+ {
115
+ $ lock = $ this ->mockUsingLockProvider ();
116
+ $ this ->acquireLockExpectations ($ lock , false );
117
+ $ actual = $ this ->mutex ->create ($ this ->command );
118
+
119
+ $ this ->assertFalse ($ actual );
120
+ }
121
+
122
+ public function testCanCreateMutexWithCustomConnectionWithLockProvider ()
123
+ {
124
+ $ lock = m::mock (LockProvider::class);
125
+ $ this ->cacheFactory ->expects ('store ' )->once ()->with ('test ' )->andReturn ($ this ->cacheRepository );
126
+ $ this ->cacheRepository ->expects ('getStore ' )->twice ()->andReturn ($ lock );
127
+
128
+ $ this ->acquireLockExpectations ($ lock , true );
129
+ $ this ->mutex ->useStore ('test ' );
130
+
131
+ $ this ->mutex ->create ($ this ->command );
132
+ }
133
+
134
+ /**
135
+ * @return void
136
+ */
137
+ private function mockUsingCacheStore (): void
138
+ {
139
+ $ this ->cacheFactory ->expects ('store ' )->once ()->andReturn ($ this ->cacheRepository );
140
+ $ this ->cacheRepository ->expects ('getStore ' )->andReturn (null );
141
+ }
142
+
143
+ private function mockUsingLockProvider (): m \MockInterface
144
+ {
145
+ $ lock = m::mock (LockProvider::class);
146
+ $ this ->cacheFactory ->expects ('store ' )->once ()->andReturn ($ this ->cacheRepository );
147
+ $ this ->cacheRepository ->expects ('getStore ' )->twice ()->andReturn ($ lock );
148
+
149
+ return $ lock ;
150
+ }
151
+
152
+ private function acquireLockExpectations (MockInterface $ lock , bool $ acquiresSuccessfully ): void
153
+ {
154
+ $ lock ->expects ('lock ' )
155
+ ->once ()
156
+ ->with (m::type ('string ' ), m::type ('int ' ))
157
+ ->andReturns ($ lock );
158
+
159
+ $ lock ->expects ('get ' )
160
+ ->once ()
161
+ ->andReturns ($ acquiresSuccessfully );
162
+ }
78
163
}
0 commit comments