|
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']; |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public static function tearDownAfterClass(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$_SERVER['SCRIPT_NAME'] = static::$originalScriptName; |
|
|
|
|
|
|
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
|
|
|
|