1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Tests\Unit\Bundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Faker\Generator; |
6
|
|
|
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; |
7
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
8
|
|
|
use Zenstruck\Foundry\Bundle\DependencyInjection\ZenstruckFoundryExtension; |
9
|
|
|
use Zenstruck\Foundry\Bundle\Maker\MakeFactory; |
10
|
|
|
use Zenstruck\Foundry\Bundle\Maker\MakeStory; |
11
|
|
|
use Zenstruck\Foundry\ChainManagerRegistry; |
12
|
|
|
use Zenstruck\Foundry\Configuration; |
13
|
|
|
use Zenstruck\Foundry\Instantiator; |
14
|
|
|
use Zenstruck\Foundry\ModelFactoryManager; |
15
|
|
|
use Zenstruck\Foundry\StoryManager; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Kevin Bond <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class ZenstruckFoundryExtensionTest extends AbstractExtensionTestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @test |
24
|
|
|
*/ |
25
|
|
|
public function default_config(): void |
26
|
|
|
{ |
27
|
|
|
$this->load(); |
28
|
|
|
|
29
|
|
|
$this->assertContainerBuilderHasService(Configuration::class); |
30
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(Configuration::class, 'setInstantiator', ['zenstruck_foundry.default_instantiator']); |
31
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(Configuration::class, 'setFaker', ['zenstruck_foundry.faker']); |
32
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(Configuration::class, 'setManagerRegistry', [ChainManagerRegistry::class]); |
33
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(Configuration::class, 'setStoryManager', [StoryManager::class]); |
34
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(Configuration::class, 'setModelFactoryManager', [ModelFactoryManager::class]); |
35
|
|
|
$this->assertCount(5, $this->container->findDefinition(Configuration::class)->getMethodCalls()); |
36
|
|
|
$this->assertTrue($this->container->getDefinition(Configuration::class)->isPublic()); |
37
|
|
|
$this->assertContainerBuilderHasService('zenstruck_foundry.default_instantiator', Instantiator::class); |
38
|
|
|
$this->assertEmpty($this->container->getDefinition('zenstruck_foundry.default_instantiator')->getMethodCalls()); |
39
|
|
|
$this->assertContainerBuilderHasService('zenstruck_foundry.faker', Generator::class); |
40
|
|
|
$this->assertEmpty($this->container->getDefinition('zenstruck_foundry.faker')->getArguments()); |
41
|
|
|
$this->assertContainerBuilderHasService(StoryManager::class); |
42
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithTag(MakeFactory::class, 'maker.command'); |
43
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithTag(MakeStory::class, 'maker.command'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @test |
48
|
|
|
*/ |
49
|
|
|
public function custom_faker_locale(): void |
50
|
|
|
{ |
51
|
|
|
$this->load(['faker' => ['locale' => 'fr_FR']]); |
52
|
|
|
|
53
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithArgument('zenstruck_foundry.faker', 0, 'fr_FR'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @test |
58
|
|
|
*/ |
59
|
|
|
public function custom_faker_service(): void |
60
|
|
|
{ |
61
|
|
|
$this->load(['faker' => ['service' => 'my_faker']]); |
62
|
|
|
|
63
|
|
|
$this->assertContainerBuilderHasService(Configuration::class); |
64
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(Configuration::class, 'setFaker', ['zenstruck_foundry.faker']); |
65
|
|
|
$this->assertContainerBuilderHasAlias('zenstruck_foundry.faker', 'my_faker'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @test |
70
|
|
|
*/ |
71
|
|
|
public function cannot_set_faker_locale_and_service(): void |
72
|
|
|
{ |
73
|
|
|
$this->expectException(InvalidConfigurationException::class); |
74
|
|
|
$this->expectExceptionMessage('Invalid configuration for path "zenstruck_foundry.faker": Cannot set faker locale when using custom service.'); |
75
|
|
|
|
76
|
|
|
$this->load(['faker' => ['service' => 'my_faker', 'locale' => 'fr_FR']]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @test |
81
|
|
|
*/ |
82
|
|
|
public function custom_instantiator_config(): void |
83
|
|
|
{ |
84
|
|
|
$this->load(['instantiator' => [ |
85
|
|
|
'without_constructor' => true, |
86
|
|
|
'allow_extra_attributes' => true, |
87
|
|
|
'always_force_properties' => true, |
88
|
|
|
]]); |
89
|
|
|
|
90
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall('zenstruck_foundry.default_instantiator', 'withoutConstructor'); |
91
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall('zenstruck_foundry.default_instantiator', 'allowExtraAttributes'); |
92
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall('zenstruck_foundry.default_instantiator', 'alwaysForceProperties'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @test |
97
|
|
|
*/ |
98
|
|
|
public function custom_instantiator_service(): void |
99
|
|
|
{ |
100
|
|
|
$this->load(['instantiator' => ['service' => 'my_instantiator']]); |
101
|
|
|
|
102
|
|
|
$this->assertContainerBuilderHasService(Configuration::class); |
103
|
|
|
$this->assertContainerBuilderHasAlias('zenstruck_foundry.default_instantiator', 'my_instantiator'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @test |
108
|
|
|
*/ |
109
|
|
|
public function cannot_configure_allow_extra_attributes_if_using_custom_instantiator_service(): void |
110
|
|
|
{ |
111
|
|
|
$this->expectException(InvalidConfigurationException::class); |
112
|
|
|
$this->expectExceptionMessage('Invalid configuration for path "zenstruck_foundry.instantiator": Cannot set "allow_extra_attributes" when using custom service.'); |
113
|
|
|
|
114
|
|
|
$this->load(['instantiator' => ['service' => 'my_instantiator', 'allow_extra_attributes' => true]]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @test |
119
|
|
|
*/ |
120
|
|
|
public function cannot_configure_without_constructor_if_using_custom_instantiator_service(): void |
121
|
|
|
{ |
122
|
|
|
$this->expectException(InvalidConfigurationException::class); |
123
|
|
|
$this->expectExceptionMessage('Invalid configuration for path "zenstruck_foundry.instantiator": Cannot set "without_constructor" when using custom service.'); |
124
|
|
|
|
125
|
|
|
$this->load(['instantiator' => ['service' => 'my_instantiator', 'without_constructor' => true]]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @test |
130
|
|
|
*/ |
131
|
|
|
public function cannot_configure_always_force_properties_if_using_custom_instantiator_service(): void |
132
|
|
|
{ |
133
|
|
|
$this->expectException(InvalidConfigurationException::class); |
134
|
|
|
$this->expectExceptionMessage('Invalid configuration for path "zenstruck_foundry.instantiator": Cannot set "always_force_properties" when using custom service.'); |
135
|
|
|
|
136
|
|
|
$this->load(['instantiator' => ['service' => 'my_instantiator', 'always_force_properties' => true]]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @test |
141
|
|
|
*/ |
142
|
|
|
public function can_enable_auto_refresh_proxies(): void |
143
|
|
|
{ |
144
|
|
|
$this->load(['auto_refresh_proxies' => true]); |
145
|
|
|
|
146
|
|
|
$this->assertContainerBuilderHasService(Configuration::class); |
147
|
|
|
$this->assertCount(6, $this->container->findDefinition(Configuration::class)->getMethodCalls()); |
148
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(Configuration::class, 'enableDefaultProxyAutoRefresh', []); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @test |
153
|
|
|
*/ |
154
|
|
|
public function can_disable_auto_refresh_proxies(): void |
155
|
|
|
{ |
156
|
|
|
$this->load(['auto_refresh_proxies' => false]); |
157
|
|
|
|
158
|
|
|
$this->assertContainerBuilderHasService(Configuration::class); |
159
|
|
|
$this->assertCount(6, $this->container->findDefinition(Configuration::class)->getMethodCalls()); |
160
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(Configuration::class, 'disableDefaultProxyAutoRefresh', []); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
protected function getContainerExtensions(): array |
164
|
|
|
{ |
165
|
|
|
return [new ZenstruckFoundryExtension()]; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|