|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Common\Migrations; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\Migrations\Events; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Prophecy\Argument; |
|
9
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
10
|
|
|
use Shlinkio\Shlink\Common\Exception\MigrationsException; |
|
11
|
|
|
use Shlinkio\Shlink\Common\Migrations\LockMigrationsSubscriber; |
|
12
|
|
|
use Symfony\Component\Lock\Factory as Locker; |
|
13
|
|
|
use Symfony\Component\Lock\LockInterface; |
|
14
|
|
|
|
|
15
|
|
|
class LockMigrationsSubscriberTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var LockMigrationsSubscriber */ |
|
18
|
|
|
private $subscriber; |
|
19
|
|
|
/** @var ObjectProphecy */ |
|
20
|
|
|
private $locker; |
|
21
|
|
|
|
|
22
|
|
|
public function setUp(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$this->locker = $this->prophesize(Locker::class); |
|
25
|
|
|
$this->subscriber = new LockMigrationsSubscriber($this->locker->reveal()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** @test */ |
|
29
|
|
|
public function itIsSubscribedToProperEvents(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$expectedEvents = [Events::onMigrationsMigrating, Events::onMigrationsMigrated]; |
|
32
|
|
|
$this->assertEquals($expectedEvents, $this->subscriber->getSubscribedEvents()); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** @test */ |
|
36
|
|
|
public function migrationInProgressIsNotifiedWhenLockCannotBeAcquired(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$lock = $this->prophesize(LockInterface::class); |
|
39
|
|
|
$acquire = $lock->acquire()->willReturn(false); |
|
40
|
|
|
$createLock = $this->locker->createLock(Argument::type('string'), Argument::type('numeric'))->willReturn( |
|
41
|
|
|
$lock->reveal() |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
$acquire->shouldBeCalledOnce(); |
|
45
|
|
|
$createLock->shouldBeCalledOnce(); |
|
46
|
|
|
|
|
47
|
|
|
$this->expectException(MigrationsException::class); |
|
48
|
|
|
$this->expectExceptionMessage('Migrations already in progress. Skipping.'); |
|
49
|
|
|
|
|
50
|
|
|
$this->subscriber->onMigrationsMigrating(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** @test */ |
|
54
|
|
|
public function migrationForTheFirstTimeAcquiresLockAndReleasesLockAfterFinishing(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$lock = $this->prophesize(LockInterface::class); |
|
57
|
|
|
$acquire = $lock->acquire()->willReturn(true); |
|
58
|
|
|
$release = $lock->release()->will(function () { |
|
59
|
|
|
}); |
|
60
|
|
|
$createLock = $this->locker->createLock(Argument::type('string'), Argument::type('numeric'))->willReturn( |
|
61
|
|
|
$lock->reveal() |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
$this->subscriber->onMigrationsMigrating(); |
|
65
|
|
|
$this->subscriber->onMigrationsMigrated(); |
|
66
|
|
|
|
|
67
|
|
|
$acquire->shouldHaveBeenCalledOnce(); |
|
68
|
|
|
$release->shouldHaveBeenCalledOnce(); |
|
69
|
|
|
$createLock->shouldHaveBeenCalledOnce(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|