Completed
Push — master ( 4a56be...71f08a )
by Kevin
27s queued 12s
created

DatabaseResetter::flushGlobalState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 2
c 1
b 1
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
9
/**
10
 * @internal
11
 *
12
 * @author Kevin Bond <[email protected]>
13
 */
14
final class DatabaseResetter
15
{
16
    /** @var bool */
17
    private static $hasBeenReset = false;
18
19
    public static function hasBeenReset(): bool
20
    {
21
        if (isset($_SERVER['FOUNDRY_DISABLE_DATABASE_RESET'])) {
22
            return true;
23
        }
24
25
        return self::$hasBeenReset;
26
    }
27
28
    public static function isDAMADoctrineTestBundleEnabled(): bool
29
    {
30
        return \class_exists(StaticDriver::class) && StaticDriver::isKeepStaticConnections();
31
    }
32 634
33
    public static function resetDatabase(KernelInterface $kernel): void
34 634
    {
35
        if (!$kernel->getContainer()->has('doctrine')) {
36
            return;
37
        }
38
39
        $application = self::createApplication($kernel);
40
        $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

40
        $databaseResetter = new ORMDatabaseResetter($application, /** @scrutinizer ignore-type */ $kernel->getContainer()->get('doctrine'));
Loading history...
41
42
        $databaseResetter->resetDatabase();
43
44
        self::flushGlobalState($kernel);
45
46
        self::$hasBeenReset = true;
47
    }
48
49
    public static function resetSchema(KernelInterface $kernel): void
50
    {
51
        foreach (self::schemaResetters($kernel) as $databaseResetter) {
52
            $databaseResetter->resetSchema();
53
        }
54
55
        if (self::isDAMADoctrineTestBundleEnabled()) {
56
            return;
57
        }
58
59
        self::flushGlobalState($kernel);
60
    }
61
62 381
    /** @retrun array<SchemaResetterInterface> */
63
    private static function schemaResetters(KernelInterface $kernel): array
64 381
    {
65 381
        $application = self::createApplication($kernel);
66
        $databaseResetters = [];
67 381
68 381
        if ($kernel->getContainer()->has('doctrine')) {
69 381
            $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

69
            $databaseResetters[] = new ORMDatabaseResetter($application, /** @scrutinizer ignore-type */ $kernel->getContainer()->get('doctrine'));
Loading history...
70
        }
71 381
72
        if ($kernel->getContainer()->has('doctrine_mongodb')) {
73 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

73
            $databaseResetters[] = new ODMSchemaResetter($application, /** @scrutinizer ignore-type */ $kernel->getContainer()->get('doctrine_mongodb'));
Loading history...
74 381
        }
75 381
76
        return $databaseResetters;
77
    }
78
79 381
    private static function flushGlobalState(KernelInterface $kernel): void
80 186
    {
81
        TestState::bootFromContainer($kernel->getContainer());
82
83 381
        TestState::flushGlobalState();
84 381
    }
85
86 381
    private static function createApplication(KernelInterface $kernel): Application
87
    {
88 381
        $application = new Application($kernel);
89 381
        $application->setAutoExit(false);
90 381
91
        return $application;
92
    }
93
}
94