Completed
Pull Request — master (#41)
by Ross
03:55
created

testCommandHandlerMiddlewareIfPresentAndNotLastIsInvalid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
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 testSimpleMiddleware()
31
    {
32
        $this->assertConfigurationIsValid([
33
            'tactician' => [
34
                'commandbus' => [
35
                    'default' => [
36
                        'middleware' => [
37
                            'my_middleware'  => 'some_middleware',
38
                            'my_middleware2' => 'some_middleware',
39
                        ]
40
                    ]
41
                ]
42
            ]
43
        ]);
44
    }
45
46
    public function testMiddlewareMustBeScalar()
47
    {
48
        $this->assertConfigurationIsInvalid(
49
            [
50
                'tactician' => [
51
                    'commandbus' => [
52
                        'default' => [
53
                            'middleware' => [
54
                                'my_middleware'  => [],
55
                                'my_middleware2' => 'some_middleware',
56
                            ]
57
                        ]
58
                    ]
59
                ]
60
            ],
61
            'Invalid type for path "tactician.commandbus.default.middleware.my_middleware". Expected scalar, but got array.'
62
        );
63
    }
64
65
    public function testDefaultMiddlewareMustExist()
66
    {
67
        $this->assertConfigurationIsInvalid(
68
            [
69
                'tactician' => [
70
                    'default_bus' => 'foo',
71
                    'commandbus' => [
72
                        'bar' => [
73
                            'middleware' => [
74
                                'my_middleware'  => 'some_middleware',
75
                            ]
76
                        ]
77
                    ]
78
                ]
79
            ],
80
            'The default_bus "foo" was not defined as command bus.'
81
        );
82
83
        $this->assertConfigurationIsInvalid(
84
            [
85
                'tactician' => [
86
                    'commandbus' => [
87
                        'bar' => [
88
                            'middleware' => [
89
                                'my_middleware'  => 'some_middleware',
90
                            ]
91
                        ]
92
                    ]
93
                ]
94
            ],
95
            'The default_bus "default" was not defined as command bus.'
96
        );
97
    }
98
99
    public function testMiddlewareDefinitionCannotBeEmpty()
100
    {
101
        $this->assertConfigurationIsInvalid(
102
            [
103
                'tactician' => [
104
                    'commandbus' => [
105
                        'default' => [
106
                            'middleware' => [
107
                            ]
108
                        ]
109
                    ]
110
                ]
111
            ],
112
            'The path "tactician.commandbus.default.middleware" should have at least 1 element(s) defined.'
113
        );
114
115
        $this->assertConfigurationIsInvalid(
116
            [
117
                'tactician' => [
118
                    'commandbus' => [
119
                        'foo' => [
120
                            'middleware' => [
121
                            ]
122
                        ]
123
                    ]
124
                ]
125
            ],
126
            'The path "tactician.commandbus.foo.middleware" should have at least 1 element(s) defined.'
127
        );
128
    }
129
130
    public function testCommandHandlerMiddlewareIfPresentAndNotLastIsInvalid()
131
    {
132
        $this->assertConfigurationIsInvalid(
133
            [
134
                'tactician' => [
135
                    'commandbus' => [
136
                        'default' => [
137
                            'middleware' => [
138
                                'tactician.middleware.command_handler',
139
                                'my_middleware.custom.stuff',
140
141
                            ]
142
                        ]
143
                    ]
144
                ]
145
            ],
146
            '"tactician.middleware.command_handler" should be last loaded middleware when it is use.'
147
        );
148
    }
149
150
    public function testCommandHandlerMiddlewarePresentAndLastIsValid()
151
    {
152
        $this->assertConfigurationIsValid(
153
            [
154
                'tactician' => [
155
                    'commandbus' => [
156
                        'default' => [
157
                            'middleware' => [
158
                                'my_middleware.custom.stuff',
159
                                'tactician.middleware.command_handler',
160
                            ]
161
                        ]
162
                    ]
163
                ]
164
            ]
165
        );
166
    }
167
    public function testCommandHandlerMiddlewareNotPresentDoesNotAffectValidation()
168
    {
169
        $this->assertConfigurationIsValid(
170
            [
171
                'tactician' => [
172
                    'commandbus' => [
173
                        'default' => [
174
                            'middleware' => [
175
                                'my_middleware.custom.stuff',
176
                                'my_middleware.custom.other_stuff',
177
                            ]
178
                        ]
179
                    ]
180
                ]
181
            ]
182
        );
183
    }
184
185
    public function testCustomMethodInflectorCanBeSet()
186
    {
187
        $this->assertConfigurationIsValid(
188
            [
189
                'tactician' => [
190
                    'method_inflector' => 'some.inflector.service',
191
                    'commandbus' => [
192
                        'default' => [
193
                            'middleware' => [
194
                                'my_middleware.custom.stuff',
195
                                'my_middleware.custom.other_stuff',
196
                            ],
197
                        ],
198
                        'second' => [
199
                            'middleware' => [
200
                                'my_middleware.custom.stuff',
201
                                'my_middleware.custom.other_stuff',
202
                            ]
203
                        ]
204
                    ]
205
                ]
206
            ]
207
        );
208
    }
209
210
    public function testSecurityConfiguration()
211
        {
212
        $this->assertConfigurationIsValid([
213
            'tactician' => [
214
                'commandbus' => [
215
                    'default' => [
216
                        'middleware' => [
217
                            'my_middleware'  => 'some_middleware',
218
                            'my_middleware2' => 'some_middleware',
219
                        ]
220
                    ]
221
                ],
222
                'security' => [
223
                    'Some\Command' => ['ROLE_USER'],
224
                    'Some\Other\Command' => ['ROLE_ADMIN'],
225
                ]
226
            ]
227
        ]);
228
    }
229
}
230