Passed
Pull Request — master (#59)
by Kevin
17:37
created

TestState   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 42.11%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 16
eloc 34
c 3
b 0
f 1
dl 0
loc 110
ccs 16
cts 38
cp 0.4211
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setFaker() 0 3 1
A setInstantiator() 0 3 1
A addGlobalState() 0 3 1
A withoutBundle() 0 2 1
A alwaysAutoRefreshProxies() 0 3 1
A bootFoundry() 0 17 4
A shutdownFoundry() 0 4 1
A bootFactory() 0 5 1
A bootFromContainer() 0 17 3
A flushGlobalState() 0 9 2
1
<?php
2
3
namespace Zenstruck\Foundry\Test;
4
5
use Faker;
6
use Psr\Container\ContainerInterface;
7
use Psr\Container\NotFoundExceptionInterface;
8
use Zenstruck\Foundry\Configuration;
9
use Zenstruck\Foundry\Factory;
10
use Zenstruck\Foundry\StoryManager;
11
12
/**
13
 * @author Kevin Bond <[email protected]>
14
 */
15
final class TestState
16
{
17
    /** @var callable|null */
18
    private static $instantiator;
19
20
    /** @var Faker\Generator|null */
21
    private static $faker;
22
23
    /** @var bool */
24
    private static $alwaysAutoRefreshProxies = false;
25
26
    /** @var callable[] */
27
    private static $globalStates = [];
28
29
    public static function setInstantiator(callable $instantiator): void
30
    {
31
        self::$instantiator = $instantiator;
32
    }
33
34
    public static function setFaker(Faker\Generator $faker): void
35
    {
36
        self::$faker = $faker;
37
    }
38
39
    public static function alwaysAutoRefreshProxies(): void
40
    {
41
        self::$alwaysAutoRefreshProxies = true;
42
    }
43
44
    /**
45
     * @deprecated Foundry now auto-detects if the bundle is installed
46
     */
47
    public static function withoutBundle(): void
48
    {
49
    }
50
51
    public static function addGlobalState(callable $callback): void
52
    {
53
        self::$globalStates[] = $callback;
54
    }
55
56
    public static function bootFoundry(?Configuration $configuration = null): void
57
    {
58 433
        $configuration = $configuration ?? new Configuration();
59
60 433
        if (self::$instantiator) {
61
            $configuration->setInstantiator(self::$instantiator);
62
        }
63
64 433
        if (self::$faker) {
65
            $configuration->setFaker(self::$faker);
66
        }
67
68 433
        if (self::$alwaysAutoRefreshProxies) {
69
            $configuration->alwaysAutoRefreshProxies();
70
        }
71
72 433
        Factory::boot($configuration);
73
    }
74 433
75
    public static function shutdownFoundry(): void
76
    {
77
        Factory::shutdown();
78
        StoryManager::reset();
79
    }
80 433
81
    /**
82 433
     * @deprecated use TestState::bootFoundry()
83
     */
84 168
    public static function bootFactory(Configuration $configuration): Configuration
85
    {
86
        self::bootFoundry($configuration);
87
88
        return Factory::configuration();
89
    }
90
91 265
    /**
92
     * @internal
93
     */
94
    public static function bootFromContainer(ContainerInterface $container): void
95
    {
96
        if ($container->has(Configuration::class)) {
97
            self::bootFoundry($container->get(Configuration::class));
98
99
            return;
100 218
        }
101
102 218
        $configuration = new Configuration();
103
104 218
        try {
105 218
            $configuration->setManagerRegistry($container->get('doctrine'));
106
        } catch (NotFoundExceptionInterface $e) {
107
            throw new \LogicException('Could not boot Foundry, is the DoctrineBundle installed/configured?', 0, $e);
108 218
        }
109 218
110
        self::bootFoundry($configuration);
111
    }
112
113
    /**
114
     * @internal
115
     */
116
    public static function flushGlobalState(): void
117
    {
118
        StoryManager::globalReset();
119
120
        foreach (self::$globalStates as $callback) {
121
            $callback();
122
        }
123
124
        StoryManager::setGlobalState();
125
    }
126
}
127