Passed
Push — master ( e7b848...449b81 )
by Kevin
03:16 queued 10s
created

ResetDatabase::_resetSchema()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 16
ccs 7
cts 8
cp 0.875
rs 10
cc 4
nc 4
nop 0
crap 4.0312
1
<?php
2
3
namespace Zenstruck\Foundry\Test;
4
5
use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver;
6
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
7
8
/**
9
 * @mixin KernelTestCase
10
 *
11
 * @author Kevin Bond <[email protected]>
12
 */
13
trait ResetDatabase
14
{
15
    /**
16
     * @internal
17
     * @beforeClass
18
     */
19
    public static function _resetDatabase(): void
20
    {
21
        if (DatabaseResetter::hasBeenReset()) {
22
            return;
23
        }
24
25
        if (!\is_subclass_of(static::class, KernelTestCase::class)) {
26
            throw new \RuntimeException(\sprintf('The "%s" trait can only be used on TestCases that extend "%s".', __TRAIT__, KernelTestCase::class));
27
        }
28
29
        static::ensureKernelShutdown();
30
31
        if ($isDAMADoctrineTestBundleEnabled = DatabaseResetter::isDAMADoctrineTestBundleEnabled()) {
32
            // disable static connections for this operation
33
            StaticDriver::setKeepStaticConnections(false);
34
        }
35
36
        DatabaseResetter::resetDatabase(static::bootKernel());
37
38
        if ($isDAMADoctrineTestBundleEnabled) {
39
            // re-enable static connections
40
            StaticDriver::setKeepStaticConnections(true);
41
        }
42
43
        static::ensureKernelShutdown();
44
    }
45
46
    /**
47
     * @internal
48
     * @before
49
     */
50 427
    public static function _resetSchema(): void
51
    {
52 427
        if (DatabaseResetter::isDAMADoctrineTestBundleEnabled()) {
53
            // not required as the DAMADoctrineTestBundle wraps each test in a transaction
54 213
            return;
55
        }
56
57 214
        if (!\is_subclass_of(static::class, KernelTestCase::class)) {
58
            throw new \RuntimeException(\sprintf('The "%s" trait can only be used on TestCases that extend "%s".', __TRAIT__, KernelTestCase::class));
59
        }
60
61 214
        if (!static::$booted) {
62 214
            static::bootKernel();
63
        }
64
65 214
        DatabaseResetter::resetSchema(static::$kernel);
66 214
    }
67
}
68