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

StepActionTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 103
Duplicated Lines 9.71 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 9
dl 10
loc 103
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\Registry\ActionsRegistry;
15
use Pierstoval\Bundle\CharacterManagerBundle\Resolver\StepResolver;
16
use \Pierstoval\Bundle\CharacterManagerBundle\Tests\Fixtures\Stubs\Action\ConcreteAbstractActionStub;
17
use Pierstoval\Bundle\CharacterManagerBundle\Tests\Controller\AbstractGeneratorControllerTest;
18
use Pierstoval\Bundle\CharacterManagerBundle\Tests\Fixtures\Stubs\Entity\CharacterStub;
19
use Pierstoval\Bundle\CharacterManagerBundle\Tests\Fixtures\Stubs\Model\StepStub;
20
use Symfony\Component\HttpFoundation\RedirectResponse;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\Routing\RouterInterface;
23
use Symfony\Component\Translation\TranslatorInterface;
24
25
class StepActionTest extends AbstractGeneratorControllerTest
26
{
27
    /**
28
     * @expectedException \RuntimeException
29
     * @expectedExceptionMessage Session is mandatory when using the character generator.
30
     */
31
    public function test step action needs session()
32
    {
33
        $controller = $this->createController();
34
        $request = new Request();
35
36
        $controller->stepAction($request, '');
37
    }
38
39
    /**
40
     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
41
     * @expectedExceptionMessage Step not found.
42
     */
43
    public function test step action with non existent name()
44
    {
45
        $resolver = new StepResolver([
46
            'manager_one' => $this->createManagerConfiguration('manager_one'),
47
        ]);
48
49
        $controller = $this->createController($resolver);
50
51
        $controller->stepAction($this->createRequest(), 'non_existent_step');
52
    }
53
54
    public function test step action should execute action class()
55
    {
56
        $resolver = new StepResolver([
57
            'test_manager' => $this->createManagerConfiguration('test_manager', [
58
                'test_step' => [
59
                    'name' => 'test_step',
60
                ],
61
            ]),
62
        ]);
63
64
        $step = StepStub::createStub();
65
66
        $action = new ConcreteAbstractActionStub();
67
        $action->configure($step->getManagerName(), $step->getName(), CharacterStub::class, $resolver);
68
69
        $registry = new ActionsRegistry();
70
        $registry->addStepAction('test_manager', $action);
71
72
        $controller = $this->createController($resolver, null, null, $registry);
73
74
        $response = $controller->stepAction($this->createRequest(), 'test_step');
75
76
        static::assertSame('Stub response based on abstract class', $response->getContent());
77
    }
78
79
    public function test step action should check for dependencies()
80
    {
81
        $resolver = new StepResolver([
82
            'test_manager' => $this->createManagerConfiguration('test_manager', [
83
                'test_step' => [
84
                    'name' => 'test_step',
85
                    'label' => 'Test step one'
86
                ],
87
                'second_step' => [
88
                    'name' => 'second_step',
89
                    'label' => 'Test step two',
90
                    'number' => 2,
91
                    'dependencies' => ['test_step'],
92
                ],
93
            ]),
94
        ]);
95
        $router = $this->createMock(RouterInterface::class);
96
        $router->expects(self::once())
97
            ->method('generate')
98
            ->with('pierstoval_character_generator_index')
99
            ->willReturn('/generate/manager_one/test_step')
100
        ;
101
        $translator = $this->createMock(TranslatorInterface::class);
102
        $translator->expects(self::once())
103
            ->method('trans')
104
            ->with('steps.dependency_not_set', [
105
                '%current_step%' => 'Test step two',
106
                '%dependency%' => 'Test step one',
107
            ], 'PierstovalCharacterManager')
108
            ->willReturn('Translated error message')
109
        ;
110
111
        $step2 = $resolver->resolve('second_step');
112
113
        $action = new ConcreteAbstractActionStub();
114
        $action->configure($step2->getManagerName(), $step2->getName(), CharacterStub::class, $resolver);
115
116
        $registry = new ActionsRegistry();
117
        $registry->addStepAction('test_manager', $action);
118
119
        $request = $this->createRequest();
120
        $controller = $this->createController($resolver, $router, $translator, $registry);
121
        $response = $controller->stepAction($request, 'second_step');
122
123
        static::assertInstanceOf(RedirectResponse::class, $response);
124
        static::assertTrue($response->isRedirect('/generate/manager_one/test_step'));
125
        static::assertSame(['Translated error message'], $request->getSession()->getFlashBag()->get('error'));
126
    }
127
}
128