Passed
Pull Request — master (#131)
by Kevin
02:54
created

TestState::disableDefaultProxyAutoRefresh()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 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|null */
24
    private static $alwaysAutoRefreshProxies;
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 enableDefaultProxyAutoRefresh(): void
40
    {
41
        self::$alwaysAutoRefreshProxies = true;
42
    }
43
44
    public static function disableDefaultProxyAutoRefresh(): void
45
    {
46
        self::$alwaysAutoRefreshProxies = false;
47
    }
48
49
    /**
50
     * @deprecated Use TestState::enableDefaultProxyAutoRefresh
51
     */
52
    public static function alwaysAutoRefreshProxies(): void
53
    {
54
        trigger_deprecation('zenstruck\foundry', '1.9', 'TestState::alwaysAutoRefreshProxies() is deprecated, use TestState::enableDefaultProxyAutoRefresh().');
55
56
        self::enableDefaultProxyAutoRefresh();
57 964
    }
58
59 964
    /**
60
     * @deprecated Foundry now auto-detects if the bundle is installed
61 964
     */
62
    public static function withoutBundle(): void
63
    {
64
        trigger_deprecation('zenstruck\foundry', '1.4.0', 'TestState::withoutBundle() is deprecated, the bundle is now auto-detected.');
65 964
    }
66
67
    public static function addGlobalState(callable $callback): void
68
    {
69 964
        self::$globalStates[] = $callback;
70
    }
71
72
    public static function bootFoundry(?Configuration $configuration = null): void
73 964
    {
74 964
        $configuration = $configuration ?? new Configuration();
75
76 964
        if (self::$instantiator) {
77
            $configuration->setInstantiator(self::$instantiator);
78 964
        }
79 964
80 964
        if (self::$faker) {
81
            $configuration->setFaker(self::$faker);
82
        }
83
84
        if (true === self::$alwaysAutoRefreshProxies) {
85
            $configuration->enableDefaultProxyAutoRefresh();
86
        } elseif (false === self::$alwaysAutoRefreshProxies) {
87
            $configuration->disableDefaultProxyAutoRefresh();
88
        }
89
90
        Factory::boot($configuration);
91
    }
92
93
    public static function shutdownFoundry(): void
94
    {
95
        Factory::shutdown();
96
        StoryManager::reset();
97 634
    }
98
99 634
    /**
100 256
     * @deprecated use TestState::bootFoundry()
101
     */
102 256
    public static function bootFactory(Configuration $configuration): Configuration
103
    {
104
        trigger_deprecation('zenstruck\foundry', '1.4.0', 'TestState::bootFactory() is deprecated, use TestState::bootFoundry().');
105 378
106
        self::bootFoundry($configuration);
107
108 378
        return Factory::configuration();
109
    }
110
111
    /**
112
     * @internal
113 378
     */
114 378
    public static function bootFromContainer(ContainerInterface $container): void
115
    {
116
        if ($container->has(Configuration::class)) {
117
            self::bootFoundry($container->get(Configuration::class));
118
119 381
            return;
120
        }
121 381
122
        $configuration = new Configuration();
123 381
124 381
        try {
125
            $configuration->setManagerRegistry($container->get('doctrine'));
126
        } catch (NotFoundExceptionInterface $e) {
127 381
            throw new \LogicException('Could not boot Foundry, is the DoctrineBundle installed/configured?', 0, $e);
128 381
        }
129
130
        self::bootFoundry($configuration);
131
    }
132
133
    /**
134
     * @internal
135
     */
136
    public static function flushGlobalState(): void
137
    {
138
        StoryManager::globalReset();
139
140
        foreach (self::$globalStates as $callback) {
141
            $callback();
142
        }
143
144
        StoryManager::setGlobalState();
145
    }
146
}
147