ResetDatabase   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 25%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 19
c 3
b 0
f 0
dl 0
loc 50
rs 10
ccs 5
cts 20
cp 0.25

2 Methods

Rating   Name   Duplication   Size   Complexity  
A _resetDatabase() 0 26 5
A _resetSchema() 0 12 2
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