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 | $this->dropAndResetDatabase(); |
||
27 | $this->createSchema(); |
||
28 | } |
||
29 | |||
30 | public function resetSchema(): void |
||
31 | { |
||
32 | if (DatabaseResetter::isDAMADoctrineTestBundleEnabled()) { |
||
33 | // not required as the DAMADoctrineTestBundle wraps each test in a transaction |
||
34 | return; |
||
35 | } |
||
36 | |||
37 | $this->dropSchema(); |
||
38 | $this->createSchema(); |
||
39 | } |
||
40 | |||
41 | private function createSchema(): void |
||
42 | { |
||
43 | if (self::isResetUsingMigrations()) { |
||
44 | $this->runCommand($this->application, 'doctrine:migrations:migrate', ['-n' => true]); |
||
45 | |||
46 | return; |
||
47 | } |
||
48 | |||
49 | foreach ($this->objectManagersToReset() as $manager) { |
||
50 | $this->runCommand( |
||
51 | $this->application, |
||
52 | 'doctrine:schema:create', |
||
53 | [ |
||
54 | '--em' => $manager, |
||
55 | ] |
||
56 | ); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | private function dropSchema(): void |
||
61 | { |
||
62 | if (self::isResetUsingMigrations()) { |
||
63 | $this->dropAndResetDatabase(); |
||
64 | |||
65 | return; |
||
66 | } |
||
67 | |||
68 | foreach ($this->objectManagersToReset() as $manager) { |
||
69 | $this->runCommand( |
||
70 | $this->application, |
||
71 | 'doctrine:schema:drop', |
||
72 | [ |
||
73 | '--em' => $manager, |
||
74 | '--force' => true, |
||
75 | ] |
||
76 | ); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | private function dropAndResetDatabase(): void |
||
81 | { |
||
82 | foreach ($this->connectionsToReset() as $connection) { |
||
83 | $dropParams = ['--connection' => $connection, '--force' => true]; |
||
84 | |||
85 | if ('sqlite' !== $this->registry->getConnection($connection)->getDatabasePlatform()->getName()) { |
||
86 | // sqlite does not support "--if-exists" (ref: https://github.com/doctrine/dbal/pull/2402) |
||
87 | $dropParams['--if-exists'] = true; |
||
88 | } |
||
89 | |||
90 | $this->runCommand($this->application, 'doctrine:database:drop', $dropParams); |
||
91 | |||
92 | $this->runCommand( |
||
93 | $this->application, |
||
94 | 'doctrine:database:create', |
||
95 | [ |
||
96 | '--connection' => $connection, |
||
97 | ] |
||
98 | ); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | /** @return list<string> */ |
||
0 ignored issues
–
show
|
|||
103 | private function connectionsToReset(): array |
||
104 | { |
||
105 | if (isset($_SERVER['FOUNDRY_RESET_CONNECTIONS'])) { |
||
106 | return \explode(',', $_SERVER['FOUNDRY_RESET_CONNECTIONS']); |
||
0 ignored issues
–
show
|
|||
107 | } |
||
108 | |||
109 | return [$this->registry->getDefaultConnectionName()]; |
||
0 ignored issues
–
show
|
|||
110 | } |
||
111 | |||
112 | /** @return list<string> */ |
||
113 | private function objectManagersToReset(): array |
||
114 | { |
||
115 | if (isset($_SERVER['FOUNDRY_RESET_OBJECT_MANAGERS'])) { |
||
116 | return \explode(',', $_SERVER['FOUNDRY_RESET_OBJECT_MANAGERS']); |
||
0 ignored issues
–
show
|
|||
117 | } |
||
118 | |||
119 | return [$this->registry->getDefaultManagerName()]; |
||
0 ignored issues
–
show
|
|||
120 | } |
||
121 | |||
122 | private static function isResetUsingMigrations(): bool |
||
123 | { |
||
124 | return 'migrate' === ($_SERVER['FOUNDRY_RESET_MODE'] ?? 'schema'); |
||
125 | } |
||
126 | } |
||
127 |
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