|
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
|
|
|
/** |
|
76
|
|
|
* @deprecated use TestState::bootFoundry() |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function bootFactory(Configuration $configuration): Configuration |
|
79
|
|
|
{ |
|
80
|
433 |
|
self::bootFoundry($configuration); |
|
81
|
|
|
|
|
82
|
433 |
|
return Factory::configuration(); |
|
83
|
|
|
} |
|
84
|
168 |
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @internal |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function bootFromContainer(ContainerInterface $container): void |
|
89
|
|
|
{ |
|
90
|
|
|
if ($container->has(Configuration::class)) { |
|
91
|
265 |
|
self::bootFoundry($container->get(Configuration::class)); |
|
92
|
|
|
|
|
93
|
|
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$configuration = new Configuration(); |
|
97
|
|
|
|
|
98
|
|
|
try { |
|
99
|
|
|
$configuration->setManagerRegistry($container->get('doctrine')); |
|
100
|
218 |
|
} catch (NotFoundExceptionInterface $e) { |
|
101
|
|
|
throw new \LogicException('Could not boot Foundry, is the DoctrineBundle installed/configured?', 0, $e); |
|
102
|
218 |
|
} |
|
103
|
|
|
|
|
104
|
218 |
|
self::bootFoundry($configuration); |
|
105
|
218 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
218 |
|
* @internal |
|
109
|
218 |
|
*/ |
|
110
|
|
|
public static function flushGlobalState(): void |
|
111
|
|
|
{ |
|
112
|
|
|
StoryManager::globalReset(); |
|
113
|
|
|
|
|
114
|
|
|
foreach (self::$globalStates as $callback) { |
|
115
|
|
|
$callback(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
StoryManager::setGlobalState(); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|