|
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
|
|
|
/** |
|
21
|
|
|
* @package AdminBundle |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
// @codingStandardsIgnoreStart |
|
25
|
|
|
|
|
26
|
|
|
namespace AdminBundle\Command; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $filename |
|
30
|
|
|
* @return bool |
|
31
|
|
|
*/ |
|
32
|
|
|
function is_readable($filename) |
|
33
|
|
|
{ |
|
34
|
|
|
return \strpos($filename, 'readable') !== false; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $filename |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
|
|
function file_exists($filename) |
|
42
|
|
|
{ |
|
43
|
|
|
return \strpos($filename, 'exists') !== false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
namespace AdminBundle\Tests\Command; |
|
47
|
|
|
|
|
48
|
|
|
use AdminBundle\Service\TwineImport\ImportTwine; |
|
49
|
|
|
use DembeloMain\Document\Importfile; |
|
50
|
|
|
use DembeloMain\Document\Licensee; |
|
51
|
|
|
use DembeloMain\Document\Topic; |
|
52
|
|
|
use DembeloMain\Model\Repository\ImportfileRepositoryInterface; |
|
53
|
|
|
use DembeloMain\Model\Repository\LicenseeRepositoryInterface; |
|
54
|
|
|
use DembeloMain\Model\Repository\TopicRepositoryInterface; |
|
55
|
|
|
use PHPUnit\Framework\TestCase; |
|
56
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
57
|
|
|
use AdminBundle\Command\ImportCommand; |
|
58
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
59
|
|
|
|
|
60
|
|
|
// @codingStandardsIgnoreEnd |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Class ImportCommandTest |
|
64
|
|
|
*/ |
|
65
|
|
|
class ImportCommandTest extends TestCase |
|
66
|
|
|
{ |
|
67
|
|
|
/** |
|
68
|
|
|
* @var ContainerAwareCommand |
|
69
|
|
|
*/ |
|
70
|
|
|
private $command; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var CommandTester |
|
74
|
|
|
*/ |
|
75
|
|
|
private $commandTester; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var ImportTwine|\PHPUnit_Framework_MockObject_MockObject |
|
79
|
|
|
*/ |
|
80
|
|
|
private $importTwineMock; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @var LicenseeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
|
84
|
|
|
*/ |
|
85
|
|
|
private $licenseeRepositoryMock; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @var TopicRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
|
89
|
|
|
*/ |
|
90
|
|
|
private $topicRepositoryMock; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @var ImportfileRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
|
94
|
|
|
*/ |
|
95
|
|
|
private $importfileRepositoryMock; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @inheritdoc |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function setUp(): void |
|
101
|
|
|
{ |
|
102
|
|
|
$this->importTwineMock = $this->createImportTwineMock(); |
|
103
|
|
|
$this->licenseeRepositoryMock = $this->createLicenseeRepositoryMock(); |
|
104
|
|
|
$this->topicRepositoryMock = $this->createTopicRepositoryMock(); |
|
105
|
|
|
$this->importfileRepositoryMock = $this->createImportfileRepositoryMock(); |
|
106
|
|
|
|
|
107
|
|
|
$this->command = new ImportCommand( |
|
|
|
|
|
|
108
|
|
|
$this->importTwineMock, |
|
109
|
|
|
$this->licenseeRepositoryMock, |
|
110
|
|
|
$this->topicRepositoryMock, |
|
111
|
|
|
$this->importfileRepositoryMock |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
$this->commandTester = new CommandTester($this->command); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* tests the execute method |
|
119
|
|
|
* |
|
120
|
|
|
* @return void |
|
121
|
|
|
*/ |
|
122
|
|
|
public function testExecute(): void |
|
123
|
|
|
{ |
|
124
|
|
|
$this->importTwineMock->expects($this->once()) |
|
125
|
|
|
->method('run') |
|
126
|
|
|
// @codingStandardsIgnoreStart |
|
127
|
|
|
->will($this->returnCallback(function (Importfile $importfile): bool { |
|
128
|
|
|
return $importfile->getFilename() === 'somefile_readable_exists.html' |
|
129
|
|
|
&& $importfile->getLicenseeId() === 'licenseeId' |
|
130
|
|
|
&& $importfile->getAuthor() === 'someauthor' |
|
131
|
|
|
&& $importfile->getPublisher() === 'somepublisher' |
|
132
|
|
|
&& $importfile->getTopicId() === 'someTopic'; |
|
133
|
|
|
})); |
|
134
|
|
|
// @codingStandardsIgnoreEnd |
|
135
|
|
|
|
|
136
|
|
|
$returnValue = $this->commandTester->execute([ |
|
137
|
|
|
'twine-archive-file' => 'somefile_readable_exists.html', |
|
138
|
|
|
'--licensee-name' => 'somelicensee', |
|
139
|
|
|
'--metadata-author' => 'someauthor', |
|
140
|
|
|
'--metadata-publisher' => 'somepublisher', |
|
141
|
|
|
'--topic-name' => 'someTopic', |
|
142
|
|
|
]); |
|
143
|
|
|
|
|
144
|
|
|
// the output of the command in the console |
|
145
|
|
|
$output = $this->commandTester->getDisplay(); |
|
146
|
|
|
$this->assertEquals('', $output); |
|
147
|
|
|
$this->assertEquals(0, $returnValue); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* tests execute method with unreadable file |
|
152
|
|
|
* |
|
153
|
|
|
* @return void |
|
154
|
|
|
*/ |
|
155
|
|
|
public function testExecuteWithUnreadableFile(): void |
|
156
|
|
|
{ |
|
157
|
|
|
$this->importTwineMock->expects($this->never()) |
|
158
|
|
|
->method('run'); |
|
159
|
|
|
|
|
160
|
|
|
$returnValue = $this->commandTester->execute([ |
|
161
|
|
|
'twine-archive-file' => 'somefile_exists.html', |
|
162
|
|
|
'--licensee-name' => 'somelicensee', |
|
163
|
|
|
'--metadata-author' => 'someauthor', |
|
164
|
|
|
'--metadata-publisher' => 'somepublisher', |
|
165
|
|
|
'--topic-name' => 'someTopic', |
|
166
|
|
|
]); |
|
167
|
|
|
|
|
168
|
|
|
// the output of the command in the console |
|
169
|
|
|
$output = $this->commandTester->getDisplay(); |
|
170
|
|
|
$this->assertContains('isn\'t readable', $output); |
|
171
|
|
|
$this->assertEquals(-1, $returnValue); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* tests execute() method with non existing file |
|
176
|
|
|
* |
|
177
|
|
|
* @return void |
|
178
|
|
|
*/ |
|
179
|
|
|
public function testExecuteWithFileNotExisting(): void |
|
180
|
|
|
{ |
|
181
|
|
|
$this->importTwineMock->expects($this->never()) |
|
182
|
|
|
->method('run'); |
|
183
|
|
|
|
|
184
|
|
|
$returnValue = $this->commandTester->execute([ |
|
185
|
|
|
'twine-archive-file' => 'somefile_readable.html', |
|
186
|
|
|
'--licensee-name' => 'somelicensee', |
|
187
|
|
|
'--metadata-author' => 'someauthor', |
|
188
|
|
|
'--metadata-publisher' => 'somepublisher', |
|
189
|
|
|
'--topic-name' => 'someTopic', |
|
190
|
|
|
]); |
|
191
|
|
|
|
|
192
|
|
|
// the output of the command in the console |
|
193
|
|
|
$output = $this->commandTester->getDisplay(); |
|
194
|
|
|
$this->assertContains('doesn\'t exist', $output); |
|
195
|
|
|
$this->assertEquals(-1, $returnValue); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* tests execute() method with exception thrown by importTwine |
|
200
|
|
|
* |
|
201
|
|
|
* @return void |
|
202
|
|
|
*/ |
|
203
|
|
|
public function testExecuteWithExeptionInImportTwine(): void |
|
204
|
|
|
{ |
|
205
|
|
|
$this->importTwineMock->expects($this->once()) |
|
206
|
|
|
->method('run') |
|
207
|
|
|
// @codingStandardsIgnoreStart |
|
208
|
|
|
->will($this->returnCallback(function (Importfile $importfile): bool { |
|
209
|
|
|
return $importfile->getFilename() === 'somefile_readable_exists.html' |
|
210
|
|
|
&& $importfile->getLicenseeId() === 'licenseeId' |
|
211
|
|
|
&& $importfile->getAuthor() === 'someauthor' |
|
212
|
|
|
&& $importfile->getPublisher() === 'somepublisher' |
|
213
|
|
|
&& $importfile->getTopicId() === 'someTopic'; |
|
214
|
|
|
})) |
|
215
|
|
|
// @codingStandardsIgnoreEnd |
|
216
|
|
|
->will($this->throwException(new \Exception('dummy Exception'))); |
|
217
|
|
|
|
|
218
|
|
|
$this->importTwineMock->expects($this->once()) |
|
219
|
|
|
->method('parserFree'); |
|
220
|
|
|
|
|
221
|
|
|
$returnValue = $this->commandTester->execute(array( |
|
222
|
|
|
'twine-archive-file' => 'somefile_readable_exists.html', |
|
223
|
|
|
'--licensee-name' => 'somelicensee', |
|
224
|
|
|
'--metadata-author' => 'someauthor', |
|
225
|
|
|
'--metadata-publisher' => 'somepublisher', |
|
226
|
|
|
'--topic-name' => 'someTopic', |
|
227
|
|
|
)); |
|
228
|
|
|
|
|
229
|
|
|
// the output of the command in the console |
|
230
|
|
|
$output = $this->commandTester->getDisplay(); |
|
231
|
|
|
$this->assertContains('dummy Exception', $output); |
|
232
|
|
|
$this->assertEquals(-1, $returnValue); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @expectedException \Exception |
|
237
|
|
|
*/ |
|
238
|
|
|
public function testExecuteWithLicenseeNotFound(): void |
|
239
|
|
|
{ |
|
240
|
|
|
$this->importTwineMock->expects($this->never()) |
|
241
|
|
|
->method('run'); |
|
242
|
|
|
|
|
243
|
|
|
$returnValue = $this->commandTester->execute(array( |
|
244
|
|
|
'command' => $this->command->getName(), |
|
245
|
|
|
'twine-archive-file' => 'somefile_exists_readable.html', |
|
246
|
|
|
'--licensee-name' => 'somelicensee2', |
|
247
|
|
|
'--metadata-author' => 'someauthor', |
|
248
|
|
|
'--metadata-publisher' => 'somepublisher', |
|
249
|
|
|
)); |
|
250
|
|
|
|
|
251
|
|
|
// the output of the command in the console |
|
252
|
|
|
$output = $this->commandTester->getDisplay(); |
|
253
|
|
|
$this->assertContains('dummy Exception', $output); |
|
254
|
|
|
$this->assertEquals(-1, $returnValue); |
|
255
|
|
|
$this->expectException(\Exception::class); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* method for returnCallback() |
|
260
|
|
|
* |
|
261
|
|
|
* @param string $arg |
|
262
|
|
|
* |
|
263
|
|
|
* @return null|\PHPUnit_Framework_MockObject_MockObject|Licensee |
|
264
|
|
|
*/ |
|
265
|
|
|
public function findOneByNameCallback($arg): ?Licensee |
|
266
|
|
|
{ |
|
267
|
|
|
if ('somelicensee' !== $arg) { |
|
268
|
|
|
return null; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
$this->licenseeRepositoryMock->expects($this->once()) |
|
272
|
|
|
->method('getId') |
|
273
|
|
|
->will($this->returnValue('licenseeId')); |
|
274
|
|
|
|
|
275
|
|
|
return $this->licenseeRepositoryMock; |
|
|
|
|
|
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* method for returnCallback() |
|
280
|
|
|
* |
|
281
|
|
|
* @param string $arg |
|
282
|
|
|
* |
|
283
|
|
|
* @return null|\PHPUnit_Framework_MockObject_MockObject|Topic |
|
284
|
|
|
*/ |
|
285
|
|
|
public function findOneTopicByNameCallback($arg): ?Topic |
|
286
|
|
|
{ |
|
287
|
|
|
if ('someTopic' !== $arg) { |
|
288
|
|
|
return null; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
$topicMock = $this->getMockBuilder(Topic::class)->disableOriginalConstructor()->getMock(); |
|
292
|
|
|
$topicMock->expects($this->once()) |
|
293
|
|
|
->method('getId') |
|
294
|
|
|
->will($this->returnValue('topicId')); |
|
295
|
|
|
|
|
296
|
|
|
return $topicMock; |
|
|
|
|
|
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* @return ImportTwine|\PHPUnit_Framework_MockObject_MockObject |
|
301
|
|
|
*/ |
|
302
|
|
|
private function createImportTwineMock(): ImportTwine |
|
303
|
|
|
{ |
|
304
|
|
|
return $this->createMock(ImportTwine::class); |
|
|
|
|
|
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* @return LicenseeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
|
309
|
|
|
*/ |
|
310
|
|
|
private function createLicenseeRepositoryMock(): LicenseeRepositoryInterface |
|
311
|
|
|
{ |
|
312
|
|
|
$mock = $this->createMock(LicenseeRepositoryInterface::class); |
|
313
|
|
|
|
|
314
|
|
|
$licenseeMock = $this->createMock(Licensee::class); |
|
315
|
|
|
$licenseeMock->method('getId')->willReturn('someLicenseeId'); |
|
|
|
|
|
|
316
|
|
|
|
|
317
|
|
|
$mock->method('findOneBy') |
|
318
|
|
|
->with(['name' => 'somelicensee']) |
|
319
|
|
|
->willReturn($licenseeMock); |
|
320
|
|
|
|
|
321
|
|
|
return $mock; |
|
|
|
|
|
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* @return TopicRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
|
326
|
|
|
*/ |
|
327
|
|
|
private function createTopicRepositoryMock(): TopicRepositoryInterface |
|
328
|
|
|
{ |
|
329
|
|
|
$mock = $this->createMock(TopicRepositoryInterface::class); |
|
330
|
|
|
|
|
331
|
|
|
$topicMock = $this->createMock(Topic::class); |
|
332
|
|
|
$topicMock->method('getId')->willReturn('someTopicId'); |
|
333
|
|
|
|
|
334
|
|
|
$mock->method('findOneBy') |
|
335
|
|
|
->willReturn(['name' => 'someTopic']) |
|
336
|
|
|
->willReturn($topicMock); |
|
337
|
|
|
|
|
338
|
|
|
return $mock; |
|
|
|
|
|
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* @return ImportfileRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
|
343
|
|
|
*/ |
|
344
|
|
|
private function createImportfileRepositoryMock(): ImportfileRepositoryInterface |
|
345
|
|
|
{ |
|
346
|
|
|
return $this->createMock(ImportfileRepositoryInterface::class); |
|
|
|
|
|
|
347
|
|
|
} |
|
348
|
|
|
} |
|
349
|
|
|
|
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..