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

ResetCharacterActionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 83
rs 10
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\Controller\GeneratorController;
15
use Pierstoval\Bundle\CharacterManagerBundle\Registry\ActionsRegistryInterface;
16
use Pierstoval\Bundle\CharacterManagerBundle\Resolver\StepResolverInterface;
17
use Pierstoval\Bundle\CharacterManagerBundle\Tests\Controller\AbstractGeneratorControllerTest;
18
use Symfony\Component\HttpFoundation\RedirectResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Routing\RouterInterface;
21
use Symfony\Component\Translation\TranslatorInterface;
22
23
class ResetCharacterActionTest extends AbstractGeneratorControllerTest
24
{
25
    /**
26
     * @expectedException \RuntimeException
27
     * @expectedExceptionMessage Session is mandatory when using the character generator.
28
     */
29
    public function test reset needs session()
30
    {
31
        $controller = $this->createController();
32
        $request = new Request();
33
34
        $controller->resetCharacterAction($request);
35
    }
36
37
    public function test reset session and flash message()
38
    {
39
        $router = $this->createMock(RouterInterface::class);
40
        $router->expects(self::once())
41
            ->method('generate')
42
            ->with('pierstoval_character_generator_index')
43
            ->willReturn('/generate/')
44
        ;
45
        $translator = $this->createMock(TranslatorInterface::class);
46
        $translator->expects(self::once())
47
            ->method('trans')
48
            ->with('steps.reset.character', [], 'PierstovalCharacterManager')
49
            ->willReturn('Translated flash message')
50
        ;
51
52
        $controller = $this->createController(null, $router, $translator);
53
        $request = $this->createRequest();
54
        $session = $request->getSession();
55
56
        if (!$session) {
57
            throw new \RuntimeException('Session should have been set in the test.');
58
        }
59
60
        $session->set('step', 10);
61
        $session->set('character', ['01' => 'step value is set and has to be removed']);
62
63
        $response = $controller->resetCharacterAction($request);
64
65
        static::assertSame([], $session->get('character'));
66
        static::assertSame(1, $session->get('step'));
67
        static::assertSame(['Translated flash message'], $session->getFlashBag()->get('success'));
68
        static::assertInstanceOf(RedirectResponse::class, $response);
69
        static::assertSame('/generate/', $response->headers->get('location'));
70
    }
71
72
    public function test reset session and flash message without translator()
73
    {
74
        $router = $this->createMock(RouterInterface::class);
75
        $router->expects(self::once())
76
            ->method('generate')
77
            ->with('pierstoval_character_generator_index')
78
            ->willReturn('/generate/')
79
        ;
80
81
        $controller = new GeneratorController(
82
            $this->createMock(StepResolverInterface::class),
83
            $this->createMock(ActionsRegistryInterface::class),
84
            $router
85
        );
86
87
        $request = $this->createRequest();
88
        $session = $request->getSession();
89
90
        if (!$session) {
91
            throw new \RuntimeException('Session should have been set in the test.');
92
        }
93
94
        $session->set('step', 10);
95
        $session->set('character', ['01' => 'step value is set and has to be removed']);
96
97
        $response = $controller->resetCharacterAction($request);
98
99
        static::assertSame([], $session->get('character'));
100
        static::assertSame(1, $session->get('step'));
101
        static::assertSame(['steps.reset.character'], $session->getFlashBag()->get('success'));
102
        static::assertInstanceOf(RedirectResponse::class, $response);
103
        static::assertSame('/generate/', $response->headers->get('location'));
104
    }
105
}
106