-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathMutexTest.php
133 lines (110 loc) · 4.09 KB
/
MutexTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace Illuminated\Console\Tests;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis as RedisFacade;
use Illuminated\Console\Mutex;
use Illuminated\Console\Tests\App\Console\Commands\GenericCommand;
use NinjaMutex\Lock\FlockLock;
use NinjaMutex\Lock\MemcachedLock;
use NinjaMutex\Lock\MySqlLock;
use NinjaMutex\Lock\PhpRedisLock;
use NinjaMutex\Lock\PredisRedisLock;
use Predis\Client as PredisClient;
use Redis;
class MutexTest extends TestCase
{
/**
* The console command mock.
*
* @var \Mockery\Mock|\Illuminate\Console\Command
*/
private $command;
/**
* Setup the test environment.
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->command = mock(GenericCommand::class)->makePartial();
$this->command->expects('getName')->andReturn('icm:generic');
$this->command->expects('argument')->andReturn(['foo' => 'bar']);
}
/** @test */
public function it_requires_command_as_constructor_parameter()
{
$mutex = new Mutex($this->command);
$this->assertInstanceOf(Mutex::class, $mutex);
}
/** @test */
public function it_determines_ninja_mutex_lock_once_while_creation()
{
$mutex = new Mutex($this->command);
$this->assertSame($mutex->getNinjaMutexLock(), $mutex->getNinjaMutexLock());
}
/** @test */
public function it_has_default_strategy_which_is_file()
{
$this->command->expects('getMutexStrategy')->andReturn('foobar');
$mutex = new Mutex($this->command);
$expectedLock = new FlockLock(storage_path('app'));
$this->assertEquals($expectedLock, $mutex->getNinjaMutexLock());
}
/** @test */
public function it_supports_mysql_strategy()
{
$this->command->expects('getMutexStrategy')->andReturn('mysql');
$mutex = new Mutex($this->command);
$expectedLock = new MySqlLock(
config('database.connections.mysql.username'),
config('database.connections.mysql.password'),
config('database.connections.mysql.host'),
config('database.connections.mysql.port', 3306)
);
$this->assertEquals($expectedLock, $mutex->getNinjaMutexLock());
}
/** @test */
public function it_supports_redis_strategy_with_phpredis_client_which_is_default()
{
$this->command->expects('getMutexStrategy')->andReturn('redis');
$redis = mock(Redis::class);
RedisFacade::shouldReceive('connection->client')->once()->andReturn($redis);
$mutex = new Mutex($this->command);
$expectedLock = new PhpRedisLock($redis);
$this->assertEquals($expectedLock, $mutex->getNinjaMutexLock());
}
/** @test */
public function it_supports_redis_strategy_with_predis_client()
{
config(['database.redis.client' => 'predis']);
$this->command->expects('getMutexStrategy')->andReturn('redis');
$predis = mock(PredisClient::class);
RedisFacade::shouldReceive('connection->client')->once()->andReturn($predis);
$mutex = new Mutex($this->command);
$expectedLock = new PredisRedisLock($predis);
$this->assertEquals($expectedLock, $mutex->getNinjaMutexLock());
}
/** @test */
public function it_supports_memcached_strategy()
{
Cache::shouldReceive('getStore')->withNoArgs()->twice()->andReturnSelf();
Cache::shouldReceive('getMemcached')->withNoArgs()->twice()->andReturnSelf();
$this->command->expects('getMutexStrategy')->andReturn('memcached');
$mutex = new Mutex($this->command);
$expectedLock = new MemcachedLock(Cache::getStore()->getMemcached());
$this->assertEquals($expectedLock, $mutex->getNinjaMutexLock());
}
/**
* @test
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function it_delegates_public_method_calls_to_ninja_mutex()
{
$ninja = mock('overload:NinjaMutex\Mutex');
$ninja->expects('isLocked');
$mutex = new Mutex($this->command);
$mutex->isLocked();
}
}