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