1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Test; |
4
|
|
|
|
5
|
|
|
use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver; |
6
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
8
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
9
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
10
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @internal |
14
|
|
|
* |
15
|
|
|
* @author Kevin Bond <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
final class DatabaseResetter |
18
|
|
|
{ |
19
|
|
|
/** @var bool */ |
20
|
|
|
private static $hasBeenReset = false; |
21
|
|
|
|
22
|
|
|
public static function hasBeenReset(): bool |
23
|
|
|
{ |
24
|
|
|
if (isset($_SERVER['FOUNDRY_DISABLE_DATABASE_RESET'])) { |
25
|
|
|
return true; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return self::$hasBeenReset; |
29
|
|
|
} |
30
|
|
|
|
31
|
427 |
|
public static function isDAMADoctrineTestBundleEnabled(): bool |
32
|
|
|
{ |
33
|
427 |
|
return \class_exists(StaticDriver::class) && StaticDriver::isKeepStaticConnections(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function resetDatabase(KernelInterface $kernel): void |
37
|
|
|
{ |
38
|
|
|
$application = self::createApplication($kernel); |
39
|
|
|
$registry = $kernel->getContainer()->get('doctrine'); |
40
|
|
|
|
41
|
|
|
foreach (self::connectionsToReset($registry) as $connection) { |
|
|
|
|
42
|
|
|
$dropParams = ['--connection' => $connection, '--force' => true]; |
43
|
|
|
|
44
|
|
|
if ('sqlite' !== $registry->getConnection($connection)->getDatabasePlatform()->getName()) { |
45
|
|
|
// sqlite does not support "--if-exists" (ref: https://github.com/doctrine/dbal/pull/2402) |
46
|
|
|
$dropParams['--if-exists'] = true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
self::runCommand($application, 'doctrine:database:drop', $dropParams); |
50
|
|
|
|
51
|
|
|
self::runCommand($application, 'doctrine:database:create', [ |
52
|
|
|
'--connection' => $connection, |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
self::createSchema($application, $registry); |
|
|
|
|
57
|
|
|
|
58
|
|
|
self::$hasBeenReset = true; |
59
|
|
|
} |
60
|
|
|
|
61
|
214 |
|
public static function resetSchema(KernelInterface $kernel): void |
62
|
|
|
{ |
63
|
214 |
|
$application = self::createApplication($kernel); |
64
|
214 |
|
$registry = $kernel->getContainer()->get('doctrine'); |
65
|
|
|
|
66
|
214 |
|
self::dropSchema($application, $registry); |
|
|
|
|
67
|
214 |
|
self::createSchema($application, $registry); |
|
|
|
|
68
|
214 |
|
} |
69
|
|
|
|
70
|
214 |
|
private static function createSchema(Application $application, ManagerRegistry $registry): void |
71
|
|
|
{ |
72
|
214 |
|
foreach (self::objectManagersToReset($registry) as $manager) { |
73
|
214 |
|
self::runCommand($application, 'doctrine:schema:create', [ |
74
|
214 |
|
'--em' => $manager, |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
214 |
|
TestState::bootFromContainer($application->getKernel()->getContainer()); |
79
|
214 |
|
TestState::flushGlobalState(); |
80
|
214 |
|
} |
81
|
|
|
|
82
|
214 |
|
private static function dropSchema(Application $application, ManagerRegistry $registry): void |
83
|
|
|
{ |
84
|
214 |
|
foreach (self::objectManagersToReset($registry) as $manager) { |
85
|
214 |
|
self::runCommand($application, 'doctrine:schema:drop', [ |
86
|
214 |
|
'--em' => $manager, |
87
|
|
|
'--force' => true, |
88
|
|
|
]); |
89
|
|
|
} |
90
|
214 |
|
} |
91
|
|
|
|
92
|
214 |
|
private static function runCommand(Application $application, string $command, array $parameters = []): void |
93
|
|
|
{ |
94
|
214 |
|
$exit = $application->run( |
95
|
214 |
|
new ArrayInput(\array_merge(['command' => $command], $parameters)), |
96
|
214 |
|
$output = new BufferedOutput() |
97
|
|
|
); |
98
|
|
|
|
99
|
214 |
|
if (0 !== $exit) { |
100
|
|
|
throw new \RuntimeException(\sprintf('Error running "%s": %s', $command, $output->fetch())); |
101
|
|
|
} |
102
|
214 |
|
} |
103
|
|
|
|
104
|
214 |
|
private static function createApplication(KernelInterface $kernel): Application |
105
|
|
|
{ |
106
|
214 |
|
$application = new Application($kernel); |
107
|
214 |
|
$application->setAutoExit(false); |
108
|
|
|
|
109
|
214 |
|
return $application; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private static function connectionsToReset(ManagerRegistry $registry): array |
113
|
|
|
{ |
114
|
|
|
if (isset($_SERVER['FOUNDRY_RESET_CONNECTIONS'])) { |
115
|
|
|
return \explode(',', $_SERVER['FOUNDRY_RESET_CONNECTIONS']); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return [$registry->getDefaultConnectionName()]; |
119
|
|
|
} |
120
|
|
|
|
121
|
214 |
|
private static function objectManagersToReset(ManagerRegistry $registry): array |
122
|
|
|
{ |
123
|
214 |
|
if (isset($_SERVER['FOUNDRY_RESET_OBJECT_MANAGERS'])) { |
124
|
|
|
return \explode(',', $_SERVER['FOUNDRY_RESET_OBJECT_MANAGERS']); |
125
|
|
|
} |
126
|
|
|
|
127
|
214 |
|
return [$registry->getDefaultManagerName()]; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|