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