|
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
|
|
|
namespace AdminBundle\Tests\Controller; |
|
20
|
|
|
|
|
21
|
|
|
use AdminBundle\Controller\UserController; |
|
22
|
|
|
use DembeloMain\Document\User; |
|
23
|
|
|
use DembeloMain\Model\Repository\UserRepositoryInterface; |
|
24
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
|
25
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
26
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
27
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
|
28
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as Templating; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Class UserControllerTest |
|
32
|
|
|
*/ |
|
33
|
|
|
class UserControllerTest extends WebTestCase |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var UserController |
|
37
|
|
|
*/ |
|
38
|
|
|
private $controller; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|UserRepositoryInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private $userRepositoryMock; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|\Swift_Mailer |
|
47
|
|
|
*/ |
|
48
|
|
|
private $mailerMock; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var Templating|\PHPUnit_Framework_MockObject_MockObject |
|
52
|
|
|
*/ |
|
53
|
|
|
private $templatingMock; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return void |
|
57
|
|
|
* |
|
58
|
|
|
* @throws \ReflectionException |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function setUp(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->userRepositoryMock = $this->createUserRepositoryMock(); |
|
63
|
|
|
$this->mailerMock = $this->createMailerMock(); |
|
64
|
|
|
$this->templatingMock = $this->createMock(Templating::class); |
|
65
|
|
|
|
|
66
|
|
|
$this->controller = new UserController( |
|
67
|
|
|
$this->userRepositoryMock, |
|
68
|
|
|
$this->mailerMock, |
|
69
|
|
|
$this->templatingMock |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* tests controller's userAction with no users in db |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
public function testUserAction(): void |
|
78
|
|
|
{ |
|
79
|
|
|
$requestMock = $this->createRequestMock(); |
|
80
|
|
|
$queryMock = $this->getMockBuilder('foobar')->setMethods(array('execute', 'getQuery'))->getMock(); |
|
81
|
|
|
|
|
82
|
|
|
$queryMock->expects($this->once()) |
|
83
|
|
|
->method('getQuery') |
|
84
|
|
|
->will($this->returnSelf()); |
|
85
|
|
|
|
|
86
|
|
|
$queryMock->expects($this->once()) |
|
87
|
|
|
->method('execute') |
|
88
|
|
|
->will($this->returnValue(array())); |
|
89
|
|
|
|
|
90
|
|
|
$this->userRepositoryMock->expects($this->once()) |
|
91
|
|
|
->method('createQueryBuilder') |
|
92
|
|
|
->will($this->returnValue($queryMock)); |
|
93
|
|
|
|
|
94
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
|
95
|
|
|
$response = $this->controller->usersAction($requestMock); |
|
96
|
|
|
$this->assertInstanceOf(Response::class, $response); |
|
97
|
|
|
$this->assertJsonStringEqualsJsonString('[]', $response->getContent()); |
|
98
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* tests controller's userAction with two users in db |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
public function testUserActionWithUsers(): void |
|
106
|
|
|
{ |
|
107
|
|
|
$requestMock = $this->createRequestMock(); |
|
108
|
|
|
$queryMock = $this->getMockBuilder('foobar')->setMethods(['execute', 'getQuery'])->getMock(); |
|
109
|
|
|
|
|
110
|
|
|
$queryMock->expects($this->once()) |
|
111
|
|
|
->method('getQuery') |
|
112
|
|
|
->will($this->returnSelf()); |
|
113
|
|
|
|
|
114
|
|
|
$this->userRepositoryMock->expects($this->once()) |
|
115
|
|
|
->method('createQueryBuilder') |
|
116
|
|
|
->will($this->returnValue($queryMock)); |
|
117
|
|
|
|
|
118
|
|
|
$user1 = new User(); |
|
119
|
|
|
$user1->setEmail('email1'); |
|
120
|
|
|
$user1->setId('id1'); |
|
121
|
|
|
$user1->setRoles('ROLE_ADMIN'); |
|
122
|
|
|
$user1->setLicenseeId('lic1'); |
|
123
|
|
|
$user2 = new User(); |
|
124
|
|
|
$user2->setEmail('email2'); |
|
125
|
|
|
$user2->setId('id2'); |
|
126
|
|
|
$user2->setRoles('ROLE_USER'); |
|
127
|
|
|
$user2->setLicenseeId('lic2'); |
|
128
|
|
|
|
|
129
|
|
|
$userArray = array( |
|
130
|
|
|
$user1, |
|
131
|
|
|
$user2, |
|
132
|
|
|
); |
|
133
|
|
|
|
|
134
|
|
|
$queryMock->expects($this->once()) |
|
135
|
|
|
->method('execute') |
|
136
|
|
|
->will($this->returnValue($userArray)); |
|
137
|
|
|
|
|
138
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
|
139
|
|
|
$response = $this->controller->usersAction($requestMock); |
|
140
|
|
|
$this->assertInstanceOf(Response::class, $response); |
|
141
|
|
|
$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()); |
|
142
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return UserRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
|
147
|
|
|
*/ |
|
148
|
|
|
private function createUserRepositoryMock(): UserRepositoryInterface |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->getMockBuilder(UserRepositoryInterface::class) |
|
|
|
|
|
|
151
|
|
|
->disableOriginalConstructor() |
|
152
|
|
|
->setMethods(['createQueryBuilder', 'find', 'findByEmail', 'findAll', 'save', 'findBy', 'findOneBy', 'getClassName']) |
|
153
|
|
|
->getMock(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return \Swift_Mailer|\PHPUnit_Framework_MockObject_MockObject |
|
158
|
|
|
*/ |
|
159
|
|
|
private function createMailerMock(): \Swift_Mailer |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->createMock(\Swift_Mailer::class); |
|
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Request |
|
166
|
|
|
*/ |
|
167
|
|
|
private function createRequestMock() |
|
168
|
|
|
{ |
|
169
|
|
|
$requestMock = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); |
|
170
|
|
|
$postMock = $this->getMockBuilder(ParameterBag::class)->disableOriginalConstructor()->getMock(); |
|
171
|
|
|
$postArray = []; |
|
172
|
|
|
$postMock->expects($this->once()) |
|
173
|
|
|
->method('get') |
|
174
|
|
|
->will($this->returnValue($postArray)); |
|
175
|
|
|
$requestMock->query = $postMock; |
|
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
return $requestMock; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|