testCommandHandlerMiddlewarePresentAndLastIsValid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
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
            //we use a regexp to support the slightly different message thrown by symfony >=5.0
76
            '#Invalid type for path "tactician.commandbus.default.middleware.my_middleware"\. Expected "?scalar"?, but got "?array"?\.#',
77
            true
78
        );
79
    }
80
81
    public function testDefaultMiddlewareMustExist()
82
    {
83
        $this->assertConfigurationIsInvalid(
84
            [
85
                'tactician' => [
86
                    'default_bus' => 'foo',
87
                    'commandbus' => [
88
                        'bar' => [
89
                            'middleware' => [
90
                                'my_middleware'  => 'some_middleware',
91
                            ]
92
                        ]
93
                    ]
94
                ]
95
            ],
96
            'The default_bus "foo" was not defined as a command bus.'
97
        );
98
99
        $this->assertConfigurationIsInvalid(
100
            [
101
                'tactician' => [
102
                    'commandbus' => [
103
                        'bar' => [
104
                            'middleware' => [
105
                                'my_middleware'  => 'some_middleware',
106
                            ]
107
                        ]
108
                    ]
109
                ]
110
            ],
111
            'The default_bus "default" was not defined as a command bus.'
112
        );
113
    }
114
115
    public function testMiddlewareDefinitionCannotBeEmpty()
116
    {
117
        $this->assertConfigurationIsInvalid(
118
            [
119
                'tactician' => [
120
                    'commandbus' => [
121
                        'default' => [
122
                            'middleware' => [
123
                            ]
124
                        ]
125
                    ]
126
                ]
127
            ],
128
            'The path "tactician.commandbus.default.middleware" should have at least 1 element(s) defined.'
129
        );
130
131
        $this->assertConfigurationIsInvalid(
132
            [
133
                'tactician' => [
134
                    'commandbus' => [
135
                        'foo' => [
136
                            'middleware' => [
137
                            ]
138
                        ]
139
                    ]
140
                ]
141
            ],
142
            'The path "tactician.commandbus.foo.middleware" should have at least 1 element(s) defined.'
143
        );
144
    }
145
146
    public function testCommandHandlerMiddlewareIfPresentAndNotLastIsInvalid()
147
    {
148
        $this->assertConfigurationIsInvalid(
149
            [
150
                'tactician' => [
151
                    'commandbus' => [
152
                        'default' => [
153
                            'middleware' => [
154
                                'tactician.middleware.command_handler',
155
                                'my_middleware.custom.stuff',
156
157
                            ]
158
                        ]
159
                    ]
160
                ]
161
            ],
162
            '"tactician.middleware.command_handler" should be the last middleware loaded when it is used.'
163
        );
164
    }
165
166
    public function testCommandHandlerMiddlewarePresentAndLastIsValid()
167
    {
168
        $this->assertConfigurationIsValid(
169
            [
170
                'tactician' => [
171
                    'commandbus' => [
172
                        'default' => [
173
                            'middleware' => [
174
                                'my_middleware.custom.stuff',
175
                                'tactician.middleware.command_handler',
176
                            ]
177
                        ]
178
                    ]
179
                ]
180
            ]
181
        );
182
    }
183
    public function testCommandHandlerMiddlewareNotPresentDoesNotAffectValidation()
184
    {
185
        $this->assertConfigurationIsValid(
186
            [
187
                'tactician' => [
188
                    'commandbus' => [
189
                        'default' => [
190
                            'middleware' => [
191
                                'my_middleware.custom.stuff',
192
                                'my_middleware.custom.other_stuff',
193
                            ]
194
                        ]
195
                    ]
196
                ]
197
            ]
198
        );
199
    }
200
201
    public function testCustomMethodInflectorCanBeSet()
202
    {
203
        $this->assertConfigurationIsValid(
204
            [
205
                'tactician' => [
206
                    'method_inflector' => 'some.inflector.service',
207
                    'commandbus' => [
208
                        'default' => [
209
                            'middleware' => [
210
                                'my_middleware.custom.stuff',
211
                                'my_middleware.custom.other_stuff',
212
                            ],
213
                        ],
214
                        'second' => [
215
                            'middleware' => [
216
                                'my_middleware.custom.stuff',
217
                                'my_middleware.custom.other_stuff',
218
                            ]
219
                        ]
220
                    ]
221
                ]
222
            ]
223
        );
224
    }
225
226
    public function testSecurityConfiguration()
227
        {
228
        $this->assertConfigurationIsValid([
229
            'tactician' => [
230
                'commandbus' => [
231
                    'default' => [
232
                        'middleware' => [
233
                            'my_middleware'  => 'some_middleware',
234
                            'my_middleware2' => 'some_middleware',
235
                        ]
236
                    ]
237
                ],
238
                'security' => [
239
                    'Some\Command' => ['ROLE_USER'],
240
                    'Some\Other\Command' => ['ROLE_ADMIN'],
241
                ]
242
            ]
243
        ]);
244
    }
245
246
    public function testCustomLoggerFormatterCanBeSet()
247
    {
248
        $this->assertConfigurationIsValid(
249
            [
250
                'tactician' => [
251
                    'logger_formatter' => 'some.formatter.service',
252
                    'commandbus' => [
253
                        'default' => [
254
                            'middleware' => [
255
                                'my_middleware.custom.stuff',
256
                                'my_middleware.custom.other_stuff',
257
                            ],
258
                        ],
259
                        'second' => [
260
                            'middleware' => [
261
                                'my_middleware.custom.stuff',
262
                                'my_middleware.custom.other_stuff',
263
                            ]
264
                        ]
265
                    ]
266
                ]
267
            ]
268
        );
269
    }
270
}
271