Passed
Pull Request — master (#1)
by Kevin
12:50
created

DatabaseResetter   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 61.11%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 108
rs 10
ccs 33
cts 54
cp 0.6111
wmc 18

10 Methods

Rating   Name   Duplication   Size   Complexity  
A objectManagersToReset() 0 7 2
A runCommand() 0 9 2
A dropSchema() 0 6 2
A createApplication() 0 6 1
A resetSchema() 0 7 1
A isDAMADoctrineTestBundleEnabled() 0 3 2
A resetDatabase() 0 20 2
A createSchema() 0 10 2
A connectionsToReset() 0 7 2
A hasBeenReset() 0 7 2
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
    /** @var bool */
20
    private static $hasBeenReset = false;
21
22
    public static function hasBeenReset(): bool
23
    {
24
        if (isset($_SERVER['FOUNDRY_DISABLE_DATABASE_RESET'])) {
25
            return true;
26
        }
27
28
        return self::$hasBeenReset;
29
    }
30
31 162
    public static function isDAMADoctrineTestBundleEnabled(): bool
32
    {
33 162
        return \class_exists(StaticDriver::class) && StaticDriver::isKeepStaticConnections();
34
    }
35
36
    public static function resetDatabase(KernelInterface $kernel): void
37
    {
38
        $application = self::createApplication($kernel);
39
        $registry = $kernel->getContainer()->get('doctrine');
40
41
        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

41
        foreach (self::connectionsToReset(/** @scrutinizer ignore-type */ $registry) as $connection) {
Loading history...
42
            self::runCommand($application, 'doctrine:database:drop', [
43
                '--connection' => $connection,
44
                '--if-exists' => true,
45
                '--force' => true,
46
            ]);
47
48
            self::runCommand($application, 'doctrine:database:create', [
49
                '--connection' => $connection,
50
            ]);
51
        }
52
53
        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

53
        self::createSchema($application, /** @scrutinizer ignore-type */ $registry);
Loading history...
54
55
        self::$hasBeenReset = true;
56
    }
57
58 162
    public static function resetSchema(KernelInterface $kernel): void
59
    {
60 162
        $application = self::createApplication($kernel);
61 162
        $registry = $kernel->getContainer()->get('doctrine');
62
63 162
        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

63
        self::dropSchema($application, /** @scrutinizer ignore-type */ $registry);
Loading history...
64 162
        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

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