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
|
|
|
private static bool $hasBeenReset = false; |
20
|
|
|
|
21
|
|
|
public static function hasBeenReset(): bool |
22
|
|
|
{ |
23
|
|
|
if (isset($_SERVER['FOUNDRY_DISABLE_DATABASE_RESET'])) { |
24
|
|
|
return true; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return self::$hasBeenReset; |
28
|
|
|
} |
29
|
|
|
|
30
|
154 |
|
public static function isDAMADoctrineTestBundleEnabled(): bool |
31
|
|
|
{ |
32
|
154 |
|
return \class_exists(StaticDriver::class) && StaticDriver::isKeepStaticConnections(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function resetDatabase(KernelInterface $kernel): void |
36
|
|
|
{ |
37
|
|
|
$application = self::createApplication($kernel); |
38
|
|
|
$registry = $kernel->getContainer()->get('doctrine'); |
39
|
|
|
|
40
|
|
|
foreach (self::connectionsToReset($registry) as $connection) { |
|
|
|
|
41
|
|
|
self::runCommand($application, 'doctrine:database:drop', [ |
42
|
|
|
'--connection' => $connection, |
43
|
|
|
'--if-exists' => true, |
44
|
|
|
'--force' => true, |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
self::runCommand($application, 'doctrine:database:create', [ |
48
|
|
|
'--connection' => $connection, |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
self::createSchema($application, $registry); |
|
|
|
|
53
|
|
|
|
54
|
|
|
self::$hasBeenReset = true; |
55
|
|
|
} |
56
|
|
|
|
57
|
154 |
|
public static function resetSchema(KernelInterface $kernel): void |
58
|
|
|
{ |
59
|
154 |
|
$application = self::createApplication($kernel); |
60
|
154 |
|
$registry = $kernel->getContainer()->get('doctrine'); |
61
|
|
|
|
62
|
154 |
|
self::dropSchema($application, $registry); |
|
|
|
|
63
|
154 |
|
self::createSchema($application, $registry); |
|
|
|
|
64
|
154 |
|
} |
65
|
|
|
|
66
|
154 |
|
private static function createSchema(Application $application, ManagerRegistry $registry): void |
67
|
|
|
{ |
68
|
154 |
|
foreach (self::objectManagersToReset($registry) as $manager) { |
69
|
154 |
|
self::runCommand($application, 'doctrine:schema:create', [ |
70
|
154 |
|
'--em' => $manager, |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
154 |
|
TestState::bootFromContainer($application->getKernel()->getContainer()); |
75
|
154 |
|
TestState::flushGlobalState(); |
76
|
154 |
|
} |
77
|
|
|
|
78
|
154 |
|
private static function dropSchema(Application $application, ManagerRegistry $registry): void |
79
|
|
|
{ |
80
|
154 |
|
foreach (self::objectManagersToReset($registry) as $manager) { |
81
|
154 |
|
self::runCommand($application, 'doctrine:schema:drop', [ |
82
|
154 |
|
'--em' => $manager, |
83
|
|
|
'--force' => true, |
84
|
|
|
]); |
85
|
|
|
} |
86
|
154 |
|
} |
87
|
|
|
|
88
|
154 |
|
private static function runCommand(Application $application, string $command, array $parameters = []): void |
89
|
|
|
{ |
90
|
154 |
|
$exit = $application->run( |
91
|
154 |
|
new ArrayInput(\array_merge(['command' => $command], $parameters)), |
92
|
154 |
|
$output = new BufferedOutput() |
93
|
|
|
); |
94
|
|
|
|
95
|
154 |
|
if (0 !== $exit) { |
96
|
|
|
throw new \RuntimeException(\sprintf('Error running "%s": %s', $command, $output->fetch())); |
97
|
|
|
} |
98
|
154 |
|
} |
99
|
|
|
|
100
|
154 |
|
private static function createApplication(KernelInterface $kernel): Application |
101
|
|
|
{ |
102
|
154 |
|
$application = new Application($kernel); |
103
|
154 |
|
$application->setAutoExit(false); |
104
|
|
|
|
105
|
154 |
|
return $application; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private static function connectionsToReset(ManagerRegistry $registry): array |
109
|
|
|
{ |
110
|
|
|
if (isset($_SERVER['FOUNDRY_RESET_CONNECTIONS'])) { |
111
|
|
|
return \explode(',', $_SERVER['FOUNDRY_RESET_CONNECTIONS']); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return [$registry->getDefaultConnectionName()]; |
115
|
|
|
} |
116
|
|
|
|
117
|
154 |
|
private static function objectManagersToReset(ManagerRegistry $registry): array |
118
|
|
|
{ |
119
|
154 |
|
if (isset($_SERVER['FOUNDRY_RESET_OBJECT_MANAGERS'])) { |
120
|
|
|
return \explode(',', $_SERVER['FOUNDRY_RESET_OBJECT_MANAGERS']); |
121
|
|
|
} |
122
|
|
|
|
123
|
154 |
|
return [$registry->getDefaultManagerName()]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|