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

LockMigrationsEntityManagerDelegatorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 14
rs 9.9666
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Common\Migrations;
5
6
use Closure;
7
use Doctrine\Common\EventManager;
8
use Doctrine\DBAL\Connection;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Interop\Container\ContainerInterface;
11
use PHPUnit\Framework\TestCase;
12
use Prophecy\Argument;
13
use Prophecy\Prophecy\ObjectProphecy;
14
use Shlinkio\Shlink\Common\Migrations\LockMigrationsEntityManagerDelegator;
15
use Shlinkio\Shlink\Common\Migrations\LockMigrationsSubscriber;
16
use Symfony\Component\Lock\Factory as Locker;
17
18
class LockMigrationsEntityManagerDelegatorTest extends TestCase
19
{
20
    private static $originalScriptName;
21
22
    /** @var LockMigrationsEntityManagerDelegator */
23
    private $delegator;
24
    /** @var ObjectProphecy */
25
    private $container;
26
    /** @var ObjectProphecy */
27
    private $em;
28
    /** @var ObjectProphecy */
29
    private $connection;
30
    /** @var ObjectProphecy */
31
    private $eventManager;
32
    /** @var Closure */
33
    private $callback;
34
35
    public static function setUpBeforeClass(): void
36
    {
37
        static::$originalScriptName = $_SERVER['SCRIPT_NAME'];
0 ignored issues
show
Bug introduced by
Since $originalScriptName is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $originalScriptName to at least protected.
Loading history...
38
    }
39
40
    public static function tearDownAfterClass(): void
41
    {
42
        $_SERVER['SCRIPT_NAME'] = static::$originalScriptName;
0 ignored issues
show
Bug introduced by
Since $originalScriptName is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $originalScriptName to at least protected.
Loading history...
43
    }
44
45
    public function setUp(): void
46
    {
47
        $this->container = $this->prophesize(ContainerInterface::class);
48
49
        $this->em = $this->prophesize(EntityManagerInterface::class);
50
        $this->connection = $this->prophesize(Connection::class);
51
        $this->em->getConnection()->willReturn($this->connection->reveal());
52
        $this->eventManager = $this->prophesize(EventManager::class);
53
        $this->connection->getEventManager()->willReturn($this->eventManager->reveal());
54
55
        $this->callback = function () {
56
            return $this->em->reveal();
57
        };
58
        $this->delegator = new LockMigrationsEntityManagerDelegator();
59
    }
60
61
    /** @test */
62
    public function subscriberIsNotRegisteredWhenScriptIsNotMigrations(): void
63
    {
64
        $_SERVER['SCRIPT_NAME'] = 'not_migrations';
65
66
        ($this->delegator)($this->container->reveal(), '', $this->callback);
67
68
        $this->container->get(Locker::class)->shouldNotHaveBeenCalled();
69
        $this->em->getConnection()->shouldNotHaveBeenCalled();
70
        $this->connection->getEventManager()->shouldNotHaveBeenCalled();
71
        $this->eventManager->addEventSubscriber()->shouldNotHaveBeenCalled();
72
    }
73
74
    /**
75
     * @test
76
     * @dataProvider provideMigrationsScripts
77
     */
78
    public function subscriberIsRegisteredWhenScriptIsMigrations(string $scriptName): void
79
    {
80
        $_SERVER['SCRIPT_NAME'] = $scriptName;
81
82
        $locker = $this->prophesize(Locker::class);
83
        $getLocker = $this->container->get(Locker::class)->willReturn($locker->reveal());
84
        $addSubscriber = $this->eventManager->addEventSubscriber(Argument::type(LockMigrationsSubscriber::class));
85
86
        ($this->delegator)($this->container->reveal(), '', $this->callback);
87
88
        $getLocker->shouldHaveBeenCalledOnce();
89
        $this->em->getConnection()->shouldHaveBeenCalledOnce();
90
        $this->connection->getEventManager()->shouldHaveBeenCalledOnce();
91
        $addSubscriber->shouldHaveBeenCalledOnce();
92
    }
93
94
    public function provideMigrationsScripts(): iterable
95
    {
96
        return [['doctrine-migrations', 'DOCTRINE-migrations', 'DOCTRINE-MIGRATIONS']];
97
    }
98
}
99