Passed
Push — master ( 829500...1175ab )
by Kevin
08:08
created

DatabaseResetter::resetSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
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 38
    public static function isDAMADoctrineTestBundleEnabled(): bool
31
    {
32 38
        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) {
0 ignored issues
show
Bug introduced by
It seems like $registry can also be of type null; however, parameter $registry of Zenstruck\Foundry\Test\D...r::connectionsToReset() does only seem to accept Doctrine\Persistence\ManagerRegistry, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        foreach (self::connectionsToReset(/** @scrutinizer ignore-type */ $registry) as $connection) {
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $registry can also be of type null; however, parameter $registry of Zenstruck\Foundry\Test\D...esetter::createSchema() does only seem to accept Doctrine\Persistence\ManagerRegistry, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        self::createSchema($application, /** @scrutinizer ignore-type */ $registry);
Loading history...
53
54
        self::$hasBeenReset = true;
55
    }
56
57 38
    public static function resetSchema(KernelInterface $kernel): void
58
    {
59 38
        $application = self::createApplication($kernel);
60 38
        $registry = $kernel->getContainer()->get('doctrine');
61
62 38
        self::dropSchema($application, $registry);
0 ignored issues
show
Bug introduced by
It seems like $registry can also be of type null; however, parameter $registry of Zenstruck\Foundry\Test\D...eResetter::dropSchema() does only seem to accept Doctrine\Persistence\ManagerRegistry, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        self::dropSchema($application, /** @scrutinizer ignore-type */ $registry);
Loading history...
63 38
        self::createSchema($application, $registry);
0 ignored issues
show
Bug introduced by
It seems like $registry can also be of type null; however, parameter $registry of Zenstruck\Foundry\Test\D...esetter::createSchema() does only seem to accept Doctrine\Persistence\ManagerRegistry, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        self::createSchema($application, /** @scrutinizer ignore-type */ $registry);
Loading history...
64 38
    }
65
66 38
    private static function createSchema(Application $application, ManagerRegistry $registry): void
67
    {
68 38
        foreach (self::objectManagersToReset($registry) as $manager) {
69 38
            self::runCommand($application, 'doctrine:schema:create', [
70 38
                '--em' => $manager,
71
            ]);
72
        }
73
74 38
        GlobalState::flush($registry);
75 38
    }
76
77 38
    private static function dropSchema(Application $application, ManagerRegistry $registry): void
78
    {
79 38
        foreach (self::objectManagersToReset($registry) as $manager) {
80 38
            self::runCommand($application, 'doctrine:schema:drop', [
81 38
                '--em' => $manager,
82
                '--force' => true,
83
            ]);
84
        }
85 38
    }
86
87 38
    private static function runCommand(Application $application, string $command, array $parameters = []): void
88
    {
89 38
        $exit = $application->run(
90 38
            new ArrayInput(\array_merge(['command' => $command], $parameters)),
91 38
            $output = new BufferedOutput()
92
        );
93
94 38
        if (0 !== $exit) {
95
            throw new \RuntimeException(\sprintf('Error running "%s": %s', $command, $output->fetch()));
96
        }
97 38
    }
98
99 38
    private static function createApplication(KernelInterface $kernel): Application
100
    {
101 38
        $application = new Application($kernel);
102 38
        $application->setAutoExit(false);
103
104 38
        return $application;
105
    }
106
107
    private static function connectionsToReset(ManagerRegistry $registry): array
108
    {
109
        if (isset($_SERVER['FOUNDRY_RESET_CONNECTIONS'])) {
110
            return \explode(',', $_SERVER['FOUNDRY_RESET_CONNECTIONS']);
111
        }
112
113
        return [$registry->getDefaultConnectionName()];
114
    }
115
116 38
    private static function objectManagersToReset(ManagerRegistry $registry): array
117
    {
118 38
        if (isset($_SERVER['FOUNDRY_RESET_OBJECT_MANAGERS'])) {
119
            return \explode(',', $_SERVER['FOUNDRY_RESET_OBJECT_MANAGERS']);
120
        }
121
122 38
        return [$registry->getDefaultManagerName()];
123
    }
124
}
125