Passed
Push — feature/451-usercontroller-as-... ( 1dcd14 )
by Michael
06:10
created

UserControllerTest::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
3
/* Copyright (C) 2015 Michael Giesler
4
 *
5
 * This file is part of Dembelo.
6
 *
7
 * Dembelo is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * Dembelo is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License 3 for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License 3
18
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
22
/**
23
 * @package DembeloMain
24
 */
25
namespace DembeloMain\Tests\Controller;
26
27
use DembeloMain\Document\User;
28
use DembeloMain\Model\Repository\UserRepositoryInterface;
29
use Doctrine\ODM\MongoDB\DocumentManager;
30
use PHPUnit\Framework\TestCase;
31
use DembeloMain\Controller\UserController;
32
use Symfony\Bundle\FrameworkBundle\Routing\Router;
33
use Symfony\Component\Form\FormFactoryInterface;
34
use Symfony\Component\Form\FormInterface;
35
use Symfony\Component\HttpFoundation\Request;
36
use Symfony\Component\HttpFoundation\Response;
37
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
38
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
39
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as Templating;
40
41
/**
42
 * Class DefaultControllerTest
43
 */
44
class UserControllerTest extends TestCase
45
{
46
    /**
47
     * @var UserController
48
     */
49
    private $controller;
50
51
    /**
52
     * @var AuthenticationUtils|\PHPUnit_Framework_MockObject_MockObject
53
     */
54
    private $authenticationUtilsMock;
55
56
    /**
57
     * @var UserRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
58
     */
59
    private $userRepositoryMock;
60
61
    /**
62
     * @var DocumentManager|\PHPUnit_Framework_MockObject_MockObject
63
     */
64
    private $documentManagerMock;
65
66
    /**
67
     * @var Templating|\PHPUnit_Framework_MockObject_MockObject
68
     */
69
    private $templatingMock;
70
71
    /**
72
     * @var Router|\PHPUnit_Framework_MockObject_MockObject
73
     */
74
    private $routerMock;
75
76
    /**
77
     * @var FormFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
78
     */
79
    private $formFactoryMock;
80
81
    /**
82
     * @var UserPasswordEncoder
83
     */
84
    private $passwordEncoderMock;
85
86
    /**
87
     * @return void
88
     */
89
    protected function setUp(): void
90
    {
91
        parent::setUp();
92
93
        $this->authenticationUtilsMock = $this->createMock(AuthenticationUtils::class);
94
        $this->userRepositoryMock = $this->createMock(UserRepositoryInterface::class);
95
        $this->documentManagerMock = $this->createMock(DocumentManager::class);
96
        $this->templatingMock = $this->createMock(Templating::class);
97
        $this->routerMock = $this->createMock(Router::class);
98
        $this->formFactoryMock = $this->createMock(FormFactoryInterface::class);
99
        $this->passwordEncoderMock = $this->createMock(UserPasswordEncoder::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Symfon...PasswordEncoder::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Component\Securi...der\UserPasswordEncoder of property $passwordEncoderMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
100
101
        $this->controller = new UserController(
102
            $this->authenticationUtilsMock,
103
            $this->userRepositoryMock,
104
            $this->documentManagerMock,
105
            $this->templatingMock,
106
            $this->routerMock,
107
            $this->formFactoryMock,
108
            $this->passwordEncoderMock
109
        );
110
    }
111
112
    /**
113
     * @return void
114
     */
115
    public function testLoginAction(): void
116
    {
117
        $loginFormMock = $this->createMock(FormInterface::class);
118
        $this->formFactoryMock->method('create')->willReturn($loginFormMock);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Symfony\Component\Form\FormFactoryInterface. ( Ignorable by Annotation )

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

118
        $this->formFactoryMock->/** @scrutinizer ignore-call */ 
119
                                method('create')->willReturn($loginFormMock);

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...
119
120
        $responseMock = $this->createMock(Response::class);
121
122
        $this->templatingMock->expects(self::once())
123
            ->method('renderResponse')
124
            ->willReturn($responseMock);
125
126
        $this->controller->loginAction();
127
    }
128
129
    /**
130
     * @return void
131
     *
132
     * @throws \Exception
133
     */
134
    public function testRegistrationActionNotSubmitted(): void
135
    {
136
        $registrationFormMock = $this->createMock(FormInterface::class);
137
        $this->formFactoryMock->method('create')->willReturn($registrationFormMock);
138
        $registrationFormMock->method('isSubmitted')->willReturn(false);
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

138
        $registrationFormMock->/** @scrutinizer ignore-call */ 
139
                               method('isSubmitted')->willReturn(false);

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...
139
140
        /** @var Request|\PHPUnit_Framework_MockObject_MockObject $request */
141
        $request = $this->createMock(Request::class);
142
143
        $responseMock = $this->createMock(Response::class);
144
145
        $this->templatingMock->expects(self::once())
146
            ->method('renderResponse')
147
            ->willReturn($responseMock);
148
149
        $this->documentManagerMock->expects(self::never())->method('persist');
150
        $this->documentManagerMock->expects(self::never())->method('flush');
151
152
        $this->controller->registrationAction($request);
153
    }
154
155
    /**
156
     * @return void
157
     *
158
     * @throws \Exception
159
     */
160
    public function testRegistrationActionSubmittedAndValid(): void
161
    {
162
        $registrationFormMock = $this->createMock(FormInterface::class);
163
        $this->formFactoryMock->method('create')->willReturn($registrationFormMock);
164
        $registrationFormMock->method('isSubmitted')->willReturn(true);
165
        $registrationFormMock->method('isValid')->willReturn(true);
166
167
        /** @var Request|\PHPUnit_Framework_MockObject_MockObject $request */
168
        $request = $this->createMock(Request::class);
169
170
        $responseMock = $this->createMock(Response::class);
0 ignored issues
show
Unused Code introduced by
The assignment to $responseMock is dead and can be removed.
Loading history...
171
172
        $this->templatingMock->expects(self::never())->method('renderResponse');
173
174
        $this->routerMock->method('generate')->willReturn('registration_success');
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

174
        $this->routerMock->/** @scrutinizer ignore-call */ 
175
                           method('generate')->willReturn('registration_success');

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...
175
176
        $this->documentManagerMock->expects(self::once())->method('persist');
177
        $this->documentManagerMock->expects(self::once())->method('flush');
178
179
        $this->controller->registrationAction($request);
180
    }
181
182
    /**
183
     * @return void
184
     */
185
    public function testRegistrationsuccessAction(): void
186
    {
187
        $responseMock = $this->createMock(Response::class);
188
189
        $this->templatingMock->expects(self::once())
190
            ->method('renderResponse')
191
            ->willReturn($responseMock);
192
        $this->controller->registrationsuccessAction();
193
    }
194
195
    /**
196
     * @return void
197
     */
198
    public function testActivateemailActionUserNotFound(): void
199
    {
200
        $hash = 'someHash';
201
        $responseMock = $this->createMock(Response::class);
0 ignored issues
show
Unused Code introduced by
The assignment to $responseMock is dead and can be removed.
Loading history...
202
203
        $this->templatingMock->expects(self::never())->method('renderResponse');
204
205
        $this->expectException(\InvalidArgumentException::class);
206
        $this->controller->activateemailAction($hash);
207
    }
208
209
    /**
210
     * @return void
211
     */
212
    public function testActivateemailActionUserFound(): void
213
    {
214
        $hash = 'someHash';
215
        $responseMock = $this->createMock(Response::class);
216
217
        $this->templatingMock->expects(self::once())
218
            ->method('renderResponse')
219
            ->willReturn($responseMock);
220
221
        $userMock = $this->createMock(User::class);
222
223
        $this->userRepositoryMock->method('findOneBy')
0 ignored issues
show
Bug introduced by
The method method() does not exist on DembeloMain\Model\Reposi...UserRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to DembeloMain\Model\Reposi...UserRepositoryInterface. ( Ignorable by Annotation )

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

223
        $this->userRepositoryMock->/** @scrutinizer ignore-call */ 
224
                                   method('findOneBy')
Loading history...
224
            ->willReturn($userMock);
225
226
        $this->controller->activateemailAction($hash);
227
    }
228
229
    /**
230
     * @return void
231
     */
232
    public function testLoginCheckAction(): void
233
    {
234
        self::assertNull($this->controller->loginCheckAction());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->controller->loginCheckAction() targeting DembeloMain\Controller\U...ler::loginCheckAction() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
235
    }
236
}
237