Completed
Push — master ( e32202...083db4 )
by Alex
08:17
created

StepResolverTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 281
Duplicated Lines 43.42 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 4
dl 122
loc 281
rs 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * This file is part of the PierstovalCharacterManagerBundle package.
5
 *
6
 * (c) Alexandre Rock Ancelet <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Pierstoval\Bundle\CharacterManagerBundle\Tests\Resolver;
13
14
use PHPUnit\Framework\TestCase;
15
use Pierstoval\Bundle\CharacterManagerBundle\Model\Step;
16
use Pierstoval\Bundle\CharacterManagerBundle\Resolver\StepResolver;
17
use Pierstoval\Bundle\CharacterManagerBundle\Tests\Fixtures\Stubs\Action\ConcreteAbstractActionStub;
18
19
class StepResolverTest extends TestCase
20
{
21
    /**
22
     * @expectedException \RuntimeException
23
     * @expectedExceptionMessage No character managers to resolve configuration for.
24
     */
25
    public function test manager steps with empty config throws exception()
26
    {
27
        $resolver = new StepResolver();
28
29
        $resolver->getManagerSteps();
30
    }
31
32
    public function test manager steps with single config option()
33
    {
34
        $stepsArray = [
35
            'one' => [
36
                'number'         => 1,
37
                'name'           => '01',
38
                'action'         => ConcreteAbstractActionStub::class,
39
                'label'          => '',
40
                'onchange_clear' => [],
41
                'dependencies'   => [],
42
                'manager_name'   => 'main_manager',
43
            ]
44
        ];
45
46
        $stepsObjects = [];
47
48
        foreach ($stepsArray as $name => $step) {
49
            $stepsObjects[$name] = Step::createFromData($step);
50
        }
51
52
        $resolver = new StepResolver(['main_manager' => ['steps' => $stepsArray]]);
53
54
        static::assertEquals($stepsObjects, $resolver->getManagerSteps('main_manager'));
55
    }
56
57
    /**
58
     * @expectedException \Pierstoval\Bundle\CharacterManagerBundle\Exception\StepNotFoundException
59
     * @expectedExceptionMessage "non_existent_step" step does not exist in manager main_manager.
60
     */
61
    public function test resolve non existent step name should throw exception()
62
    {
63
        $resolver = new StepResolver([
64
            'main_manager' => [
65
                'steps' => [
66
                    'step_1' => [
67
                        'action' => ConcreteAbstractActionStub::class,
68
                        'name' =>  'step_1',
69
                        'label' =>  'Step 1',
70
                        'dependencies' =>  [],
71
                        'manager_name' => 'main_manager',
72
                        'onchange_clear' =>  [],
73
                        'number' =>  1,
74
                    ],
75
                ],
76
            ],
77
        ]);
78
79
        $resolver->resolve('non_existent_step');
80
    }
81
82
    /**
83
     * @expectedException \Pierstoval\Bundle\CharacterManagerBundle\Exception\StepNotFoundException
84
     * @expectedExceptionMessage "5" step does not exist in manager main_manager.
85
     */
86
    public function test resolve non existent step number should throw an exception()
87
    {
88
        $resolver = new StepResolver([
89
            'main_manager' => [
90
                'steps' => [
91
                    'step_1' => [
92
                        'action' => ConcreteAbstractActionStub::class,
93
                        'name' =>  'step_1',
94
                        'label' =>  'Step 1',
95
                        'dependencies' =>  [],
96
                        'manager_name' => 'main_manager',
97
                        'onchange_clear' =>  [],
98
                        'number' =>  1,
99
                    ],
100
                ],
101
            ],
102
        ]);
103
104
        $resolver->resolveNumber(5);
105
    }
106
107
    public function test resolve step name should return correct step values()
108
    {
109
        $step1 = [
110
            'number' => 0,
111
            'name' =>  'step_1',
112
            'action' => ConcreteAbstractActionStub::class,
113
            'label' =>  'Step 1',
114
            'manager_name' => 'main_manager',
115
            'onchange_clear' =>  [],
116
            'dependencies' =>  [],
117
        ];
118
119
        $resolver = new StepResolver([
120
            'main_manager' => [
121
                'steps' => [
122
                    'step_1' => $step1,
123
                ],
124
            ],
125
        ]);
126
127
        $resolvedStep = $resolver->resolve('step_1');
128
129
        static::assertNotNull($resolvedStep);
130
        static::assertSame($step1['number'], $resolvedStep->getNumber());
131
        static::assertSame($step1['name'], $resolvedStep->getName());
132
        static::assertSame($step1['label'], $resolvedStep->getLabel());
133
        static::assertSame($step1['action'], $resolvedStep->getAction());
134
        static::assertSame($step1['manager_name'], $resolvedStep->getManagerName());
135
        static::assertSame($step1['onchange_clear'], $resolvedStep->getOnchangeClear());
136
        static::assertSame($step1['dependencies'], $resolvedStep->getDependencies());
137
    }
138
139
    public function test resolve step number should return correct step()
140
    {
141
        $step1 = [
142
            'number' => 0,
143
            'name' =>  'step_1',
144
            'action' => ConcreteAbstractActionStub::class,
145
            'label' =>  'Step 1',
146
            'manager_name' => 'main_manager',
147
            'onchange_clear' =>  [],
148
            'dependencies' =>  [],
149
        ];
150
151
        $resolver = new StepResolver([
152
            'main_manager' => [
153
                'steps' => [
154
                    'step_1' => $step1,
155
                ],
156
            ],
157
        ]);
158
159
        $resolvedStep = $resolver->resolveNumber(0);
160
161
        static::assertNotNull($resolvedStep);
162
        static::assertSame($step1['number'], $resolvedStep->getNumber());
163
        static::assertSame($step1['name'], $resolvedStep->getName());
164
        static::assertSame($step1['label'], $resolvedStep->getLabel());
165
        static::assertSame($step1['action'], $resolvedStep->getAction());
166
        static::assertSame($step1['manager_name'], $resolvedStep->getManagerName());
167
        static::assertSame($step1['onchange_clear'], $resolvedStep->getOnchangeClear());
168
        static::assertSame($step1['dependencies'], $resolvedStep->getDependencies());
169
    }
170
171
    /**
172
     * @expectedException \RuntimeException
173
     * @expectedExceptionMessage "non_existent_step" step does not exist in manager test.
174
     */
175
    public function test resolve with no steps should throw exception()
176
    {
177
        $resolver = new StepResolver(['test' => ['steps' => []]]);
178
179
        $resolver->resolve('non_existent_step');
180
    }
181
182
    /**
183
     * @expectedException \RuntimeException
184
     * @expectedExceptionMessage "0" step does not exist in manager test.
185
     */
186
    public function test resolve number with no steps should throw exception()
187
    {
188
        $resolver = new StepResolver(['test' => ['steps' => []]]);
189
190
        $resolver->resolveNumber(0);
191
    }
192
193
    /**
194
     * @expectedException \InvalidArgumentException
195
     * @expectedExceptionMessage "invalid_manager" manager does not exist, or is not initialized yet.
196
     */
197
    public function test resolve invalid manager should throw exception()
198
    {
199
        $resolver = new StepResolver([
200
            'main_manager' => [
201
                'steps' => [
202
                    'step_1' => [
203
                        'action'         => '',
204
                        'label'          => '',
205
                        'number'         => 1,
206
                        'name'           => '',
207
                        'manager_name'   => '',
208
                        'dependencies'   => [],
209
                        'onchange_clear' => [],
210
                    ],
211
                ],
212
            ],
213
        ]);
214
215
        $resolver->resolve('', 'invalid_manager');
216
    }
217
218
    public function test resolve multiple manager names with same steps names()
219
    {
220
        $resolver = new StepResolver([
221
            'manager_one' => [
222
                'character_class' => '',
223
                'steps'           => [
224
                    '01' => [
225
                        'action'         => '',
226
                        'label'          => '',
227
                        'number'         => 1,
228
                        'name'           => '',
229
                        'manager_name'   => 'manager_one',
230
                        'dependencies'   => [],
231
                        'onchange_clear' => [],
232
                    ],
233
                ],
234
            ],
235
            'manager_two' => [
236
                'character_class' => '',
237
                'steps'           => [
238
                    '01' => [
239
                        'action'         => '',
240
                        'label'          => '',
241
                        'number'         => 1,
242
                        'name'           => '',
243
                        'manager_name'   => 'manager_two',
244
                        'dependencies'   => [],
245
                        'onchange_clear' => [],
246
                    ],
247
                ],
248
            ],
249
        ]);
250
251
        $stepFromManagerOne = $resolver->resolve('01', 'manager_one');
252
        static::assertNotNull($stepFromManagerOne);
253
        static::assertSame('manager_one', $stepFromManagerOne->getManagerName());
254
255
        $stepFromManagerTwo = $resolver->resolve('01', 'manager_two');
256
        static::assertNotNull($stepFromManagerTwo);
257
        static::assertSame('manager_two', $stepFromManagerTwo->getManagerName());
258
    }
259
260
    /**
261
     * @expectedException \InvalidArgumentException
262
     * @expectedExceptionMessage You did not specify which character manager you want to get the steps from, and you have more than one manager. Possible choices: manager_one, manager_two
263
     */
264
    public function test resolve with no manager name when multiple managers configured should throw exception()
265
    {
266
        $resolver = new StepResolver([
267
            'manager_one' => [
268
                'character_class' => '',
269
                'steps'           => [
270
                    '01' => [
271
                        'action'         => '',
272
                        'label'          => '',
273
                        'number'         => 1,
274
                        'name'           => '',
275
                        'manager_name'   => '',
276
                        'dependencies'   => [],
277
                        'onchange_clear' => [],
278
                    ],
279
                ],
280
            ],
281
            'manager_two' => [
282
                'character_class' => '',
283
                'steps'           => [
284
                    '01' => [
285
                        'action'         => '',
286
                        'label'          => '',
287
                        'number'         => 1,
288
                        'name'           => '',
289
                        'manager_name'   => '',
290
                        'dependencies'   => [],
291
                        'onchange_clear' => [],
292
                    ],
293
                ],
294
            ],
295
        ]);
296
297
        $resolver->resolve('01');
298
    }
299
}
300