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

Factories::_setUpFactories()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 21
ccs 10
cts 11
cp 0.9091
rs 9.9332
cc 4
nc 3
nop 0
crap 4.0119
1
<?php
2
3
namespace Zenstruck\Foundry\Test;
4
5
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6
use Zenstruck\Foundry\Factory;
7
use Zenstruck\Foundry\StoryManager;
8
9
/**
10
 * @mixin KernelTestCase
11
 *
12
 * @author Kevin Bond <[email protected]>
13
 */
14
trait Factories
15
{
16
    /**
17
     * @internal
18
     * @before
19
     */
20 427
    public static function _setUpFactories(): void
21
    {
22 427
        if (!\is_subclass_of(static::class, KernelTestCase::class)) {
23
            throw new \RuntimeException(\sprintf('The "%s" trait can only be used on TestCases that extend "%s".', __TRAIT__, KernelTestCase::class));
24
        }
25
26 427
        if (!static::$booted) {
27 427
            static::bootKernel();
28
        }
29
30 427
        TestState::bootFromContainer(static::$kernel->getContainer())->setManagerRegistry(
31
            new LazyManagerRegistry(static function() {
32 205
                if (!static::$booted) {
33 205
                    static::bootKernel();
34
                }
35
36 205
                return static::$kernel->getContainer()->get('doctrine');
37 427
            })
38
        );
39
40 427
        self::ensureKernelShutdown();
41 427
    }
42
43
    /**
44
     * @internal
45
     * @after
46
     */
47 427
    public static function _tearDownFactories(): void
48
    {
49 427
        Factory::faker()->unique(true); // reset unique
50 427
        StoryManager::reset();
51 427
    }
52
}
53