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