1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Test; |
4
|
|
|
|
5
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @internal |
10
|
|
|
*/ |
11
|
|
|
final class ORMDatabaseResetter extends AbstractSchemaResetter |
12
|
|
|
{ |
13
|
|
|
/** @var Application */ |
14
|
|
|
private $application; |
15
|
|
|
/** @var ManagerRegistry */ |
16
|
|
|
private $registry; |
17
|
|
|
|
18
|
|
|
public function __construct(Application $application, ManagerRegistry $registry) |
19
|
|
|
{ |
20
|
|
|
$this->application = $application; |
21
|
|
|
$this->registry = $registry; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function resetDatabase(): void |
25
|
|
|
{ |
26
|
|
|
foreach ($this->connectionsToReset() as $connection) { |
27
|
|
|
$dropParams = ['--connection' => $connection, '--force' => true]; |
28
|
|
|
|
29
|
|
|
if ('sqlite' !== $this->registry->getConnection($connection)->getDatabasePlatform()->getName()) { |
30
|
|
|
// sqlite does not support "--if-exists" (ref: https://github.com/doctrine/dbal/pull/2402) |
31
|
|
|
$dropParams['--if-exists'] = true; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->runCommand($this->application, 'doctrine:database:drop', $dropParams); |
35
|
|
|
|
36
|
|
|
$this->runCommand( |
37
|
|
|
$this->application, |
38
|
|
|
'doctrine:database:create', |
39
|
|
|
[ |
40
|
|
|
'--connection' => $connection, |
41
|
|
|
] |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->createSchema(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function resetSchema(): void |
49
|
|
|
{ |
50
|
|
|
if (DatabaseResetter::isDAMADoctrineTestBundleEnabled()) { |
51
|
|
|
// not required as the DAMADoctrineTestBundle wraps each test in a transaction |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->dropSchema(); |
56
|
|
|
$this->createSchema(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function createSchema(): void |
60
|
|
|
{ |
61
|
|
|
if (self::isResetUsingMigrations()) { |
62
|
|
|
$this->runCommand($this->application, 'doctrine:migrations:migrate', ['-n' => true]); |
63
|
|
|
|
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
foreach ($this->objectManagersToReset() as $manager) { |
68
|
|
|
$this->runCommand( |
69
|
|
|
$this->application, |
70
|
|
|
'doctrine:schema:create', |
71
|
|
|
[ |
72
|
|
|
'--em' => $manager, |
73
|
|
|
] |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function dropSchema(): void |
79
|
|
|
{ |
80
|
|
|
if (self::isResetUsingMigrations()) { |
81
|
|
|
$this->resetDatabase(); |
82
|
|
|
|
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach ($this->objectManagersToReset() as $manager) { |
87
|
|
|
$this->runCommand( |
88
|
|
|
$this->application, |
89
|
|
|
'doctrine:schema:drop', |
90
|
|
|
[ |
91
|
|
|
'--em' => $manager, |
92
|
|
|
'--force' => true, |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** @return list<string> */ |
|
|
|
|
99
|
|
|
private function connectionsToReset(): array |
100
|
|
|
{ |
101
|
|
|
if (isset($_SERVER['FOUNDRY_RESET_CONNECTIONS'])) { |
102
|
|
|
return \explode(',', $_SERVER['FOUNDRY_RESET_CONNECTIONS']); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return [$this->registry->getDefaultConnectionName()]; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** @return list<string> */ |
109
|
|
|
private function objectManagersToReset(): array |
110
|
|
|
{ |
111
|
|
|
if (isset($_SERVER['FOUNDRY_RESET_OBJECT_MANAGERS'])) { |
112
|
|
|
return \explode(',', $_SERVER['FOUNDRY_RESET_OBJECT_MANAGERS']); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return [$this->registry->getDefaultManagerName()]; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private static function isResetUsingMigrations(): bool |
119
|
|
|
{ |
120
|
|
|
return 'migrate' === ($_SERVER['FOUNDRY_RESET_MODE'] ?? 'schema'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths