ResetDatabase::_resetDatabase()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 26
ccs 0
cts 13
cp 0
rs 9.5555
cc 5
nc 6
nop 0
crap 30
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
        if ($isDAMADoctrineTestBundleEnabled = DatabaseResetter::isDAMADoctrineTestBundleEnabled()) {
30
            // disable static connections for this operation
31
            StaticDriver::setKeepStaticConnections(false);
32
        }
33
34
        $kernel = static::createKernel();
35
        $kernel->boot();
36
37
        DatabaseResetter::resetDatabase($kernel);
38
39
        if ($isDAMADoctrineTestBundleEnabled) {
40
            // re-enable static connections
41
            StaticDriver::setKeepStaticConnections(true);
42
        }
43
44
        $kernel->shutdown();
45
    }
46
47
    /**
48
     * @internal
49
     * @before
50
     */
51 634
    public static function _resetSchema(): void
52
    {
53 634
        if (!\is_subclass_of(static::class, KernelTestCase::class)) {
54
            throw new \RuntimeException(\sprintf('The "%s" trait can only be used on TestCases that extend "%s".', __TRAIT__, KernelTestCase::class));
55 253
        }
56
57
        $kernel = static::createKernel();
58 381
        $kernel->boot();
59
60
        DatabaseResetter::resetSchema($kernel);
61
62 381
        $kernel->shutdown();
63 381
    }
64
}
65