Completed
Push — master ( ba95f5...ac295e )
by
unknown
03:55
created

CleanMediaCommandTest::testExecuteFilesExists()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 8.8571
cc 1
eloc 22
nc 1
nop 0
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\FilesystemTest as LegacyTestCase;
23
use Symfony\Component\Filesystem\Tests\FilesystemTestCase as BaseTestCase;
24
25
// Polyfill for old symfony 2.3 TestCase class
26
if (class_exists('Symfony\Component\Filesystem\Tests\FilesystemTestCase')) {
27
    class FilesystemTestCase extends BaseTestCase
28
    {
29
    }
30
} else {
31
    class FilesystemTestCase extends LegacyTestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Comprehensibility Best Practice introduced by
The type Sonata\MediaBundle\Tests...mand\FilesystemTestCase has been defined more than once; this definition is ignored, only the first definition in this file (L27-29) is considered.

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.

Loading history...
32
    {
33
    }
34
}
35
36
class CleanMediaCommandTest extends FilesystemTestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
37
{
38
    /**
39
     * @var \PHPUnit_Framework_MockObject_MockObject|ContainerInterface
40
     */
41
    protected $container;
42
43
    /**
44
     * @var Application
45
     */
46
    protected $application;
47
48
    /**
49
     * @var ContainerAwareCommand
50
     */
51
    protected $command;
52
53
    /**
54
     * @var CommandTester
55
     */
56
    protected $tester;
57
58
    /**
59
     * @var \PHPUnit_Framework_MockObject_MockObject|Pool
60
     */
61
    private $pool;
62
63
    /**
64
     * @var \PHPUnit_Framework_MockObject_MockObject|MediaManagerInterface
65
     */
66
    private $mediaManager;
67
68
    /**
69
     * @var \PHPUnit_Framework_MockObject_MockObject|Local
70
     */
71
    private $fileSystemLocal;
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    protected function setUp()
77
    {
78
        parent::setUp();
79
80
        $this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
81
82
        $this->command = new CleanMediaCommand();
83
        $this->command->setContainer($this->container);
84
85
        $this->application = new Application();
86
        $this->application->add($this->command);
87
88
        $this->tester = new CommandTester($this->application->find('sonata:media:clean-uploads'));
89
90
        $this->pool = $pool =  $this->getMockBuilder('Sonata\MediaBundle\Provider\Pool')->disableOriginalConstructor()->getMock();
91
92
        $this->mediaManager = $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
93
94
        $this->fileSystemLocal = $fileSystemLocal = $this->getMockBuilder('Sonata\MediaBundle\Filesystem\Local')->disableOriginalConstructor()->getMock();
95
        $this->fileSystemLocal->expects($this->once())->method('getDirectory')->will($this->returnValue($this->workspace));
96
97
        $this->container->expects($this->any())
98
            ->method('get')
99
            ->will($this->returnCallback(function ($id) use ($pool, $mediaManager, $fileSystemLocal) {
100
                switch ($id) {
101
                    case 'sonata.media.pool':
102
                        return $pool;
103
                    case 'sonata.media.manager.media':
104
                        return $mediaManager;
105
                    case 'sonata.media.adapter.filesystem.local':
106
                        return $fileSystemLocal;
107
                }
108
109
                return;
110
            }));
111
    }
112
113
    public function testExecuteDirectoryNotExists()
114
    {
115
        $context = array(
116
            'providers' => array(),
117
            'formats'   => array(),
118
            'download'  => array(),
119
        );
120
121
        $this->pool->expects($this->once())->method('getContexts')->will($this->returnValue(array('foo' => $context)));
122
123
        $output = $this->tester->execute(array('command' => $this->command->getName()));
124
125
        $this->assertRegExp('@\'.+\' does not exist\s+done!@', $this->tester->getDisplay());
126
127
        $this->assertSame(0, $output);
128
    }
129
130
    public function testExecuteEmptyDirectory()
131
    {
132
        $this->filesystem->mkdir($this->workspace.DIRECTORY_SEPARATOR.'foo');
133
134
        $context = array(
135
            'providers' => array(),
136
            'formats'   => array(),
137
            'download'  => array(),
138
        );
139
140
        $this->pool->expects($this->once())->method('getContexts')->will($this->returnValue(array('foo' => $context)));
141
142
        $output = $this->tester->execute(array('command' => $this->command->getName()));
143
144
        $this->assertRegExp('@Context: foo\s+done!@', $this->tester->getDisplay());
145
146
        $this->assertSame(0, $output);
147
    }
148
149
    public function testExecuteFilesExists()
150
    {
151
        $this->filesystem->mkdir($this->workspace.DIRECTORY_SEPARATOR.'foo');
152
        $this->filesystem->touch($this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'qwertz.ext');
153
        $this->filesystem->touch($this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'thumb_1_bar.ext');
154
155
        $context = array(
156
            'providers' => array(),
157
            'formats'   => array(),
158
            'download'  => array(),
159
        );
160
161
        $provider = $this->getMockBuilder('Sonata\MediaBundle\Provider\FileProvider')->disableOriginalConstructor()->getMock();
162
        $provider->expects($this->any())->method('getName')->will($this->returnValue('fooprovider'));
163
164
        $this->pool->expects($this->any())->method('getContexts')->will($this->returnValue(array('foo' => $context)));
165
        $this->pool->expects($this->any())->method('getProviders')->will($this->returnValue(array($provider)));
166
167
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
168
169
        $this->mediaManager->expects($this->once())->method('findOneBy')
170
            ->with($this->equalTo(array('id' => 1, 'context' => 'foo')))
171
            ->will($this->returnValue(array($media)));
172
        $this->mediaManager->expects($this->once())->method('findBy')
173
            ->with($this->equalTo(array('providerReference' => 'qwertz.ext', 'providers' => array('fooprovider'))))
174
            ->will($this->returnValue(array($media)));
175
176
        $output = $this->tester->execute(array('command' => $this->command->getName()));
177
178
        $this->assertRegExp('@Context: foo\s+done!@', $this->tester->getDisplay());
179
180
        $this->assertSame(0, $output);
181
    }
182
183
    public function testExecuteFilesExistsVerbose()
184
    {
185
        $this->filesystem->mkdir($this->workspace.DIRECTORY_SEPARATOR.'foo');
186
        $this->filesystem->touch($this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'qwertz.ext');
187
        $this->filesystem->touch($this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'thumb_1_bar.ext');
188
189
        $context = array(
190
            'providers' => array(),
191
            'formats'   => array(),
192
            'download'  => array(),
193
        );
194
195
        $provider = $this->getMockBuilder('Sonata\MediaBundle\Provider\FileProvider')->disableOriginalConstructor()->getMock();
196
        $provider->expects($this->any())->method('getName')->will($this->returnValue('fooprovider'));
197
198
        $this->pool->expects($this->any())->method('getContexts')->will($this->returnValue(array('foo' => $context)));
199
        $this->pool->expects($this->any())->method('getProviders')->will($this->returnValue(array($provider)));
200
201
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
202
203
        $this->mediaManager->expects($this->once())->method('findOneBy')
204
            ->with($this->equalTo(array('id' => 1, 'context' => 'foo')))
205
            ->will($this->returnValue(array($media)));
206
        $this->mediaManager->expects($this->once())->method('findBy')
207
            ->with($this->equalTo(array('providerReference' => 'qwertz.ext', 'providers' => array('fooprovider'))))
208
            ->will($this->returnValue(array($media)));
209
210
        $output = $this->tester->execute(array('command' => $this->command->getName(), '--verbose' => true));
211
212
        $this->assertRegExp('@Context: foo\s+\'qwertz.ext\' found\s+\'thumb_1_bar.ext\' found\s+done!@', $this->tester->getDisplay());
213
214
        $this->assertSame(0, $output);
215
    }
216
217
    public function testExecuteDryRun()
218
    {
219
        $this->filesystem->mkdir($this->workspace.DIRECTORY_SEPARATOR.'foo');
220
        $this->filesystem->touch($this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'qwertz.ext');
221
        $this->filesystem->touch($this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'thumb_1_bar.ext');
222
223
        $context = array(
224
            'providers' => array(),
225
            'formats'   => array(),
226
            'download'  => array(),
227
        );
228
229
        $provider = $this->getMockBuilder('Sonata\MediaBundle\Provider\FileProvider')->disableOriginalConstructor()->getMock();
230
        $provider->expects($this->any())->method('getName')->will($this->returnValue('fooprovider'));
231
232
        $this->pool->expects($this->any())->method('getContexts')->will($this->returnValue(array('foo' => $context)));
233
        $this->pool->expects($this->any())->method('getProviders')->will($this->returnValue(array($provider)));
234
235
        $this->mediaManager->expects($this->once())->method('findOneBy')
236
            ->with($this->equalTo(array('id' => 1, 'context' => 'foo')))
237
            ->will($this->returnValue(array()));
238
        $this->mediaManager->expects($this->once())->method('findBy')
239
            ->with($this->equalTo(array('providerReference' => 'qwertz.ext', 'providers' => array('fooprovider'))))
240
            ->will($this->returnValue(array()));
241
242
        $output = $this->tester->execute(array('command' => $this->command->getName(), '--dry-run' => true));
243
244
        $this->assertRegExp('@Context: foo\s+\'qwertz.ext\' is orphanend\s+\'thumb_1_bar.ext\' is orphanend\s+done!@', $this->tester->getDisplay());
245
246
        $this->assertSame(0, $output);
247
    }
248
249
    public function testExecute()
250
    {
251
        $this->filesystem->mkdir($this->workspace.DIRECTORY_SEPARATOR.'foo');
252
        $this->filesystem->touch($this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'qwertz.ext');
253
        $this->filesystem->touch($this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'thumb_1_bar.ext');
254
255
        $context = array(
256
            'providers' => array(),
257
            'formats'   => array(),
258
            'download'  => array(),
259
        );
260
261
        $provider = $this->getMockBuilder('Sonata\MediaBundle\Provider\FileProvider')->disableOriginalConstructor()->getMock();
262
        $provider->expects($this->any())->method('getName')->will($this->returnValue('fooprovider'));
263
264
        $this->pool->expects($this->any())->method('getContexts')->will($this->returnValue(array('foo' => $context)));
265
        $this->pool->expects($this->any())->method('getProviders')->will($this->returnValue(array($provider)));
266
267
        $this->mediaManager->expects($this->once())->method('findOneBy')
268
            ->with($this->equalTo(array('id' => 1, 'context' => 'foo')))
269
            ->will($this->returnValue(array()));
270
        $this->mediaManager->expects($this->once())->method('findBy')
271
            ->with($this->equalTo(array('providerReference' => 'qwertz.ext', 'providers' => array('fooprovider'))))
272
            ->will($this->returnValue(array()));
273
274
        $output = $this->tester->execute(array('command' => $this->command->getName()));
275
276
        $this->assertRegExp('@Context: foo\s+\'qwertz.ext\' was successfully removed\s+\'thumb_1_bar.ext\' was successfully removed\s+done!@', $this->tester->getDisplay());
277
278
        $this->assertSame(0, $output);
279
    }
280
}
281