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

ImagineControllerTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 201
Duplicated Lines 54.73 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 5
Bugs 1 Features 1
Metric Value
dl 110
loc 201
wmc 11
c 5
b 1
f 1
lcom 1
cbo 6
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\Controller;
4
5
use Liip\ImagineBundle\Tests\Functional\WebTestCase;
6
use Symfony\Bundle\FrameworkBundle\Client;
7
use Symfony\Component\Filesystem\Filesystem;
8
use Liip\ImagineBundle\Imagine\Cache\Signer;
9
10
/**
11
 * @covers Liip\ImagineBundle\Controller\ImagineController
12
 */
13
class ImagineControllerTest extends WebTestCase
14
{
15
    /**
16
     * @var Client
17
     */
18
    protected $client;
19
20
    protected $webRoot;
21
22
    protected $cacheRoot;
23
24
    /**
25
     * @var Filesystem
26
     */
27
    protected $filesystem;
28
29
    public function setUp()
30
    {
31
        parent::setUp();
32
33
        $this->client = $this->createClient();
34
35
        $this->webRoot = self::$kernel->getContainer()->getParameter('kernel.root_dir').'/web';
36
        $this->cacheRoot = $this->webRoot.'/media/cache';
37
38
        $this->filesystem = new Filesystem();
39
        $this->filesystem->remove($this->cacheRoot);
40
    }
41
42
    public function testCouldBeGetFromContainer()
43
    {
44
        $controller = self::$kernel->getContainer()->get('liip_imagine.controller');
45
46
        $this->assertInstanceOf('Liip\ImagineBundle\Controller\ImagineController', $controller);
47
    }
48
49
    public function testShouldResolvePopulatingCacheFirst()
50
    {
51
        //guard
52
        $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
53
54
        $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/images/cats.jpeg');
55
56
        $response = $this->client->getResponse();
57
58
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
59
        $this->assertEquals(301, $response->getStatusCode());
60
        $this->assertEquals('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $response->getTargetUrl());
61
62
        $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
63
    }
64
65
    public function testShouldResolveFromCache()
66
    {
67
        $this->filesystem->dumpFile(
68
            $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg',
69
            'anImageContent'
70
        );
71
72
        $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/images/cats.jpeg');
73
74
        $response = $this->client->getResponse();
75
76
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
77
        $this->assertEquals(301, $response->getStatusCode());
78
        $this->assertEquals('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $response->getTargetUrl());
79
80
        $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg');
81
    }
82
83
    /**
84
     * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
85
     * @expectedExceptionMessage Signed url does not pass the sign check for path "images/cats.jpeg" and filter "thumbnail_web_path" and runtime config {"thumbnail":{"size":["50","50"]}}
86
     */
87
    public function testThrowBadRequestIfSignInvalidWhileUsingCustomFilters()
88
    {
89
        $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/rc/invalidHash/images/cats.jpeg?'.http_build_query(array(
90
            'filters' => array(
91
                'thumbnail' => array('size' => array(50, 50)),
92
            ),
93
            '_hash' => 'invalid',
94
        )));
95
    }
96
97
    /**
98
     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
99
     * @expectedExceptionMessage Filters must be an array. Value was "some-string"
100
     */
101
    public function testShouldThrowNotFoundHttpExceptionIfFiltersNotArray()
102
    {
103
        $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/rc/invalidHash/images/cats.jpeg?'.http_build_query(array(
104
            'filters' => 'some-string',
105
            '_hash' => 'hash',
106
        )));
107
    }
108
109
    /**
110
     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
111
     * @expectedExceptionMessage Source image could not be found
112
     */
113
    public function testShouldThrowNotFoundHttpExceptionIfFileNotExists()
114
    {
115
        $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/images/shrodinger_cats_which_not_exist.jpeg');
116
    }
117
118
    /**
119
     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
120
     */
121
    public function testInvalidFilterShouldThrowNotFoundHttpException()
122
    {
123
        $this->client->request('GET', '/media/cache/resolve/invalid-filter/images/cats.jpeg');
124
    }
125
126
    public function testShouldResolveWithCustomFiltersPopulatingCacheFirst()
127
    {
128
        /** @var Signer $signer */
129
        $signer = self::$kernel->getContainer()->get('liip_imagine.cache.signer');
130
131
        $params = array(
132
            'filters' => array(
133
                'thumbnail' => array('size' => array(50, 50)),
134
            ),
135
        );
136
137
        $path = 'images/cats.jpeg';
138
139
        $hash = $signer->sign($path, $params['filters']);
140
141
        $expectedCachePath = 'thumbnail_web_path/rc/'.$hash.'/'.$path;
142
143
        $url = 'http://localhost/media/cache/resolve/'.$expectedCachePath.'?'.http_build_query($params);
144
145
        //guard
146
        $this->assertFileNotExists($this->cacheRoot.'/'.$expectedCachePath);
147
148
        $this->client->request('GET', $url);
149
150
        $response = $this->client->getResponse();
151
152
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
153
        $this->assertEquals(301, $response->getStatusCode());
154
        $this->assertEquals('http://localhost/media/cache/'.$expectedCachePath, $response->getTargetUrl());
155
156
        $this->assertFileExists($this->cacheRoot.'/'.$expectedCachePath);
157
    }
158
159
    public function testShouldResolveWithCustomFiltersFromCache()
160
    {
161
        /** @var Signer $signer */
162
        $signer = self::$kernel->getContainer()->get('liip_imagine.cache.signer');
163
164
        $params = array(
165
            'filters' => array(
166
                'thumbnail' => array('size' => array(50, 50)),
167
            ),
168
        );
169
170
        $path = 'images/cats.jpeg';
171
172
        $hash = $signer->sign($path, $params['filters']);
173
174
        $expectedCachePath = 'thumbnail_web_path/rc/'.$hash.'/'.$path;
175
176
        $url = 'http://localhost/media/cache/resolve/'.$expectedCachePath.'?'.http_build_query($params);
177
178
        $this->filesystem->dumpFile(
179
            $this->cacheRoot.'/'.$expectedCachePath,
180
            'anImageContent'
181
        );
182
183
        $this->client->request('GET', $url);
184
185
        $response = $this->client->getResponse();
186
187
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
188
        $this->assertEquals(301, $response->getStatusCode());
189
        $this->assertEquals('http://localhost/media/cache'.'/'.$expectedCachePath, $response->getTargetUrl());
190
191
        $this->assertFileExists($this->cacheRoot.'/'.$expectedCachePath);
192
    }
193
194
    public function testShouldResolvePathWithSpecialCharactersAndWhiteSpaces()
195
    {
196
        $this->filesystem->dumpFile(
197
            $this->cacheRoot.'/thumbnail_web_path/images/foo bar.jpeg',
198
            'anImageContent'
199
        );
200
201
        // we are calling url with encoded file name as it will be called by browser
202
        $urlEncodedFileName = 'foo+bar';
203
        $this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/images/'.$urlEncodedFileName.'.jpeg');
204
205
        $response = $this->client->getResponse();
206
207
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
208
        $this->assertEquals(301, $response->getStatusCode());
209
        $this->assertEquals('http://localhost/media/cache/thumbnail_web_path/images/foo bar.jpeg', $response->getTargetUrl());
210
211
        $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/foo bar.jpeg');
212
    }
213
}
214