|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\MediaBundle\Tests\Command; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\MediaBundle\Command\CleanMediaCommand; |
|
15
|
|
|
use Sonata\MediaBundle\Filesystem\Local; |
|
16
|
|
|
use Sonata\MediaBundle\Model\MediaManagerInterface; |
|
17
|
|
|
use Sonata\MediaBundle\Provider\Pool; |
|
18
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
19
|
|
|
use Symfony\Component\Console\Application; |
|
20
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
22
|
|
|
use Symfony\Component\Filesystem\Tests\FilesystemTestCase; |
|
23
|
|
|
|
|
24
|
|
|
if (class_exists('Symfony\Component\Filesystem\Tests\FilesystemTestCase')) { |
|
25
|
|
|
class TestCase extends FilesystemTestCase |
|
26
|
|
|
{ |
|
27
|
|
|
} |
|
28
|
|
|
} else { |
|
29
|
|
|
class TestCase extends \PHPUnit_Framework_TestCase |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @author Sullivan Senechal <[email protected]> |
|
36
|
|
|
* |
|
37
|
|
|
* @requires function Symfony\Component\Filesystem\Tests\FilesystemTestCase::setUpBeforeClass |
|
38
|
|
|
*/ |
|
39
|
|
|
class CleanMediaCommandTest extends TestCase |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
/** |
|
42
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ContainerInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $container; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var Application |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $application; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var ContainerAwareCommand |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $command; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var CommandTester |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $tester; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|Pool |
|
63
|
|
|
*/ |
|
64
|
|
|
private $pool; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|MediaManagerInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
private $mediaManager; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|Local |
|
73
|
|
|
*/ |
|
74
|
|
|
private $fileSystemLocal; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function setUp() |
|
80
|
|
|
{ |
|
81
|
|
|
parent::setUp(); |
|
82
|
|
|
|
|
83
|
|
|
$this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); |
|
84
|
|
|
|
|
85
|
|
|
$this->command = new CleanMediaCommand(); |
|
86
|
|
|
$this->command->setContainer($this->container); |
|
87
|
|
|
|
|
88
|
|
|
$this->application = new Application(); |
|
89
|
|
|
$this->application->add($this->command); |
|
90
|
|
|
|
|
91
|
|
|
$this->tester = new CommandTester($this->application->find('sonata:media:clean-uploads')); |
|
92
|
|
|
|
|
93
|
|
|
$this->pool = $pool = $this->getMockBuilder('Sonata\MediaBundle\Provider\Pool')->disableOriginalConstructor()->getMock(); |
|
94
|
|
|
|
|
95
|
|
|
$this->mediaManager = $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface'); |
|
96
|
|
|
|
|
97
|
|
|
$this->fileSystemLocal = $fileSystemLocal = $this->getMockBuilder('Sonata\MediaBundle\Filesystem\Local')->disableOriginalConstructor()->getMock(); |
|
98
|
|
|
$this->fileSystemLocal->expects($this->once())->method('getDirectory')->will($this->returnValue($this->workspace)); |
|
99
|
|
|
|
|
100
|
|
|
$this->container->expects($this->any()) |
|
101
|
|
|
->method('get') |
|
102
|
|
|
->will($this->returnCallback(function ($id) use ($pool, $mediaManager, $fileSystemLocal) { |
|
103
|
|
|
switch ($id) { |
|
104
|
|
|
case 'sonata.media.pool': |
|
105
|
|
|
return $pool; |
|
106
|
|
|
case 'sonata.media.manager.media': |
|
107
|
|
|
return $mediaManager; |
|
108
|
|
|
case 'sonata.media.adapter.filesystem.local': |
|
109
|
|
|
return $fileSystemLocal; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return; |
|
113
|
|
|
})); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function testExecuteDirectoryNotExists() |
|
117
|
|
|
{ |
|
118
|
|
|
$context = array( |
|
119
|
|
|
'providers' => array(), |
|
120
|
|
|
'formats' => array(), |
|
121
|
|
|
'download' => array(), |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
$this->pool->expects($this->once())->method('getContexts')->will($this->returnValue(array('foo' => $context))); |
|
125
|
|
|
|
|
126
|
|
|
$output = $this->tester->execute(array('command' => $this->command->getName())); |
|
127
|
|
|
|
|
128
|
|
|
$this->assertRegExp('@\'.+\' does not exist\s+done!@', $this->tester->getDisplay()); |
|
129
|
|
|
|
|
130
|
|
|
$this->assertSame(0, $output); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function testExecuteEmptyDirectory() |
|
134
|
|
|
{ |
|
135
|
|
|
$this->filesystem->mkdir($this->workspace.DIRECTORY_SEPARATOR.'foo'); |
|
136
|
|
|
|
|
137
|
|
|
$context = array( |
|
138
|
|
|
'providers' => array(), |
|
139
|
|
|
'formats' => array(), |
|
140
|
|
|
'download' => array(), |
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
|
|
$this->pool->expects($this->once())->method('getContexts')->will($this->returnValue(array('foo' => $context))); |
|
144
|
|
|
|
|
145
|
|
|
$output = $this->tester->execute(array('command' => $this->command->getName())); |
|
146
|
|
|
|
|
147
|
|
|
$this->assertRegExp('@Context: foo\s+done!@', $this->tester->getDisplay()); |
|
148
|
|
|
|
|
149
|
|
|
$this->assertSame(0, $output); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function testExecuteFilesExists() |
|
153
|
|
|
{ |
|
154
|
|
|
$this->filesystem->mkdir($this->workspace.DIRECTORY_SEPARATOR.'foo'); |
|
155
|
|
|
$this->filesystem->touch($this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'qwertz.ext'); |
|
156
|
|
|
$this->filesystem->touch($this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'thumb_1_bar.ext'); |
|
157
|
|
|
|
|
158
|
|
|
$context = array( |
|
159
|
|
|
'providers' => array(), |
|
160
|
|
|
'formats' => array(), |
|
161
|
|
|
'download' => array(), |
|
162
|
|
|
); |
|
163
|
|
|
|
|
164
|
|
|
$provider = $this->getMockBuilder('Sonata\MediaBundle\Provider\FileProvider')->disableOriginalConstructor()->getMock(); |
|
165
|
|
|
$provider->expects($this->any())->method('getName')->will($this->returnValue('fooprovider')); |
|
166
|
|
|
|
|
167
|
|
|
$this->pool->expects($this->any())->method('getContexts')->will($this->returnValue(array('foo' => $context))); |
|
168
|
|
|
$this->pool->expects($this->any())->method('getProviders')->will($this->returnValue(array($provider))); |
|
169
|
|
|
|
|
170
|
|
|
$media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface'); |
|
171
|
|
|
|
|
172
|
|
|
$this->mediaManager->expects($this->once())->method('findOneBy') |
|
173
|
|
|
->with($this->equalTo(array('id' => 1, 'context' => 'foo'))) |
|
174
|
|
|
->will($this->returnValue(array($media))); |
|
175
|
|
|
$this->mediaManager->expects($this->once())->method('findBy') |
|
176
|
|
|
->with($this->equalTo(array('providerReference' => 'qwertz.ext', 'providers' => array('fooprovider')))) |
|
177
|
|
|
->will($this->returnValue(array($media))); |
|
178
|
|
|
|
|
179
|
|
|
$output = $this->tester->execute(array('command' => $this->command->getName())); |
|
180
|
|
|
|
|
181
|
|
|
$this->assertRegExp('@Context: foo\s+done!@', $this->tester->getDisplay()); |
|
182
|
|
|
|
|
183
|
|
|
$this->assertSame(0, $output); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function testExecuteFilesExistsVerbose() |
|
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 = array( |
|
193
|
|
|
'providers' => array(), |
|
194
|
|
|
'formats' => array(), |
|
195
|
|
|
'download' => array(), |
|
196
|
|
|
); |
|
197
|
|
|
|
|
198
|
|
|
$provider = $this->getMockBuilder('Sonata\MediaBundle\Provider\FileProvider')->disableOriginalConstructor()->getMock(); |
|
199
|
|
|
$provider->expects($this->any())->method('getName')->will($this->returnValue('fooprovider')); |
|
200
|
|
|
|
|
201
|
|
|
$this->pool->expects($this->any())->method('getContexts')->will($this->returnValue(array('foo' => $context))); |
|
202
|
|
|
$this->pool->expects($this->any())->method('getProviders')->will($this->returnValue(array($provider))); |
|
203
|
|
|
|
|
204
|
|
|
$media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface'); |
|
205
|
|
|
|
|
206
|
|
|
$this->mediaManager->expects($this->once())->method('findOneBy') |
|
207
|
|
|
->with($this->equalTo(array('id' => 1, 'context' => 'foo'))) |
|
208
|
|
|
->will($this->returnValue(array($media))); |
|
209
|
|
|
$this->mediaManager->expects($this->once())->method('findBy') |
|
210
|
|
|
->with($this->equalTo(array('providerReference' => 'qwertz.ext', 'providers' => array('fooprovider')))) |
|
211
|
|
|
->will($this->returnValue(array($media))); |
|
212
|
|
|
|
|
213
|
|
|
$output = $this->tester->execute(array('command' => $this->command->getName(), '--verbose' => true)); |
|
214
|
|
|
|
|
215
|
|
|
$this->assertOutputFoundInContext( |
|
216
|
|
|
'/Context: foo\s+(.+)\s+done!/ms', |
|
217
|
|
|
array( |
|
218
|
|
|
'\'qwertz.ext\' found', |
|
219
|
|
|
'\'thumb_1_bar.ext\' found', |
|
220
|
|
|
), |
|
221
|
|
|
$this->tester->getDisplay() |
|
222
|
|
|
); |
|
223
|
|
|
$this->assertSame(0, $output); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function testExecuteDryRun() |
|
227
|
|
|
{ |
|
228
|
|
|
$this->filesystem->mkdir($this->workspace.DIRECTORY_SEPARATOR.'foo'); |
|
229
|
|
|
$this->filesystem->touch($this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'qwertz.ext'); |
|
230
|
|
|
$this->filesystem->touch($this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'thumb_1_bar.ext'); |
|
231
|
|
|
|
|
232
|
|
|
$context = array( |
|
233
|
|
|
'providers' => array(), |
|
234
|
|
|
'formats' => array(), |
|
235
|
|
|
'download' => array(), |
|
236
|
|
|
); |
|
237
|
|
|
|
|
238
|
|
|
$provider = $this->getMockBuilder('Sonata\MediaBundle\Provider\FileProvider')->disableOriginalConstructor()->getMock(); |
|
239
|
|
|
$provider->expects($this->any())->method('getName')->will($this->returnValue('fooprovider')); |
|
240
|
|
|
|
|
241
|
|
|
$this->pool->expects($this->any())->method('getContexts')->will($this->returnValue(array('foo' => $context))); |
|
242
|
|
|
$this->pool->expects($this->any())->method('getProviders')->will($this->returnValue(array($provider))); |
|
243
|
|
|
|
|
244
|
|
|
$this->mediaManager->expects($this->once())->method('findOneBy') |
|
245
|
|
|
->with($this->equalTo(array('id' => 1, 'context' => 'foo'))) |
|
246
|
|
|
->will($this->returnValue(array())); |
|
247
|
|
|
$this->mediaManager->expects($this->once())->method('findBy') |
|
248
|
|
|
->with($this->equalTo(array('providerReference' => 'qwertz.ext', 'providers' => array('fooprovider')))) |
|
249
|
|
|
->will($this->returnValue(array())); |
|
250
|
|
|
|
|
251
|
|
|
$output = $this->tester->execute(array('command' => $this->command->getName(), '--dry-run' => true)); |
|
252
|
|
|
|
|
253
|
|
|
$this->assertOutputFoundInContext( |
|
254
|
|
|
'/Context: foo\s+(.+)\s+done!/ms', |
|
255
|
|
|
array( |
|
256
|
|
|
'\'qwertz.ext\' is orphanend', |
|
257
|
|
|
'\'thumb_1_bar.ext\' is orphanend', |
|
258
|
|
|
), |
|
259
|
|
|
$this->tester->getDisplay() |
|
260
|
|
|
); |
|
261
|
|
|
$this->assertSame(0, $output); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
public function testExecute() |
|
265
|
|
|
{ |
|
266
|
|
|
$this->filesystem->mkdir($this->workspace.DIRECTORY_SEPARATOR.'foo'); |
|
267
|
|
|
$this->filesystem->touch($this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'qwertz.ext'); |
|
268
|
|
|
$this->filesystem->touch($this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'thumb_1_bar.ext'); |
|
269
|
|
|
|
|
270
|
|
|
$context = array( |
|
271
|
|
|
'providers' => array(), |
|
272
|
|
|
'formats' => array(), |
|
273
|
|
|
'download' => array(), |
|
274
|
|
|
); |
|
275
|
|
|
|
|
276
|
|
|
$provider = $this->getMockBuilder('Sonata\MediaBundle\Provider\FileProvider')->disableOriginalConstructor()->getMock(); |
|
277
|
|
|
$provider->expects($this->any())->method('getName')->will($this->returnValue('fooprovider')); |
|
278
|
|
|
|
|
279
|
|
|
$this->pool->expects($this->any())->method('getContexts')->will($this->returnValue(array('foo' => $context))); |
|
280
|
|
|
$this->pool->expects($this->any())->method('getProviders')->will($this->returnValue(array($provider))); |
|
281
|
|
|
|
|
282
|
|
|
$this->mediaManager->expects($this->once())->method('findOneBy') |
|
283
|
|
|
->with($this->equalTo(array('id' => 1, 'context' => 'foo'))) |
|
284
|
|
|
->will($this->returnValue(array())); |
|
285
|
|
|
$this->mediaManager->expects($this->once())->method('findBy') |
|
286
|
|
|
->with($this->equalTo(array('providerReference' => 'qwertz.ext', 'providers' => array('fooprovider')))) |
|
287
|
|
|
->will($this->returnValue(array())); |
|
288
|
|
|
|
|
289
|
|
|
$output = $this->tester->execute(array('command' => $this->command->getName())); |
|
290
|
|
|
|
|
291
|
|
|
$this->assertOutputFoundInContext( |
|
292
|
|
|
'/Context: foo\s+(.+)\s+done!/ms', |
|
293
|
|
|
array( |
|
294
|
|
|
'\'qwertz.ext\' was successfully removed', |
|
295
|
|
|
'\'thumb_1_bar.ext\' was successfully removed', |
|
296
|
|
|
), |
|
297
|
|
|
$this->tester->getDisplay() |
|
298
|
|
|
); |
|
299
|
|
|
$this->assertSame(0, $output); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Asserts whether all expected texts can be found in the output within a given context. |
|
304
|
|
|
* |
|
305
|
|
|
* @param string $extract PCRE regex expected to have a single matching group, extracting the content of a context |
|
|
|
|
|
|
306
|
|
|
* @param array $expected Excerpts of text expected to be found in the output |
|
307
|
|
|
* @param string $output Searched output |
|
308
|
|
|
*/ |
|
309
|
|
|
private function assertOutputFoundInContext($extractor, $expected, $output) |
|
310
|
|
|
{ |
|
311
|
|
|
preg_match_all($extractor, $output, $matches); |
|
312
|
|
|
|
|
313
|
|
|
$found = false; |
|
314
|
|
|
foreach ($matches[1] as $match) { |
|
315
|
|
|
if ($this->containsAll($match, $expected)) { |
|
316
|
|
|
$found = true; |
|
317
|
|
|
break; |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
$this->assertTrue($found, sprintf( |
|
322
|
|
|
'Unable to find "%s" in "%s" with extractor "%s"', |
|
323
|
|
|
implode('", "', $expected), |
|
324
|
|
|
$output, |
|
325
|
|
|
$extractor |
|
326
|
|
|
)); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* Returns whether every needle can be found as a substring of the haystack. |
|
331
|
|
|
* |
|
332
|
|
|
* @param string $haystack |
|
333
|
|
|
* @param array $needles Array of (potential) substrings of the haystack |
|
334
|
|
|
*/ |
|
335
|
|
|
private function containsAll($haystack, $needles) |
|
336
|
|
|
{ |
|
337
|
|
|
foreach ($needles as $needle) { |
|
338
|
|
|
if (false === strpos($haystack, $needle)) { |
|
339
|
|
|
return false; |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
return true; |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|
This check looks for classes that have been defined more than once in the same file.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.