|
1
|
|
|
<?php |
|
2
|
|
|
/* Copyright (C) 2016 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\TopicController; |
|
23
|
|
|
use DembeloMain\Document\Topic; |
|
24
|
|
|
use DembeloMain\Model\Repository\Doctrine\ODM\TopicRepository; |
|
25
|
|
|
use DembeloMain\Model\Repository\TopicRepositoryInterface; |
|
26
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
|
27
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
28
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
|
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
30
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Class TopicControllerTest |
|
34
|
|
|
*/ |
|
35
|
|
|
class TopicControllerTest extends WebTestCase |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var TopicController |
|
39
|
|
|
*/ |
|
40
|
|
|
private $controller; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|TopicRepositoryInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
private $topicRepositoryMock; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function setUp(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$this->topicRepositoryMock = $this->createTopicRepositoryMock(); |
|
53
|
|
|
$this->controller = new TopicController( |
|
54
|
|
|
$this->topicRepositoryMock |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* tests topicAction() with no topics |
|
60
|
|
|
*/ |
|
61
|
|
|
public function testListActionWithNoTopics() |
|
62
|
|
|
{ |
|
63
|
|
|
$parameterBag = $this->getMockBuilder(ParameterBag::class)->disableOriginalConstructor()->setMethods(['get'])->getMock(); |
|
64
|
|
|
|
|
65
|
|
|
$requestMock = $this->getMockBuilder(Request::class)->getMock(); |
|
66
|
|
|
$requestMock->query = $parameterBag; |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
|
69
|
|
|
$response = $this->controller->listAction($requestMock); |
|
70
|
|
|
$this->assertInstanceOf(Response::class, $response); |
|
71
|
|
|
$this->assertJsonStringEqualsJsonString('[]', $response->getContent()); |
|
72
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* tests topicAction with one topic |
|
77
|
|
|
*/ |
|
78
|
|
|
public function testListActionWithOneTopic() |
|
79
|
|
|
{ |
|
80
|
|
|
$topic = new Topic(); |
|
81
|
|
|
$topic->setName('someName'); |
|
82
|
|
|
$topic->setId('someId'); |
|
83
|
|
|
$topic->setStatus(1); |
|
84
|
|
|
$topic->setSortKey(123); |
|
85
|
|
|
$topic->setOriginalImageName("someImageName"); |
|
86
|
|
|
|
|
87
|
|
|
$this->topicRepositoryMock->expects($this->once()) |
|
88
|
|
|
->method('findBy') |
|
89
|
|
|
->willReturn([$topic]); |
|
90
|
|
|
|
|
91
|
|
|
$parameterBag = $this->getMockBuilder(ParameterBag::class)->disableOriginalConstructor()->setMethods(['get'])->getMock(); |
|
92
|
|
|
|
|
93
|
|
|
$requestMock = $this->getMockBuilder(Request::class)->getMock(); |
|
94
|
|
|
$requestMock->query = $parameterBag; |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
|
97
|
|
|
$response = $this->controller->listAction($requestMock); |
|
98
|
|
|
$this->assertInstanceOf(Response::class, $response); |
|
99
|
|
|
$this->assertJsonStringEqualsJsonString('[{"id":"someId","name":"someName","status":"1","sortKey":123,"originalImageName":"someImageName"}]', $response->getContent()); |
|
100
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* tests topicAction with some filters |
|
105
|
|
|
*/ |
|
106
|
|
|
public function testListActionWithSomeFilters() |
|
107
|
|
|
{ |
|
108
|
|
|
$topic = new Topic(); |
|
109
|
|
|
$topic->setName('someName'); |
|
110
|
|
|
$topic->setId('someId'); |
|
111
|
|
|
$topic->setStatus(1); |
|
112
|
|
|
$topic->setSortKey(123); |
|
113
|
|
|
$topic->setOriginalImageName("someImageName"); |
|
114
|
|
|
|
|
115
|
|
|
$this->topicRepositoryMock->expects($this->never()) |
|
116
|
|
|
->method('findBy') |
|
117
|
|
|
->willReturn([$topic]); |
|
118
|
|
|
$this->topicRepositoryMock->expects($this->once()) |
|
119
|
|
|
->method('findFiltered') |
|
120
|
|
|
->with(['status' => '1']) |
|
121
|
|
|
->willReturn([$topic]); |
|
122
|
|
|
|
|
123
|
|
|
$parameterBag = $this->getMockBuilder(ParameterBag::class)->disableOriginalConstructor()->setMethods(['get'])->getMock(); |
|
124
|
|
|
$parameterBag->expects($this->once()) |
|
125
|
|
|
->method('get') |
|
126
|
|
|
->with('filter') |
|
127
|
|
|
->willReturn(['status' => '1']); |
|
128
|
|
|
|
|
129
|
|
|
$requestMock = $this->getMockBuilder(Request::class)->getMock(); |
|
130
|
|
|
$requestMock->query = $parameterBag; |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
|
133
|
|
|
$response = $this->controller->listAction($requestMock); |
|
134
|
|
|
$this->assertInstanceOf(Response::class, $response); |
|
135
|
|
|
$this->assertJsonStringEqualsJsonString('[{"id":"someId","name":"someName","status":"1","sortKey":123,"originalImageName":"someImageName"}]', $response->getContent()); |
|
136
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* tests uploadImageAction with an error in $_FILES |
|
141
|
|
|
*/ |
|
142
|
|
|
public function testUploadImageActionWithNoFileError() |
|
143
|
|
|
{ |
|
144
|
|
|
$parameterBag = $this->getMockBuilder(ParameterBag::class)->disableOriginalConstructor()->setMethods(['get'])->getMock(); |
|
145
|
|
|
$parameterBag->expects($this->never()) |
|
146
|
|
|
->method('get'); |
|
147
|
|
|
|
|
148
|
|
|
$requestMock = $this->getMockBuilder(Request::class)->getMock(); |
|
149
|
|
|
$requestMock->query = $parameterBag; |
|
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
$_FILES['upload'] = [ |
|
152
|
|
|
'error' => UPLOAD_ERR_NO_FILE, |
|
153
|
|
|
]; |
|
154
|
|
|
|
|
155
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
|
156
|
|
|
$response = $this->controller->uploadImageAction($requestMock); |
|
157
|
|
|
$this->assertInstanceOf(Response::class, $response); |
|
158
|
|
|
$this->assertJsonStringEqualsJsonString('{"status":"error"}', $response->getContent()); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* tests uploadImageAction |
|
163
|
|
|
*/ |
|
164
|
|
|
public function testUploadImageActionWithNoError() |
|
165
|
|
|
{ |
|
166
|
|
|
$parameterBag = $this->getMockBuilder(ParameterBag::class)->disableOriginalConstructor()->setMethods(['get'])->getMock(); |
|
167
|
|
|
$parameterBag->expects($this->once()) |
|
168
|
|
|
->method('get') |
|
169
|
|
|
->with('topic_image_directory') |
|
170
|
|
|
->willReturn('someDirectory'); |
|
171
|
|
|
|
|
172
|
|
|
$requestMock = $this->getMockBuilder(Request::class)->getMock(); |
|
173
|
|
|
$requestMock->query = $parameterBag; |
|
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
$_FILES['upload'] = [ |
|
176
|
|
|
'error' => UPLOAD_ERR_OK, |
|
177
|
|
|
'name' => 'someName', |
|
178
|
|
|
'tmp_name' => 'someTmpName', |
|
179
|
|
|
]; |
|
180
|
|
|
|
|
181
|
|
|
/* @var $response \Symfony\Component\HttpFoundation\Response */ |
|
182
|
|
|
$response = $this->controller->uploadImageAction($requestMock); |
|
183
|
|
|
$this->assertInstanceOf(Response::class, $response); |
|
184
|
|
|
$decoded = json_decode($response->getContent()); |
|
185
|
|
|
$this->assertEquals('someName', $decoded->originalImageName); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return TopicRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
|
190
|
|
|
*/ |
|
191
|
|
|
private function createTopicRepositoryMock(): TopicRepositoryInterface |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->createMock(TopicRepositoryInterface::class); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|