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

ResolveCacheTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 147
Duplicated Lines 40.82 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 60
loc 147
wmc 9
c 1
b 0
f 0
lcom 1
cbo 10
rs 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Liip\ImagineBundle\Tests\Functional\Command;
4
5
use Liip\ImagineBundle\Tests\Functional\WebTestCase;
6
use Liip\ImagineBundle\Command\ResolveCacheCommand;
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\ResolveCacheCommand
15
 */
16
class ResolveCacheTest 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 testShouldResolveWithEmptyCache()
40
    {
41
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
42
43
        $output = $this->executeConsole(
44
            new ResolveCacheCommand(),
45
            array(
46
                'paths' => array('images/cats.jpeg'),
47
                '--filters' => array('thumbnail_web_path'), )
48
        );
49
50
        $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
51
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg');
52
        $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output);
53
    }
54
55
    public function testShouldResolveWithCacheExists()
56
    {
57
        $this->filesystem->dumpFile(
58
            $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg',
59
            'anImageContent'
60
        );
61
62
        $output = $this->executeConsole(
63
            new ResolveCacheCommand(),
64
            array(
65
                'paths' => array('images/cats.jpeg'),
66
                '--filters' => array('thumbnail_web_path'), )
67
        );
68
69
        $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
70
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg');
71
        $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output);
72
    }
73
74
    public function testShouldResolveWithFewPathsAndSingleFilter()
75
    {
76
        $output = $this->executeConsole(
77
            new ResolveCacheCommand(),
78
            array(
79
                'paths' => array('images/cats.jpeg', 'images/cats2.jpeg'),
80
                '--filters' => array('thumbnail_web_path'), )
81
        );
82
83
        $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output);
84
        $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats2.jpeg', $output);
85
    }
86
87
    public function testShouldResolveWithFewPathsSingleFilterAndPartiallyFullCache()
88
    {
89
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
90
91
        $this->filesystem->dumpFile(
92
            $this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg',
93
            'anImageContent'
94
        );
95
96
        $output = $this->executeConsole(
97
            new ResolveCacheCommand(),
98
            array(
99
                'paths' => array('images/cats.jpeg', 'images/cats2.jpeg'),
100
                '--filters' => array('thumbnail_web_path'), )
101
        );
102
103
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg');
104
        $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
105
        $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats2.jpeg');
106
        $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output);
107
        $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats2.jpeg', $output);
108
    }
109
110
    public function testShouldResolveWithFewPathsAndFewFilters()
111
    {
112
        $output = $this->executeConsole(
113
            new ResolveCacheCommand(),
114
            array(
115
                'paths' => array('images/cats.jpeg', 'images/cats2.jpeg'),
116
                '--filters' => array('thumbnail_web_path', 'thumbnail_default'), )
117
        );
118
119
        $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output);
120
        $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats2.jpeg', $output);
121
        $this->assertContains('http://localhost/media/cache/thumbnail_default/images/cats.jpeg', $output);
122
        $this->assertContains('http://localhost/media/cache/thumbnail_default/images/cats2.jpeg', $output);
123
    }
124
125
    public function testShouldResolveWithFewPathsAndWithoutFilters()
126
    {
127
        $output = $this->executeConsole(
128
            new ResolveCacheCommand(),
129
            array('paths' => array('images/cats.jpeg', 'images/cats2.jpeg'))
130
        );
131
132
        $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output);
133
        $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats2.jpeg', $output);
134
        $this->assertContains('http://localhost/media/cache/thumbnail_default/images/cats.jpeg', $output);
135
        $this->assertContains('http://localhost/media/cache/thumbnail_default/images/cats2.jpeg', $output);
136
    }
137
138
    /**
139
     * Helper function return the result of command execution.
140
     *
141
     * @param Command $command
142
     * @param array   $arguments
143
     * @param array   $options
144
     *
145
     * @return string
146
     */
147
    protected function executeConsole(Command $command, array $arguments = array(), array $options = array())
148
    {
149
        $command->setApplication(new Application($this->createClient()->getKernel()));
150
        if ($command instanceof ContainerAwareCommand) {
151
            $command->setContainer($this->createClient()->getContainer());
152
        }
153
154
        $arguments = array_replace(array('command' => $command->getName()), $arguments);
155
        $options = array_replace(array('--env' => 'test'), $options);
156
157
        $commandTester = new CommandTester($command);
158
        $commandTester->execute($arguments, $options);
159
160
        return $commandTester->getDisplay();
161
    }
162
}
163