Passed
Push — master ( 829500...1175ab )
by Kevin
08:08
created

ResetDatabase   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 28.57%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 53
ccs 6
cts 21
cp 0.2857
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A _resetDatabase() 0 25 5
A _resetSchema() 0 16 4
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 38
    public static function _resetSchema(): void
51
    {
52 38
        if (DatabaseResetter::isDAMADoctrineTestBundleEnabled()) {
53
            // not required as the DAMADoctrineTestBundle wraps each test in a transaction
54
            return;
55
        }
56
57 38
        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 38
        if (!static::$booted) {
62 38
            static::bootKernel();
63
        }
64
65 38
        DatabaseResetter::resetSchema(static::$kernel);
66 38
    }
67
}
68