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

ResetStepActionTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 111
Duplicated Lines 9.01 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 10
loc 111
rs 10
c 0
b 0
f 0

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\Controller\GeneratorController;
13
14
use Pierstoval\Bundle\CharacterManagerBundle\Resolver\StepResolver;
15
use Pierstoval\Bundle\CharacterManagerBundle\Tests\Controller\AbstractGeneratorControllerTest;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Routing\RouterInterface;
19
use Symfony\Component\Translation\TranslatorInterface;
20
21
class ResetStepActionTest extends AbstractGeneratorControllerTest
22
{
23
    /**
24
     * @expectedException \RuntimeException
25
     * @expectedExceptionMessage Session is mandatory when using the character generator.
26
     */
27
    public function test reset step needs session()
28
    {
29
        $controller = $this->createController();
30
        $request = new Request();
31
32
        $controller->resetStepAction($request, '');
33
    }
34
35
    /**
36
     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
37
     * @expectedExceptionMessage Step not found.
38
     */
39
    public function test reset step with non existent name()
40
    {
41
        $resolver = new StepResolver([
42
            'manager_one' => $this->createManagerConfiguration('manager_one'),
43
        ]);
44
45
        $controller = $this->createController($resolver);
46
47
        $controller->resetStepAction($this->createRequest(), 'non_existent_step');
48
    }
49
50
    public function test reset step removes onchange clear steps()
51
    {
52
        $resolver = new StepResolver([
53
            'manager_one' => $this->createManagerConfiguration('manager_one', [
54
                '01' => [
55
                    'name'           => '01',
56
                    'onchange_clear' => ['03'],
57
                ],
58
                '02' => [
59
                    'name' => '02',
60
                ],
61
                '03' => [
62
                    'name' => '03',
63
                ],
64
            ]),
65
        ]);
66
        $router = $this->createMock(RouterInterface::class);
67
        $router->expects(self::once())
68
            ->method('generate')
69
            ->with('pierstoval_character_generator_step', ['requestStep' => '01'])
70
            ->willReturn('/generate/01')
71
        ;
72
        $translator = $this->createMock(TranslatorInterface::class);
73
        $translator->expects(self::once())
74
            ->method('trans')
75
            ->with('steps.reset.step', [], 'PierstovalCharacterManager')
76
            ->willReturn('Translated flash message')
77
        ;
78
79
        $controller = $this->createController($resolver, $router, $translator);
80
        $request = $this->createRequest();
81
        $session = $request->getSession();
82
83
        $session->set('character', [
84
            'manager_one' => [
85
                '01' => 'Should be removed',
86
                '02' => 'Should be kept',
87
                '03' => 'Should be removed',
88
            ],
89
        ]);
90
91
        $response = $controller->resetStepAction($request, '01');
92
93
        static::assertInstanceOf(RedirectResponse::class, $response);
94
        static::assertTrue($response->isRedirect('/generate/01'));
95
        static::assertSame([
96
            'manager_one' => [
97
                '02' => 'Should be kept'
98
            ],
99
        ], $session->get('character'));
100
        static::assertSame(['Translated flash message'], $session->getFlashBag()->get('success'));
101
    }
102
103
    public function test reset step with multiple managers correctly redirects()
104
    {
105
        $resolver = new StepResolver([
106
            'manager_one' => $this->createManagerConfiguration('manager_one', [
107
                '01' => [
108
                    'name' => '01',
109
                ],
110
            ]),
111
            'manager_two' => $this->createManagerConfiguration('manager_two', [
112
                '01' => [
113
                    'name' => '01',
114
                ],
115
            ]),
116
        ]);
117
        $router = $this->createMock(RouterInterface::class);
118
        $router->expects(self::once())
119
            ->method('generate')
120
            ->with('pierstoval_character_generator_step', ['requestStep' => '01', 'manager' => 'manager_two'])
121
            ->willReturn('/generate/manager_two/01')
122
        ;
123
124
        $controller = $this->createController($resolver, $router);
125
126
        $response = $controller->resetStepAction($this->createRequest(), '01', 'manager_two');
127
128
        static::assertInstanceOf(RedirectResponse::class, $response);
129
        static::assertTrue($response->isRedirect('/generate/manager_two/01'));
130
    }
131
}
132