|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\Tests\Command; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\MediaBundle\Command\CleanMediaCommand; |
|
17
|
|
|
use Sonata\MediaBundle\Filesystem\Local; |
|
18
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
|
19
|
|
|
use Sonata\MediaBundle\Model\MediaManagerInterface; |
|
20
|
|
|
use Sonata\MediaBundle\Provider\FileProvider; |
|
21
|
|
|
use Sonata\MediaBundle\Provider\Pool; |
|
22
|
|
|
use Sonata\MediaBundle\Tests\Fixtures\FilesystemTestCase; |
|
23
|
|
|
use Symfony\Component\Console\Application; |
|
24
|
|
|
use Symfony\Component\Console\Command\Command; |
|
25
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
26
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @author Sullivan Senechal <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
class CleanMediaCommandTest extends FilesystemTestCase |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var Application |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $application; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Command |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $command; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var CommandTester |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $tester; |
|
47
|
|
|
|
|
48
|
|
|
private $pool; |
|
49
|
|
|
|
|
50
|
|
|
private $mediaManager; |
|
51
|
|
|
|
|
52
|
|
|
private $fileSystemLocal; |
|
53
|
|
|
|
|
54
|
|
|
protected function setUp(): void |
|
55
|
|
|
{ |
|
56
|
|
|
parent::setUp(); |
|
57
|
|
|
|
|
58
|
|
|
$this->pool = $pool = $this->createMock(Pool::class); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
$this->mediaManager = $mediaManager = $this->createMock(MediaManagerInterface::class); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
$this->fileSystemLocal = $fileSystemLocal = $this->createMock(Local::class); |
|
|
|
|
|
|
63
|
|
|
$this->fileSystemLocal->expects($this->once())->method('getDirectory')->willReturn($this->workspace); |
|
64
|
|
|
|
|
65
|
|
|
$this->command = new CleanMediaCommand($this->fileSystemLocal, $this->pool, $this->mediaManager); |
|
66
|
|
|
|
|
67
|
|
|
$this->application = new Application(); |
|
68
|
|
|
$this->application->add($this->command); |
|
69
|
|
|
|
|
70
|
|
|
$this->tester = new CommandTester($this->application->find('sonata:media:clean-uploads')); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testExecuteDirectoryNotExists(): void |
|
74
|
|
|
{ |
|
75
|
|
|
$context = [ |
|
76
|
|
|
'providers' => [], |
|
77
|
|
|
'formats' => [], |
|
78
|
|
|
'download' => [], |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
$this->pool->expects($this->once())->method('getContexts')->willReturn(['foo' => $context]); |
|
82
|
|
|
|
|
83
|
|
|
$output = $this->tester->execute(['command' => $this->command->getName()]); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertRegExp('@\'.+\' does not exist\s+done!@', $this->tester->getDisplay()); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertSame(0, $output); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testExecuteEmptyDirectory(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$this->filesystem->mkdir($this->workspace.\DIRECTORY_SEPARATOR.'foo'); |
|
93
|
|
|
|
|
94
|
|
|
$context = [ |
|
95
|
|
|
'providers' => [], |
|
96
|
|
|
'formats' => [], |
|
97
|
|
|
'download' => [], |
|
98
|
|
|
]; |
|
99
|
|
|
|
|
100
|
|
|
$this->pool->expects($this->once())->method('getContexts')->willReturn(['foo' => $context]); |
|
101
|
|
|
|
|
102
|
|
|
$output = $this->tester->execute(['command' => $this->command->getName()]); |
|
103
|
|
|
|
|
104
|
|
|
$this->assertRegExp('@Context: foo\s+done!@', $this->tester->getDisplay()); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertSame(0, $output); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testExecuteFilesExists(): void |
|
110
|
|
|
{ |
|
111
|
|
|
$this->filesystem->mkdir($this->workspace.\DIRECTORY_SEPARATOR.'foo'); |
|
112
|
|
|
$this->filesystem->touch($this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'qwertz.ext'); |
|
113
|
|
|
$this->filesystem->touch($this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'thumb_1_bar.ext'); |
|
114
|
|
|
|
|
115
|
|
|
$context = [ |
|
116
|
|
|
'providers' => [], |
|
117
|
|
|
'formats' => [], |
|
118
|
|
|
'download' => [], |
|
119
|
|
|
]; |
|
120
|
|
|
|
|
121
|
|
|
$provider = $this->createMock(FileProvider::class); |
|
122
|
|
|
$provider->method('getName')->willReturn('fooprovider'); |
|
123
|
|
|
|
|
124
|
|
|
$this->pool->method('getContexts')->willReturn(['foo' => $context]); |
|
125
|
|
|
$this->pool->method('getProviders')->willReturn([$provider]); |
|
126
|
|
|
|
|
127
|
|
|
$media = $this->createMock(MediaInterface::class); |
|
128
|
|
|
|
|
129
|
|
|
$this->mediaManager->expects($this->once())->method('findOneBy') |
|
130
|
|
|
->with($this->equalTo(['id' => 1, 'context' => 'foo'])) |
|
131
|
|
|
->willReturn([$media]); |
|
132
|
|
|
$this->mediaManager->expects($this->once())->method('findBy') |
|
133
|
|
|
->with($this->equalTo(['providerReference' => 'qwertz.ext', 'providerName' => ['fooprovider']])) |
|
134
|
|
|
->willReturn([$media]); |
|
135
|
|
|
|
|
136
|
|
|
$output = $this->tester->execute(['command' => $this->command->getName()]); |
|
137
|
|
|
|
|
138
|
|
|
$this->assertRegExp('@Context: foo\s+done!@', $this->tester->getDisplay()); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertSame(0, $output); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function testExecuteFilesExistsVerbose(): void |
|
144
|
|
|
{ |
|
145
|
|
|
$this->filesystem->mkdir($this->workspace.\DIRECTORY_SEPARATOR.'foo'); |
|
146
|
|
|
$this->filesystem->touch($this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'qwertz.ext'); |
|
147
|
|
|
$this->filesystem->touch($this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'thumb_1_bar.ext'); |
|
148
|
|
|
|
|
149
|
|
|
$context = [ |
|
150
|
|
|
'providers' => [], |
|
151
|
|
|
'formats' => [], |
|
152
|
|
|
'download' => [], |
|
153
|
|
|
]; |
|
154
|
|
|
|
|
155
|
|
|
$provider = $this->createMock(FileProvider::class); |
|
156
|
|
|
$provider->method('getName')->willReturn('fooprovider'); |
|
157
|
|
|
|
|
158
|
|
|
$this->pool->method('getContexts')->willReturn(['foo' => $context]); |
|
159
|
|
|
$this->pool->method('getProviders')->willReturn([$provider]); |
|
160
|
|
|
|
|
161
|
|
|
$media = $this->createMock(MediaInterface::class); |
|
162
|
|
|
|
|
163
|
|
|
$this->mediaManager->expects($this->once())->method('findOneBy') |
|
164
|
|
|
->with($this->equalTo(['id' => 1, 'context' => 'foo'])) |
|
165
|
|
|
->willReturn([$media]); |
|
166
|
|
|
$this->mediaManager->expects($this->once())->method('findBy') |
|
167
|
|
|
->with($this->equalTo(['providerReference' => 'qwertz.ext', 'providerName' => ['fooprovider']])) |
|
168
|
|
|
->willReturn([$media]); |
|
169
|
|
|
|
|
170
|
|
|
$output = $this->tester->execute( |
|
171
|
|
|
['command' => $this->command->getName()], |
|
172
|
|
|
['verbosity' => OutputInterface::VERBOSITY_VERBOSE] |
|
173
|
|
|
); |
|
174
|
|
|
|
|
175
|
|
|
$this->assertOutputFoundInContext( |
|
176
|
|
|
'/Context: foo\s+(.+)\s+done!/ms', |
|
177
|
|
|
[ |
|
178
|
|
|
'\'qwertz.ext\' found', |
|
179
|
|
|
'\'thumb_1_bar.ext\' found', |
|
180
|
|
|
], |
|
181
|
|
|
$this->tester->getDisplay() |
|
182
|
|
|
); |
|
183
|
|
|
$this->assertSame(0, $output); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function testExecuteDryRun(): void |
|
187
|
|
|
{ |
|
188
|
|
|
$this->filesystem->mkdir($this->workspace.\DIRECTORY_SEPARATOR.'foo'); |
|
189
|
|
|
$this->filesystem->touch($this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'qwertz.ext'); |
|
190
|
|
|
$this->filesystem->touch($this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'thumb_1_bar.ext'); |
|
191
|
|
|
|
|
192
|
|
|
$context = [ |
|
193
|
|
|
'providers' => [], |
|
194
|
|
|
'formats' => [], |
|
195
|
|
|
'download' => [], |
|
196
|
|
|
]; |
|
197
|
|
|
|
|
198
|
|
|
$provider = $this->createMock(FileProvider::class); |
|
199
|
|
|
$provider->method('getName')->willReturn('fooprovider'); |
|
200
|
|
|
|
|
201
|
|
|
$this->pool->method('getContexts')->willReturn(['foo' => $context]); |
|
202
|
|
|
$this->pool->method('getProviders')->willReturn([$provider]); |
|
203
|
|
|
|
|
204
|
|
|
$this->mediaManager->expects($this->once())->method('findOneBy') |
|
205
|
|
|
->with($this->equalTo(['id' => 1, 'context' => 'foo'])) |
|
206
|
|
|
->willReturn(null); |
|
207
|
|
|
$this->mediaManager->expects($this->once())->method('findBy') |
|
208
|
|
|
->with($this->equalTo(['providerReference' => 'qwertz.ext', 'providerName' => ['fooprovider']])) |
|
209
|
|
|
->willReturn([]); |
|
210
|
|
|
|
|
211
|
|
|
$output = $this->tester->execute(['command' => $this->command->getName(), '--dry-run' => true]); |
|
212
|
|
|
|
|
213
|
|
|
$this->assertOutputFoundInContext( |
|
214
|
|
|
'/Context: foo\s+(.+)\s+done!/ms', |
|
215
|
|
|
[ |
|
216
|
|
|
'\'qwertz.ext\' is orphanend', |
|
217
|
|
|
'\'thumb_1_bar.ext\' is orphanend', |
|
218
|
|
|
], |
|
219
|
|
|
$this->tester->getDisplay() |
|
220
|
|
|
); |
|
221
|
|
|
$this->assertSame(0, $output); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function testExecute(): void |
|
225
|
|
|
{ |
|
226
|
|
|
$this->filesystem->mkdir($this->workspace.\DIRECTORY_SEPARATOR.'foo'); |
|
227
|
|
|
$this->filesystem->touch($this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'qwertz.ext'); |
|
228
|
|
|
$this->filesystem->touch($this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'thumb_1_bar.ext'); |
|
229
|
|
|
|
|
230
|
|
|
$context = [ |
|
231
|
|
|
'providers' => [], |
|
232
|
|
|
'formats' => [], |
|
233
|
|
|
'download' => [], |
|
234
|
|
|
]; |
|
235
|
|
|
|
|
236
|
|
|
$provider = $this->createMock(FileProvider::class); |
|
237
|
|
|
$provider->method('getName')->willReturn('fooprovider'); |
|
238
|
|
|
|
|
239
|
|
|
$this->pool->method('getContexts')->willReturn(['foo' => $context]); |
|
240
|
|
|
$this->pool->method('getProviders')->willReturn([$provider]); |
|
241
|
|
|
|
|
242
|
|
|
$this->mediaManager->expects($this->once())->method('findOneBy') |
|
243
|
|
|
->with($this->equalTo(['id' => 1, 'context' => 'foo'])) |
|
244
|
|
|
->willReturn(null); |
|
245
|
|
|
$this->mediaManager->expects($this->once())->method('findBy') |
|
246
|
|
|
->with($this->equalTo(['providerReference' => 'qwertz.ext', 'providerName' => ['fooprovider']])) |
|
247
|
|
|
->willReturn([]); |
|
248
|
|
|
|
|
249
|
|
|
$output = $this->tester->execute(['command' => $this->command->getName()]); |
|
250
|
|
|
|
|
251
|
|
|
$this->assertOutputFoundInContext( |
|
252
|
|
|
'/Context: foo\s+(.+)\s+done!/ms', |
|
253
|
|
|
[ |
|
254
|
|
|
'\'qwertz.ext\' was successfully removed', |
|
255
|
|
|
'\'thumb_1_bar.ext\' was successfully removed', |
|
256
|
|
|
], |
|
257
|
|
|
$this->tester->getDisplay() |
|
258
|
|
|
); |
|
259
|
|
|
$this->assertSame(0, $output); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Asserts whether all expected texts can be found in the output within a given context. |
|
264
|
|
|
*/ |
|
265
|
|
|
private function assertOutputFoundInContext( |
|
266
|
|
|
string $extractor, |
|
267
|
|
|
array $expected, |
|
268
|
|
|
string $output |
|
269
|
|
|
): void { |
|
270
|
|
|
preg_match_all($extractor, $output, $matches); |
|
271
|
|
|
|
|
272
|
|
|
$found = false; |
|
273
|
|
|
foreach ($matches[1] as $match) { |
|
274
|
|
|
if ($this->containsAll($match, $expected)) { |
|
275
|
|
|
$found = true; |
|
276
|
|
|
|
|
277
|
|
|
break; |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
$this->assertTrue($found, sprintf( |
|
282
|
|
|
'Unable to find "%s" in "%s" with extractor "%s"', |
|
283
|
|
|
implode('", "', $expected), |
|
284
|
|
|
$output, |
|
285
|
|
|
$extractor |
|
286
|
|
|
)); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Returns whether every needle can be found as a substring of the haystack. |
|
291
|
|
|
*/ |
|
292
|
|
|
private function containsAll(string $haystack, array $needles): bool |
|
293
|
|
|
{ |
|
294
|
|
|
foreach ($needles as $needle) { |
|
295
|
|
|
if (false === strpos($haystack, $needle)) { |
|
296
|
|
|
return false; |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
return true; |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.