Completed
Pull Request — master (#87)
by Tomasz
05:51
created

testCustomLoggerFormatterCanBeSet()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
4
namespace League\Tactician\Bundle\Tests\DependencyInjection;
5
6
use League\Tactician\Bundle\DependencyInjection\Configuration;
7
use Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait;
8
use PHPUnit\Framework\TestCase;
9
10
class ConfigurationTest extends TestCase
11
{
12
    use ConfigurationTestCaseTrait;
13
14
    /**
15
     * Return the instance of ConfigurationInterface that should be used by the
16
     * Configuration-specific assertions in this test-case
17
     *
18
     * @return \Symfony\Component\Config\Definition\ConfigurationInterface
19
     */
20
    protected function getConfiguration()
21
    {
22
        return new Configuration();
23
    }
24
25
    public function testBlankConfiguration()
26
    {
27
        $this->assertConfigurationIsValid([]);
28
    }
29
30
    public function testDefaultConfiguration()
31
    {
32
        $this->assertProcessedConfigurationEquals(
33
            [],
34
            [
35
                'commandbus' => ['default' => ['middleware' => ['tactician.middleware.command_handler']]],
36
                'default_bus' => 'default',
37
                'method_inflector' => 'tactician.handler.method_name_inflector.handle',
38
                'security' => [],
39
                'logger_formatter' => 'tactician.logger.class_properties_formatter'
40
            ]
41
        );
42
    }
43
44
    public function testSimpleMiddleware()
45
    {
46
        $this->assertConfigurationIsValid([
47
            'tactician' => [
48
                'commandbus' => [
49
                    'default' => [
50
                        'middleware' => [
51
                            'my_middleware'  => 'some_middleware',
52
                            'my_middleware2' => 'some_middleware',
53
                        ]
54
                    ]
55
                ]
56
            ]
57
        ]);
58
    }
59
60
    public function testMiddlewareMustBeScalar()
61
    {
62
        $this->assertConfigurationIsInvalid(
63
            [
64
                'tactician' => [
65
                    'commandbus' => [
66
                        'default' => [
67
                            'middleware' => [
68
                                'my_middleware'  => [],
69
                                'my_middleware2' => 'some_middleware',
70
                            ]
71
                        ]
72
                    ]
73
                ]
74
            ],
75
            'Invalid type for path "tactician.commandbus.default.middleware.my_middleware". Expected scalar, but got array.'
76
        );
77
    }
78
79
    public function testDefaultMiddlewareMustExist()
80
    {
81
        $this->assertConfigurationIsInvalid(
82
            [
83
                'tactician' => [
84
                    'default_bus' => 'foo',
85
                    'commandbus' => [
86
                        'bar' => [
87
                            'middleware' => [
88
                                'my_middleware'  => 'some_middleware',
89
                            ]
90
                        ]
91
                    ]
92
                ]
93
            ],
94
            'The default_bus "foo" was not defined as a command bus.'
95
        );
96
97
        $this->assertConfigurationIsInvalid(
98
            [
99
                'tactician' => [
100
                    'commandbus' => [
101
                        'bar' => [
102
                            'middleware' => [
103
                                'my_middleware'  => 'some_middleware',
104
                            ]
105
                        ]
106
                    ]
107
                ]
108
            ],
109
            'The default_bus "default" was not defined as a command bus.'
110
        );
111
    }
112
113
    public function testMiddlewareDefinitionCannotBeEmpty()
114
    {
115
        $this->assertConfigurationIsInvalid(
116
            [
117
                'tactician' => [
118
                    'commandbus' => [
119
                        'default' => [
120
                            'middleware' => [
121
                            ]
122
                        ]
123
                    ]
124
                ]
125
            ],
126
            'The path "tactician.commandbus.default.middleware" should have at least 1 element(s) defined.'
127
        );
128
129
        $this->assertConfigurationIsInvalid(
130
            [
131
                'tactician' => [
132
                    'commandbus' => [
133
                        'foo' => [
134
                            'middleware' => [
135
                            ]
136
                        ]
137
                    ]
138
                ]
139
            ],
140
            'The path "tactician.commandbus.foo.middleware" should have at least 1 element(s) defined.'
141
        );
142
    }
143
144
    public function testCommandHandlerMiddlewareIfPresentAndNotLastIsInvalid()
145
    {
146
        $this->assertConfigurationIsInvalid(
147
            [
148
                'tactician' => [
149
                    'commandbus' => [
150
                        'default' => [
151
                            'middleware' => [
152
                                'tactician.middleware.command_handler',
153
                                'my_middleware.custom.stuff',
154
155
                            ]
156
                        ]
157
                    ]
158
                ]
159
            ],
160
            '"tactician.middleware.command_handler" should be the last middleware loaded when it is used.'
161
        );
162
    }
163
164
    public function testCommandHandlerMiddlewarePresentAndLastIsValid()
165
    {
166
        $this->assertConfigurationIsValid(
167
            [
168
                'tactician' => [
169
                    'commandbus' => [
170
                        'default' => [
171
                            'middleware' => [
172
                                'my_middleware.custom.stuff',
173
                                'tactician.middleware.command_handler',
174
                            ]
175
                        ]
176
                    ]
177
                ]
178
            ]
179
        );
180
    }
181
    public function testCommandHandlerMiddlewareNotPresentDoesNotAffectValidation()
182
    {
183
        $this->assertConfigurationIsValid(
184
            [
185
                'tactician' => [
186
                    'commandbus' => [
187
                        'default' => [
188
                            'middleware' => [
189
                                'my_middleware.custom.stuff',
190
                                'my_middleware.custom.other_stuff',
191
                            ]
192
                        ]
193
                    ]
194
                ]
195
            ]
196
        );
197
    }
198
199
    public function testCustomMethodInflectorCanBeSet()
200
    {
201
        $this->assertConfigurationIsValid(
202
            [
203
                'tactician' => [
204
                    'method_inflector' => 'some.inflector.service',
205
                    'commandbus' => [
206
                        'default' => [
207
                            'middleware' => [
208
                                'my_middleware.custom.stuff',
209
                                'my_middleware.custom.other_stuff',
210
                            ],
211
                        ],
212
                        'second' => [
213
                            'middleware' => [
214
                                'my_middleware.custom.stuff',
215
                                'my_middleware.custom.other_stuff',
216
                            ]
217
                        ]
218
                    ]
219
                ]
220
            ]
221
        );
222
    }
223
224
    public function testSecurityConfiguration()
225
        {
226
        $this->assertConfigurationIsValid([
227
            'tactician' => [
228
                'commandbus' => [
229
                    'default' => [
230
                        'middleware' => [
231
                            'my_middleware'  => 'some_middleware',
232
                            'my_middleware2' => 'some_middleware',
233
                        ]
234
                    ]
235
                ],
236
                'security' => [
237
                    'Some\Command' => ['ROLE_USER'],
238
                    'Some\Other\Command' => ['ROLE_ADMIN'],
239
                ]
240
            ]
241
        ]);
242
    }
243
244
    public function testCustomLoggerFormatterCanBeSet()
245
    {
246
        $this->assertConfigurationIsValid(
247
            [
248
                'tactician' => [
249
                    'logger_formatter' => 'some.formatter.service',
250
                    'commandbus' => [
251
                        'default' => [
252
                            'middleware' => [
253
                                'my_middleware.custom.stuff',
254
                                'my_middleware.custom.other_stuff',
255
                            ],
256
                        ],
257
                        'second' => [
258
                            'middleware' => [
259
                                'my_middleware.custom.stuff',
260
                                'my_middleware.custom.other_stuff',
261
                            ]
262
                        ]
263
                    ]
264
                ]
265
            ]
266
        );
267
    }
268
}
269