|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Test; |
|
4
|
|
|
|
|
5
|
|
|
use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
|
7
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
8
|
|
|
use Zenstruck\Foundry\Factory; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @internal |
|
12
|
|
|
* |
|
13
|
|
|
* @author Kevin Bond <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
final class DatabaseResetter |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var bool */ |
|
18
|
|
|
private static $hasBeenReset = false; |
|
19
|
|
|
|
|
20
|
|
|
public static function hasBeenReset(): bool |
|
21
|
|
|
{ |
|
22
|
|
|
if (isset($_SERVER['FOUNDRY_DISABLE_DATABASE_RESET'])) { |
|
23
|
|
|
return true; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return self::$hasBeenReset; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function isDAMADoctrineTestBundleEnabled(): bool |
|
30
|
|
|
{ |
|
31
|
|
|
return \class_exists(StaticDriver::class) && StaticDriver::isKeepStaticConnections(); |
|
32
|
634 |
|
} |
|
33
|
|
|
|
|
34
|
634 |
|
public static function resetDatabase(KernelInterface $kernel): void |
|
35
|
|
|
{ |
|
36
|
|
|
if (!$kernel->getContainer()->has('doctrine')) { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$application = self::createApplication($kernel); |
|
41
|
|
|
$databaseResetter = new ORMDatabaseResetter($application, $kernel->getContainer()->get('doctrine')); |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
$databaseResetter->resetDatabase(); |
|
44
|
|
|
|
|
45
|
|
|
self::bootFoundry($kernel); |
|
46
|
|
|
|
|
47
|
|
|
self::$hasBeenReset = true; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public static function resetSchema(KernelInterface $kernel): void |
|
51
|
|
|
{ |
|
52
|
|
|
foreach (self::schemaResetters($kernel) as $databaseResetter) { |
|
53
|
|
|
$databaseResetter->resetSchema(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (self::isDAMADoctrineTestBundleEnabled()) { |
|
57
|
|
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
self::bootFoundry($kernel); |
|
61
|
|
|
} |
|
62
|
381 |
|
|
|
63
|
|
|
/** @retrun array<SchemaResetterInterface> */ |
|
64
|
381 |
|
private static function schemaResetters(KernelInterface $kernel): array |
|
65
|
381 |
|
{ |
|
66
|
|
|
$application = self::createApplication($kernel); |
|
67
|
381 |
|
$databaseResetters = []; |
|
68
|
381 |
|
|
|
69
|
381 |
|
if ($kernel->getContainer()->has('doctrine')) { |
|
70
|
|
|
$databaseResetters[] = new ORMDatabaseResetter($application, $kernel->getContainer()->get('doctrine')); |
|
|
|
|
|
|
71
|
381 |
|
} |
|
72
|
|
|
|
|
73
|
381 |
|
if ($kernel->getContainer()->has('doctrine_mongodb')) { |
|
74
|
381 |
|
$databaseResetters[] = new ODMSchemaResetter($application, $kernel->getContainer()->get('doctrine_mongodb')); |
|
|
|
|
|
|
75
|
381 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $databaseResetters; |
|
78
|
|
|
} |
|
79
|
381 |
|
|
|
80
|
186 |
|
private static function bootFoundry(KernelInterface $kernel): void |
|
81
|
|
|
{ |
|
82
|
|
|
if (!Factory::isBooted()) { |
|
83
|
381 |
|
TestState::bootFromContainer($kernel->getContainer()); |
|
84
|
381 |
|
} |
|
85
|
|
|
|
|
86
|
381 |
|
TestState::flushGlobalState(); |
|
87
|
|
|
} |
|
88
|
381 |
|
|
|
89
|
381 |
|
private static function createApplication(KernelInterface $kernel): Application |
|
90
|
381 |
|
{ |
|
91
|
|
|
$application = new Application($kernel); |
|
92
|
|
|
$application->setAutoExit(false); |
|
93
|
|
|
|
|
94
|
381 |
|
return $application; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|