Completed
Push — master ( 0136c1...94aca9 )
by Lukas Kahwe
03:29
created

RemoveCacheTest::testShouldRemoveCacheBySinglePath()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
c 0
b 0
f 0
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
3
namespace Liip\ImagineBundle\Tests\Functional\Command;
4
5
use Liip\ImagineBundle\Tests\Functional\WebTestCase;
6
use Liip\ImagineBundle\Command\RemoveCacheCommand;
7
use Symfony\Component\Filesystem\Filesystem;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
10
use Symfony\Component\Console\Application;
11
use Symfony\Component\Console\Tester\CommandTester;
12
13
/**
14
 * @covers Liip\ImagineBundle\Command\RemoveCacheCommand
15
 */
16
class RemoveCacheTest extends WebTestCase
17
{
18
    protected $client;
19
20
    protected $webRoot;
21
22
    protected $filesystem;
23
24
    protected $cacheRoot;
25
26
    public function setUp()
27
    {
28
        parent::setUp();
29
30
        $this->client = $this->createClient();
31
32
        $this->webRoot = self::$kernel->getContainer()->getParameter('kernel.root_dir').'/web';
33
        $this->cacheRoot = $this->webRoot.'/media/cache';
34
35
        $this->filesystem = new Filesystem();
36
        $this->filesystem->remove($this->cacheRoot);
37
    }
38
39
    public function testExecuteSuccessfullyWithEmptyCacheAndWithoutParameters()
40
    {
41
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
42
43
        $this->executeConsole(new RemoveCacheCommand());
44
    }
45
46
    public function testExecuteSuccessfullyWithEmptyCacheAndOnePathAndOneFilter()
47
    {
48
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
49
50
        $this->executeConsole(
51
            new RemoveCacheCommand(),
52
            array(
53
                'paths' => array('images/cats.jpeg'),
54
                '--filters' => array('thumbnail_web_path'),
55
        ));
56
    }
57
58
    public function testExecuteSuccessfullyWithEmptyCacheAndMultiplePaths()
59
    {
60
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
61
62
        $this->executeConsole(
63
            new RemoveCacheCommand(),
64
            array('paths' => array('images/cats.jpeg', 'images/cats2.jpeg'))
65
        );
66
    }
67
68
    public function testExecuteSuccessfullyWithEmptyCacheAndMultipleFilters()
69
    {
70
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
71
72
        $this->executeConsole(
73
            new RemoveCacheCommand(),
74
            array('--filters' => array('thumbnail_web_path', 'thumbnail_default'))
75
        );
76
    }
77
78
    public function testShouldRemoveAllCacheIfParametersDoesNotPassed()
79
    {
80
        $this->filesystem->dumpFile(
81
            $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg',
82
            'anImageContent'
83
        );
84
        $this->filesystem->dumpFile(
85
            $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg',
86
            'anImageContent2'
87
        );
88
        $this->filesystem->dumpFile(
89
            $this->cacheRoot.'/thumbnail_default/images/cats.jpeg',
90
            'anImageContent'
91
        );
92
93
        $this->executeConsole(new RemoveCacheCommand());
94
95
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
96
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg');
97
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg');
98
    }
99
100
    public function testShouldRemoveCacheBySinglePath()
101
    {
102
        $this->filesystem->dumpFile(
103
            $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg',
104
            'anImageContent'
105
        );
106
        $this->filesystem->dumpFile(
107
            $this->cacheRoot.'/thumbnail_default/images/cats.jpeg',
108
            'anImageContent'
109
        );
110
        $this->filesystem->dumpFile(
111
            $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg',
112
            'anImageContent2'
113
        );
114
        $this->filesystem->dumpFile(
115
            $this->cacheRoot.'/thumbnail_default/images/cats2.jpeg',
116
            'anImageContent2'
117
        );
118
119
        $this->executeConsole(
120
            new RemoveCacheCommand(),
121
            array('paths' => array('images/cats.jpeg'))
122
        );
123
124
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
125
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg');
126
        $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg');
127
        $this->assertFileExists($this->cacheRoot.'/thumbnail_default/images/cats2.jpeg');
128
    }
129
130
    public function testShouldRemoveCacheByMultiplePaths()
131
    {
132
        $this->filesystem->dumpFile(
133
            $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg',
134
            'anImageContent'
135
        );
136
        $this->filesystem->dumpFile(
137
            $this->cacheRoot.'/thumbnail_default/images/cats.jpeg',
138
            'anImageContent'
139
        );
140
        $this->filesystem->dumpFile(
141
            $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg',
142
            'anImageContent2'
143
        );
144
        $this->filesystem->dumpFile(
145
            $this->cacheRoot.'/thumbnail_default/images/cats2.jpeg',
146
            'anImageContent2'
147
        );
148
149
        $this->executeConsole(
150
            new RemoveCacheCommand(),
151
            array('paths' => array('images/cats.jpeg', 'images/cats2.jpeg'))
152
        );
153
154
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
155
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg');
156
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg');
157
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats2.jpeg');
158
    }
159
160
    public function testShouldRemoveCacheBySingleFilter()
161
    {
162
        $this->filesystem->dumpFile(
163
            $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg',
164
            'anImageContent'
165
        );
166
        $this->filesystem->dumpFile(
167
            $this->cacheRoot.'/thumbnail_default/images/cats.jpeg',
168
            'anImageContent'
169
        );
170
        $this->filesystem->dumpFile(
171
            $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg',
172
            'anImageContent2'
173
        );
174
        $this->filesystem->dumpFile(
175
            $this->cacheRoot.'/thumbnail_default/images/cats2.jpeg',
176
            'anImageContent2'
177
        );
178
179
        $this->executeConsole(
180
            new RemoveCacheCommand(),
181
            array('--filters' => array('thumbnail_default'))
182
        );
183
184
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg');
185
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats2.jpeg');
186
        $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
187
        $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg');
188
    }
189
190
    public function testShouldRemoveCacheByMultipleFilters()
191
    {
192
        $this->filesystem->dumpFile(
193
            $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg',
194
            'anImageContent'
195
        );
196
        $this->filesystem->dumpFile(
197
            $this->cacheRoot.'/thumbnail_default/images/cats.jpeg',
198
            'anImageContent'
199
        );
200
        $this->filesystem->dumpFile(
201
            $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg',
202
            'anImageContent2'
203
        );
204
        $this->filesystem->dumpFile(
205
            $this->cacheRoot.'/thumbnail_default/images/cats2.jpeg',
206
            'anImageContent2'
207
        );
208
209
        $this->executeConsole(
210
            new RemoveCacheCommand(),
211
            array('--filters' => array('thumbnail_default', 'thumbnail_web_path'))
212
        );
213
214
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
215
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg');
216
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg');
217
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats2.jpeg');
218
    }
219
220
    public function testShouldRemoveCacheByOnePathAndMultipleFilters()
221
    {
222
        $this->filesystem->dumpFile(
223
            $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg',
224
            'anImageContent'
225
        );
226
        $this->filesystem->dumpFile(
227
            $this->cacheRoot.'/thumbnail_default/images/cats.jpeg',
228
            'anImageContent'
229
        );
230
        $this->filesystem->dumpFile(
231
            $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg',
232
            'anImageContent2'
233
        );
234
235
        $this->executeConsole(
236
            new RemoveCacheCommand(),
237
            array(
238
                'paths' => array('images/cats.jpeg'),
239
                '--filters' => array('thumbnail_default', 'thumbnail_web_path'), )
240
        );
241
242
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
243
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg');
244
        $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg');
245
    }
246
247
    public function testShouldRemoveCacheByMultiplePathsAndSingleFilter()
248
    {
249
        $this->filesystem->dumpFile(
250
            $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg',
251
            'anImageContent'
252
        );
253
        $this->filesystem->dumpFile(
254
            $this->cacheRoot.'/thumbnail_default/images/cats.jpeg',
255
            'anImageContent'
256
        );
257
        $this->filesystem->dumpFile(
258
            $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg',
259
            'anImageContent2'
260
        );
261
262
        $this->executeConsole(
263
            new RemoveCacheCommand(),
264
            array(
265
                'paths' => array('images/cats.jpeg', 'images/cats2.jpeg'),
266
                '--filters' => array('thumbnail_web_path'), )
267
        );
268
269
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
270
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg');
271
        $this->assertFileExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg');
272
    }
273
274
    /**
275
     * Helper function return the result of command execution.
276
     *
277
     * @param Command $command
278
     * @param array   $arguments
279
     * @param array   $options
280
     *
281
     * @return string
282
     */
283
    protected function executeConsole(Command $command, array $arguments = array(), array $options = array())
284
    {
285
        $command->setApplication(new Application($this->createClient()->getKernel()));
286
        if ($command instanceof ContainerAwareCommand) {
287
            $command->setContainer($this->createClient()->getContainer());
288
        }
289
290
        $arguments = array_replace(array('command' => $command->getName()), $arguments);
291
        $options = array_replace(array('--env' => 'test'), $options);
292
293
        $commandTester = new CommandTester($command);
294
        $commandTester->execute($arguments, $options);
295
296
        return $commandTester->getDisplay();
297
    }
298
}
299