Passed
Pull Request — master (#1)
by Kevin
02:46
created

Factories   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 73.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 37
rs 10
ccs 11
cts 15
cp 0.7332
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A _tearDownFactories() 0 4 1
A _setUpFactories() 0 21 4
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 162
    public static function _setUpFactories(): void
21
    {
22 162
        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 162
        if (!static::$booted) {
27 162
            static::bootKernel();
28
        }
29
30 162
        TestState::bootFromContainer(static::$kernel->getContainer())->setManagerRegistry(
31
            new LazyManagerRegistry(static function() {
32
                if (!static::$booted) {
33
                    static::bootKernel();
34
                }
35
36
                return static::$kernel->getContainer()->get('doctrine');
37 162
            })
38
        );
39
40 162
        self::ensureKernelShutdown();
41 162
    }
42
43
    /**
44
     * @internal
45
     * @after
46
     */
47 162
    public static function _tearDownFactories(): void
48
    {
49 162
        Factory::faker()->unique(true); // reset unique
50 162
        StoryManager::reset();
51 162
    }
52
}
53