Passed
Pull Request — master (#439)
by Alejandro
11:54
created

LockMigrationsSubscriberTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 26
c 1
b 0
f 0
dl 0
loc 55
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A migrationForTheFirstTimeAcquiresLockAndReleasesLockAfterFinishing() 0 16 1
A migrationInProgressIsNotifiedWhenLockCannotBeAcquired() 0 15 1
A setUp() 0 4 1
A itIsSubscribedToProperEvents() 0 4 1
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