Passed
Push — bug/414-up-button-leaves-readp... ( 8b0ecd...5b272e )
by Michael
06:08
created

FinanceNodeControllerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
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
class FinanceNodeControllerTest extends TestCase
36
{
37
    /**
38
     * @var FinanceNodeController
39
     */
40
    private $controller;
41
42
    /**
43
     * @var Templating|\PHPUnit_Framework_MockObject_MockObject
44
     */
45
    private $templatingMock;
46
47
    /**
48
     * @var TokenStorage|\PHPUnit_Framework_MockObject_MockObject
49
     */
50
    private $tokenStorageMock;
51
52
    /**
53
     * @var Readpath|\PHPUnit_Framework_MockObject_MockObject
54
     */
55
    private $readpathMock;
56
57
    /**
58
     * @var FeatureToggle|\PHPUnit_Framework_MockObject_MockObject
59
     */
60
    private $featureToggleMock;
61
62
    /**
63
     * @var TextNodeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
64
     */
65
    private $textnodeRepositoryMock;
66
67
    /**
68
     * @var AuthorizationCheckerInterface|\PHPUnit_Framework_MockObject_MockObject
69
     */
70
    private $authorizationCheckerMock;
71
72
    /**
73
     * @var Router|\PHPUnit_Framework_MockObject_MockObject
74
     */
75
    private $routerMock;
76
77
    /**
78
     * @return void
79
     */
80
    protected function setUp(): void
81
    {
82
        parent::setUp();
83
84
        $this->templatingMock = $this->createMock(Templating::class);
85
        $this->tokenStorageMock = $this->createMock(TokenStorage::class);
86
        $this->readpathMock = $this->createMock(Readpath::class);
87
        $this->featureToggleMock = $this->createMock(FeatureToggle::class);
88
        $this->textnodeRepositoryMock = $this->createMock(TextNodeRepositoryInterface::class);
89
        $this->authorizationCheckerMock = $this->createMock(AuthorizationCheckerInterface::class);
90
        $this->routerMock = $this->createMock(Router::class);
91
92
        $this->controller = new FinanceNodeController(
93
            $this->templatingMock,
94
            $this->tokenStorageMock,
95
            $this->readpathMock,
96
            $this->featureToggleMock,
97
            $this->textnodeRepositoryMock,
98
            $this->authorizationCheckerMock,
99
            $this->routerMock
100
        );
101
    }
102
103
    /**
104
     * @return void
105
     */
106
    public function testShowActionRedirectsToLoginWhenIsNeeded(): void
107
    {
108
        $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

108
        $this->featureToggleMock->/** @scrutinizer ignore-call */ 
109
                                  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...
109
            ->with('login_needed')
110
            ->willReturn(true);
111
        $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

111
        $this->authorizationCheckerMock->/** @scrutinizer ignore-call */ 
112
                                         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...
112
            ->with('ROLE_USER')
113
            ->willReturn(false);
114
        $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

114
        $this->routerMock->/** @scrutinizer ignore-call */ 
115
                           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...
115
        $arbitraryId = 'someArbitraryId';
116
117
        $returnValue = $this->controller->showAction($arbitraryId);
118
119
        self::assertInstanceOf(RedirectResponse::class, $returnValue);
120
    }
121
122
    /**
123
     * @return void
124
     */
125
    public function testShowActionRendersTemplate(): void
126
    {
127
        $this->featureToggleMock->method('hasFeature')
128
            ->with('login_needed')
129
            ->willReturn(false);
130
        $arbitraryId = 'someArbitraryId';
131
132
        $textnodeMock = $this->createMock(Textnode::class);
133
134
        $this->textnodeRepositoryMock->expects(self::once())
135
            ->method('findOneActiveByArbitraryId')
136
            ->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

136
            ->with(/** @scrutinizer ignore-type */ $arbitraryId)
Loading history...
137
            ->willReturn($textnodeMock);
138
139
        $this->templatingMock->expects(self::once())
140
            ->method('renderResponse')
141
            ->willReturn(new Response());
142
143
        $this->controller->showAction($arbitraryId);
144
    }
145
146
    /**
147
     * @return void
148
     */
149
    public function testShowActionRedirectsToMainpageForInvalidId(): void
150
    {
151
        $this->featureToggleMock->method('hasFeature')
152
            ->with('login_needed')
153
            ->willReturn(false);
154
        $arbitraryId = 'someNonExistentArbitraryId';
155
156
        $this->textnodeRepositoryMock->expects(self::once())
157
            ->method('findOneActiveByArbitraryId')
158
            ->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

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