Passed
Push — master ( 71f08a...6052e8 )
by Kevin
04:39
created

DatabaseResetter::bootFoundry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 3
c 1
b 1
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
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'));
0 ignored issues
show
Bug introduced by
It seems like $kernel->getContainer()->get('doctrine') can also be of type null; however, parameter $registry of Zenstruck\Foundry\Test\O...Resetter::__construct() 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
        $databaseResetter = new ORMDatabaseResetter($application, /** @scrutinizer ignore-type */ $kernel->getContainer()->get('doctrine'));
Loading history...
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'));
0 ignored issues
show
Bug introduced by
It seems like $kernel->getContainer()->get('doctrine') can also be of type null; however, parameter $registry of Zenstruck\Foundry\Test\O...Resetter::__construct() 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

70
            $databaseResetters[] = new ORMDatabaseResetter($application, /** @scrutinizer ignore-type */ $kernel->getContainer()->get('doctrine'));
Loading history...
71 381
        }
72
73 381
        if ($kernel->getContainer()->has('doctrine_mongodb')) {
74 381
            $databaseResetters[] = new ODMSchemaResetter($application, $kernel->getContainer()->get('doctrine_mongodb'));
0 ignored issues
show
Bug introduced by
It seems like $kernel->getContainer()->get('doctrine_mongodb') can also be of type null; however, parameter $registry of Zenstruck\Foundry\Test\O...Resetter::__construct() 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

74
            $databaseResetters[] = new ODMSchemaResetter($application, /** @scrutinizer ignore-type */ $kernel->getContainer()->get('doctrine_mongodb'));
Loading history...
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