1
|
|
|
<?php |
2
|
|
|
/* Copyright (C) 2017 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 AdminBundle\Tests\Controller; |
21
|
|
|
|
22
|
|
|
use AdminBundle\Controller\UserController; |
23
|
|
|
use DembeloMain\Document\User; |
24
|
|
|
use DembeloMain\Model\Repository\UserRepositoryInterface; |
25
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
26
|
|
|
use Symfony\Component\HttpFoundation\Response; |
27
|
|
|
use Symfony\Component\HttpFoundation\Request; |
28
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class UserControllerTest |
32
|
|
|
* @package AdminBundle\Tests\Controller |
33
|
|
|
*/ |
34
|
|
|
class UserControllerTest extends WebTestCase |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var UserController |
38
|
|
|
*/ |
39
|
|
|
private $controller; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|UserRepositoryInterface |
43
|
|
|
*/ |
44
|
|
|
private $userRepositoryMock; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|\Swift_Mailer |
48
|
|
|
*/ |
49
|
|
|
private $mailerMock; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
protected function setUp(): void |
55
|
|
|
{ |
56
|
|
|
$this->userRepositoryMock = $this->createUserRepositoryMock(); |
57
|
|
|
$this->mailerMock = $this->createMailerMock(); |
58
|
|
|
|
59
|
|
|
$this->controller = new UserController( |
60
|
|
|
$this->userRepositoryMock, |
61
|
|
|
$this->mailerMock |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* tests controller's userAction with no users in db |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public function testUserAction(): void |
70
|
|
|
{ |
71
|
|
|
$requestMock = $this->createRequestMock(); |
72
|
|
|
$queryMock = $this->getMockBuilder('foobar')->setMethods(array('execute', 'getQuery'))->getMock(); |
73
|
|
|
|
74
|
|
|
$queryMock->expects($this->once()) |
75
|
|
|
->method('getQuery') |
76
|
|
|
->will($this->returnSelf()); |
77
|
|
|
|
78
|
|
|
$queryMock->expects($this->once()) |
79
|
|
|
->method('execute') |
80
|
|
|
->will($this->returnValue(array())); |
81
|
|
|
|
82
|
|
|
$this->userRepositoryMock->expects($this->once()) |
83
|
|
|
->method('createQueryBuilder') |
84
|
|
|
->will($this->returnValue($queryMock)); |
85
|
|
|
|
86
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
87
|
|
|
$response = $this->controller->usersAction($requestMock); |
88
|
|
|
$this->assertInstanceOf(Response::class, $response); |
89
|
|
|
$this->assertJsonStringEqualsJsonString('[]', $response->getContent()); |
90
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* tests controller's userAction with two users in db |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
public function testUserActionWithUsers(): void |
98
|
|
|
{ |
99
|
|
|
$requestMock = $this->createRequestMock(); |
100
|
|
|
$queryMock = $this->getMockBuilder('foobar')->setMethods(['execute', 'getQuery'])->getMock(); |
101
|
|
|
|
102
|
|
|
$queryMock->expects($this->once()) |
103
|
|
|
->method('getQuery') |
104
|
|
|
->will($this->returnSelf()); |
105
|
|
|
|
106
|
|
|
$this->userRepositoryMock->expects($this->once()) |
107
|
|
|
->method('createQueryBuilder') |
108
|
|
|
->will($this->returnValue($queryMock)); |
109
|
|
|
|
110
|
|
|
$user1 = new User(); |
111
|
|
|
$user1->setEmail('email1'); |
112
|
|
|
$user1->setId('id1'); |
113
|
|
|
$user1->setRoles('ROLE_ADMIN'); |
114
|
|
|
$user1->setLicenseeId('lic1'); |
115
|
|
|
$user2 = new User(); |
116
|
|
|
$user2->setEmail('email2'); |
117
|
|
|
$user2->setId('id2'); |
118
|
|
|
$user2->setRoles('ROLE_USER'); |
119
|
|
|
$user2->setLicenseeId('lic2'); |
120
|
|
|
|
121
|
|
|
$userArray = array( |
122
|
|
|
$user1, |
123
|
|
|
$user2, |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
$queryMock->expects($this->once()) |
127
|
|
|
->method('execute') |
128
|
|
|
->will($this->returnValue($userArray)); |
129
|
|
|
|
130
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
131
|
|
|
$response = $this->controller->usersAction($requestMock); |
132
|
|
|
$this->assertInstanceOf(Response::class, $response); |
133
|
|
|
$this->assertJsonStringEqualsJsonString('[{"id":"id1","gender":null,"email":"email1","roles":"ROLE_ADMIN","licenseeId":"lic1","status":null,"source":null,"reason":null,"created":"'.date('Y-m-d H:i:s', 0).'","updated":"'.date('Y-m-d H:i:s', 0).'"},{"id":"id2","email":"email2","roles":"ROLE_USER","licenseeId":"lic2","status":null,"source":null,"reason":null,"gender":null,"created":"'.date('Y-m-d H:i:s', 0).'","updated":"'.date('Y-m-d H:i:s', 0).'"}]', $response->getContent()); |
134
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return UserRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
private function createUserRepositoryMock(): UserRepositoryInterface |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
return $this->getMockBuilder(UserRepositoryInterface::class) |
|
|
|
|
143
|
|
|
->disableOriginalConstructor() |
144
|
|
|
->setMethods(['createQueryBuilder', 'find', 'findByEmail', 'findAll', 'save', 'findBy', 'findOneBy', 'getClassName']) |
145
|
|
|
->getMock(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return \Swift_Mailer|\PHPUnit_Framework_MockObject_MockObject |
150
|
|
|
*/ |
151
|
|
|
private function createMailerMock(): \Swift_Mailer |
152
|
|
|
{ |
153
|
|
|
return $this->createMock(\Swift_Mailer::class); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Request |
158
|
|
|
*/ |
159
|
|
|
private function createRequestMock() |
160
|
|
|
{ |
161
|
|
|
$requestMock = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); |
162
|
|
|
$postMock = $this->getMockBuilder(ParameterBag::class)->disableOriginalConstructor()->getMock(); |
163
|
|
|
$postArray = []; |
164
|
|
|
$postMock->expects($this->once()) |
165
|
|
|
->method('get') |
166
|
|
|
->will($this->returnValue($postArray)); |
167
|
|
|
$requestMock->query = $postMock; |
|
|
|
|
168
|
|
|
|
169
|
|
|
return $requestMock; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.