-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathMySQLMutexTest.php
64 lines (47 loc) · 1.6 KB
/
MySQLMutexTest.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
<?php
declare(strict_types=1);
namespace Malkusch\Lock\Tests\Mutex;
use Malkusch\Lock\Mutex\MySQLMutex;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class MySQLMutexTest extends TestCase
{
/** @var \PDO&MockObject */
private $pdo;
/** @var MySQLMutex */
private $mutex;
#[\Override]
protected function setUp(): void
{
parent::setUp();
$this->pdo = $this->createMock(\PDO::class);
$this->mutex = new MySQLMutex($this->pdo, 'test');
}
public function testAcquireLock(): void
{
$statement = $this->createMock(\PDOStatement::class);
$this->pdo->expects(self::once())
->method('prepare')
->with('SELECT GET_LOCK(?, ?)')
->willReturn($statement);
$statement->expects(self::once())
->method('execute')
->with(['php-malkusch-lock:test', 0]);
$statement->expects(self::once())
->method('fetch')
->willReturn([1]);
\Closure::bind(static fn ($mutex) => $mutex->lock(), null, MySQLMutex::class)($this->mutex);
}
public function testReleaseLock(): void
{
$statement = $this->createMock(\PDOStatement::class);
$this->pdo->expects(self::once())
->method('prepare')
->with('DO RELEASE_LOCK(?)')
->willReturn($statement);
$statement->expects(self::once())
->method('execute')
->with(['php-malkusch-lock:test']);
\Closure::bind(static fn ($mutex) => $mutex->unlock(), null, MySQLMutex::class)($this->mutex);
}
}