Passed
Push — master ( 94badf...ee3e77 )
by Michael
01:46
created

FinanceNodeControllerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 134
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 20 1
A testShowActionRedirectsToMainpageForInvalidId() 0 20 1
A testShowActionRendersTemplate() 0 19 1
A testShowActionRedirectsToLoginWhenIsNeeded() 0 14 1
1
<?php
2
/* Copyright (C) 2018 Michael Giesler
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace DembeloMain\Tests\Controller;
21
22
use DembeloMain\Controller\FinanceNodeController;
23
use DembeloMain\Document\Textnode;
24
use DembeloMain\Model\FeatureToggle;
25
use DembeloMain\Model\Readpath;
26
use DembeloMain\Model\Repository\TextNodeRepositoryInterface;
27
use PHPUnit\Framework\TestCase;
28
use Symfony\Bundle\FrameworkBundle\Routing\Router;
29
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as Templating;
30
use Symfony\Component\HttpFoundation\RedirectResponse;
31
use Symfony\Component\HttpFoundation\Response;
32
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
33
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
34
35
/**
36
 * Class FinanceNodeControllerTest
37
 */
38
class FinanceNodeControllerTest extends TestCase
39
{
40
    /**
41
     * @var FinanceNodeController
42
     */
43
    private $controller;
44
45
    /**
46
     * @var Templating|\PHPUnit_Framework_MockObject_MockObject
47
     */
48
    private $templatingMock;
49
50
    /**
51
     * @var TokenStorage|\PHPUnit_Framework_MockObject_MockObject
52
     */
53
    private $tokenStorageMock;
54
55
    /**
56
     * @var Readpath|\PHPUnit_Framework_MockObject_MockObject
57
     */
58
    private $readpathMock;
59
60
    /**
61
     * @var FeatureToggle|\PHPUnit_Framework_MockObject_MockObject
62
     */
63
    private $featureToggleMock;
64
65
    /**
66
     * @var TextNodeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
67
     */
68
    private $textnodeRepositoryMock;
69
70
    /**
71
     * @var AuthorizationCheckerInterface|\PHPUnit_Framework_MockObject_MockObject
72
     */
73
    private $authorizationCheckerMock;
74
75
    /**
76
     * @var Router|\PHPUnit_Framework_MockObject_MockObject
77
     */
78
    private $routerMock;
79
80
    /**
81
     * @return void
82
     */
83
    protected function setUp(): void
84
    {
85
        parent::setUp();
86
87
        $this->templatingMock = $this->createMock(Templating::class);
88
        $this->tokenStorageMock = $this->createMock(TokenStorage::class);
89
        $this->readpathMock = $this->createMock(Readpath::class);
90
        $this->featureToggleMock = $this->createMock(FeatureToggle::class);
91
        $this->textnodeRepositoryMock = $this->createMock(TextNodeRepositoryInterface::class);
92
        $this->authorizationCheckerMock = $this->createMock(AuthorizationCheckerInterface::class);
93
        $this->routerMock = $this->createMock(Router::class);
94
95
        $this->controller = new FinanceNodeController(
96
            $this->templatingMock,
97
            $this->tokenStorageMock,
98
            $this->readpathMock,
99
            $this->featureToggleMock,
100
            $this->textnodeRepositoryMock,
101
            $this->authorizationCheckerMock,
102
            $this->routerMock
103
        );
104
    }
105
106
    /**
107
     * @return void
108
     */
109
    public function testShowActionRedirectsToLoginWhenIsNeeded(): void
110
    {
111
        $this->featureToggleMock->method('hasFeature')
0 ignored issues
show
Bug introduced by
The method method() does not exist on DembeloMain\Model\FeatureToggle. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
        $this->featureToggleMock->/** @scrutinizer ignore-call */ 
112
                                  method('hasFeature')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
            ->with('login_needed')
113
            ->willReturn(true);
114
        $this->authorizationCheckerMock->method('isGranted')
0 ignored issues
show
Bug introduced by
The method method() does not exist on Symfony\Component\Securi...izationCheckerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
        $this->authorizationCheckerMock->/** @scrutinizer ignore-call */ 
115
                                         method('isGranted')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
            ->with('ROLE_USER')
116
            ->willReturn(false);
117
        $this->routerMock->method('generate')->willReturn('login_route');
0 ignored issues
show
Bug introduced by
The method method() does not exist on Symfony\Bundle\FrameworkBundle\Routing\Router. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
        $this->routerMock->/** @scrutinizer ignore-call */ 
118
                           method('generate')->willReturn('login_route');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
        $arbitraryId = 'someArbitraryId';
119
120
        $returnValue = $this->controller->showAction($arbitraryId);
121
122
        self::assertInstanceOf(RedirectResponse::class, $returnValue);
123
    }
124
125
    /**
126
     * @return void
127
     */
128
    public function testShowActionRendersTemplate(): void
129
    {
130
        $this->featureToggleMock->method('hasFeature')
131
            ->with('login_needed')
132
            ->willReturn(false);
133
        $arbitraryId = 'someArbitraryId';
134
135
        $textnodeMock = $this->createMock(Textnode::class);
136
137
        $this->textnodeRepositoryMock->expects(self::once())
138
            ->method('findOneActiveByArbitraryId')
139
            ->with($arbitraryId)
0 ignored issues
show
Bug introduced by
$arbitraryId of type string is incompatible with the type array expected by parameter $arguments of PHPUnit\Framework\MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
            ->with(/** @scrutinizer ignore-type */ $arbitraryId)
Loading history...
140
            ->willReturn($textnodeMock);
141
142
        $this->templatingMock->expects(self::once())
143
            ->method('renderResponse')
144
            ->willReturn(new Response());
145
146
        $this->controller->showAction($arbitraryId);
147
    }
148
149
    /**
150
     * @return void
151
     */
152
    public function testShowActionRedirectsToMainpageForInvalidId(): void
153
    {
154
        $this->featureToggleMock->method('hasFeature')
155
            ->with('login_needed')
156
            ->willReturn(false);
157
        $arbitraryId = 'someNonExistentArbitraryId';
158
159
        $this->textnodeRepositoryMock->expects(self::once())
160
            ->method('findOneActiveByArbitraryId')
161
            ->with($arbitraryId)
0 ignored issues
show
Bug introduced by
$arbitraryId of type string is incompatible with the type array expected by parameter $arguments of PHPUnit\Framework\MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

161
            ->with(/** @scrutinizer ignore-type */ $arbitraryId)
Loading history...
162
            ->willReturn(null);
163
164
        $this->templatingMock->expects(self::never())
165
            ->method('renderResponse');
166
167
        $this->routerMock->method('generate')->willReturn('maipage');
168
169
        $returnValue = $this->controller->showAction($arbitraryId);
170
171
        self::assertInstanceOf(RedirectResponse::class, $returnValue);
172
    }
173
}
174