Completed
Pull Request — master (#1559)
by Grégoire
02:43
created

ImageProviderTest::testHelperProperties()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 87
rs 8.2836
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Tests\Provider;
15
16
use Gaufrette\Adapter;
17
use Gaufrette\File;
18
use Gaufrette\Filesystem;
19
use Imagine\Image\Box;
20
use Imagine\Image\BoxInterface;
21
use Imagine\Image\ImageInterface;
22
use Imagine\Image\ImagineInterface;
23
use Sonata\MediaBundle\CDN\Server;
24
use Sonata\MediaBundle\Generator\DefaultGenerator;
25
use Sonata\MediaBundle\Metadata\MetadataBuilderInterface;
26
use Sonata\MediaBundle\Provider\ImageProvider;
27
use Sonata\MediaBundle\Resizer\ResizerInterface;
28
use Sonata\MediaBundle\Tests\Entity\Media;
29
use Sonata\MediaBundle\Thumbnail\FormatThumbnail;
30
31
class ImageProviderTest extends AbstractProviderTest
32
{
33
    public function getProvider($allowedExtensions = [], $allowedMimeTypes = [], $box = false)
34
    {
35
        $resizer = $this->createMock(ResizerInterface::class);
36
        $resizer->expects($this->any())->method('resize')->will($this->returnValue(true));
37
        if ($box) {
38
            $resizer->expects($this->any())->method('getBox')->will($box);
39
        }
40
41
        $adapter = $this->createMock(Adapter::class);
42
43
        $filesystem = $this->getMockBuilder(Filesystem::class)
44
            ->setMethods(['get'])
45
            ->setConstructorArgs([$adapter])
46
            ->getMock();
47
        $file = $this->getMockBuilder(File::class)
48
            ->setConstructorArgs(['foo', $filesystem])
49
            ->getMock();
50
        $filesystem->expects($this->any())->method('get')->will($this->returnValue($file));
51
52
        $cdn = new Server('/uploads/media');
53
54
        $generator = new DefaultGenerator();
55
56
        $thumbnail = new FormatThumbnail('jpg');
57
58
        $size = $this->createMock(BoxInterface::class);
59
        $size->expects($this->any())->method('getWidth')->will($this->returnValue(100));
60
        $size->expects($this->any())->method('getHeight')->will($this->returnValue(100));
61
62
        $image = $this->createMock(ImageInterface::class);
63
        $image->expects($this->any())->method('getSize')->will($this->returnValue($size));
64
65
        $adapter = $this->createMock(ImagineInterface::class);
66
        $adapter->expects($this->any())->method('open')->will($this->returnValue($image));
67
68
        $metadata = $this->createMock(MetadataBuilderInterface::class);
69
70
        $provider = new ImageProvider('image', $filesystem, $cdn, $generator, $thumbnail, $allowedExtensions, $allowedMimeTypes, $adapter, $metadata);
71
        $provider->setResizer($resizer);
72
73
        return $provider;
74
    }
75
76
    public function testProvider(): void
77
    {
78
        $provider = $this->getProvider();
79
80
        $media = new Media();
81
        $media->setName('test.png');
82
        $media->setProviderReference('ASDASDAS.png');
83
        $media->setId(1023456);
84
        $media->setContext('default');
85
86
        $this->assertSame('default/0011/24/ASDASDAS.png', $provider->getReferenceImage($media));
87
88
        $this->assertSame('default/0011/24', $provider->generatePath($media));
89
        $this->assertSame('/uploads/media/default/0011/24/thumb_1023456_big.png', $provider->generatePublicUrl($media, 'big'));
90
        $this->assertSame('/uploads/media/default/0011/24/ASDASDAS.png', $provider->generatePublicUrl($media, 'reference'));
91
92
        $this->assertSame('default/0011/24/ASDASDAS.png', $provider->generatePrivateUrl($media, 'reference'));
93
        $this->assertSame('default/0011/24/thumb_1023456_big.png', $provider->generatePrivateUrl($media, 'big'));
94
    }
95
96
    public function testHelperProperties(): void
97
    {
98
        $adminBox = new Box(100, 100);
99
        $mediumBox = new Box(500, 250);
100
        $largeBox = new Box(1000, 500);
101
102
        $provider = $this->getProvider(
103
            [],
104
            [],
105
            $this->onConsecutiveCalls(
106
                $largeBox, // first properties call
107
                $mediumBox,
108
                $largeBox,
109
                $mediumBox, // second call
110
                $mediumBox,
111
                $largeBox,
112
                $adminBox, // Third call
113
                $largeBox, // Fourth call
114
                $mediumBox,
115
                $largeBox,
116
                $largeBox, // Fifth call
117
                $mediumBox,
118
                $largeBox
119
            ));
120
121
        $provider->addFormat('admin', ['width' => 100]);
122
        $provider->addFormat('default_medium', ['width' => 500]);
123
        $provider->addFormat('default_large', ['width' => 1000]);
124
125
        $media = new Media();
126
        $media->setName('test.png');
127
        $media->setProviderReference('ASDASDAS.png');
128
        $media->setId(10);
129
        $media->setHeight(500);
130
        $media->setWidth(1500);
131
        $media->setContext('default');
132
133
        $srcSet = '/uploads/media/default/0001/01/thumb_10_default_medium.png 500w, /uploads/media/default/0001/01/thumb_10_default_large.png 1000w, /uploads/media/default/0001/01/ASDASDAS.png 1500w';
134
135
        $properties = $provider->getHelperProperties($media, 'default_large');
136
137
        $this->assertInternalType('array', $properties);
138
        $this->assertSame('test.png', $properties['title']);
139
        $this->assertSame(1000, $properties['width']);
140
        $this->assertSame($srcSet, $properties['srcset']);
141
        $this->assertSame(
142
            '/uploads/media/default/0001/01/thumb_10_default_large.png',
143
            $properties['src']
144
        );
145
        $this->assertSame('(max-width: 1000px) 100vw, 1000px', $properties['sizes']);
146
147
        $properties = $provider->getHelperProperties($media, 'default_large', ['srcset' => ['default_medium']]);
148
        $this->assertSame($srcSet, $properties['srcset']);
149
        $this->assertSame(
150
            '/uploads/media/default/0001/01/thumb_10_default_large.png',
151
            $properties['src']
152
        );
153
        $this->assertSame('(max-width: 500px) 100vw, 500px', $properties['sizes']);
154
155
        $properties = $provider->getHelperProperties($media, 'admin', [
156
            'width' => 150,
157
        ]);
158
        $this->assertArrayNotHasKey('sizes', $properties);
159
        $this->assertArrayNotHasKey('srcset', $properties);
160
161
        $this->assertSame(150, $properties['width']);
162
163
        $properties = $provider->getHelperProperties($media, 'default_large', ['picture' => ['default_medium', 'default_large'], 'class' => 'some-class']);
164
        $this->assertArrayHasKey('picture', $properties);
165
        $this->assertArrayNotHasKey('srcset', $properties);
166
        $this->assertArrayNotHasKey('sizes', $properties);
167
        $this->assertArrayHasKey('source', $properties['picture']);
168
        $this->assertArrayHasKey('img', $properties['picture']);
169
        $this->assertArrayHasKey('class', $properties['picture']['img']);
170
        $this->assertArrayHasKey('media', $properties['picture']['source'][0]);
171
        $this->assertSame('(max-width: 500px)', $properties['picture']['source'][0]['media']);
172
173
        $properties = $provider->getHelperProperties($media, 'default_large', ['picture' => ['(max-width: 200px)' => 'default_medium', 'default_large'], 'class' => 'some-class']);
174
        $this->assertArrayHasKey('picture', $properties);
175
        $this->assertArrayNotHasKey('srcset', $properties);
176
        $this->assertArrayNotHasKey('sizes', $properties);
177
        $this->assertArrayHasKey('source', $properties['picture']);
178
        $this->assertArrayHasKey('img', $properties['picture']);
179
        $this->assertArrayHasKey('class', $properties['picture']['img']);
180
        $this->assertArrayHasKey('media', $properties['picture']['source'][0]);
181
        $this->assertSame('(max-width: 200px)', $properties['picture']['source'][0]['media']);
182
    }
183
184
    public function testThumbnail(): void
185
    {
186
        $provider = $this->getProvider();
187
188
        $media = new Media();
189
        $media->setName('test.png');
190
        $media->setProviderReference('ASDASDAS.png');
191
        $media->setId(1023456);
192
        $media->setContext('default');
193
194
        $this->assertTrue($provider->requireThumbnails($media));
0 ignored issues
show
Unused Code introduced by
The call to ImageProvider::requireThumbnails() has too many arguments starting with $media.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
195
196
        $provider->addFormat('big', ['width' => 200, 'height' => 100, 'constraint' => true]);
197
198
        $this->assertNotEmpty($provider->getFormats(), '::getFormats() return an array');
199
200
        $provider->generateThumbnails($media);
201
202
        $this->assertSame('default/0011/24/thumb_1023456_big.png', $provider->generatePrivateUrl($media, 'big'));
203
    }
204
205
    public function testEvent(): void
206
    {
207
        $provider = $this->getProvider();
208
209
        $provider->addFormat('big', ['width' => 200, 'height' => 100, 'constraint' => true]);
210
211
        $file = new \Symfony\Component\HttpFoundation\File\File(realpath(__DIR__.'/../fixtures/logo.png'));
212
213
        $media = new Media();
214
        $media->setContext('default');
215
        $media->setBinaryContent($file);
216
        $media->setId(1023456);
217
218
        // pre persist the media
219
        $provider->transform($media);
220
        $provider->prePersist($media);
221
222
        $this->assertSame('logo.png', $media->getName(), '::getName() return the file name');
223
        $this->assertNotNull($media->getProviderReference(), '::getProviderReference() is set');
224
225
        // post persit the media
226
        $provider->postPersist($media);
227
228
        $provider->postRemove($media);
229
    }
230
231
    public function testTransformFormatNotSupported(): void
232
    {
233
        $provider = $this->getProvider();
234
235
        $file = new \Symfony\Component\HttpFoundation\File\File(realpath(__DIR__.'/../fixtures/logo.png'));
236
237
        $media = new Media();
238
        $media->setBinaryContent($file);
239
240
        $this->assertNull($provider->transform($media));
241
        $this->assertNull($media->getWidth(), 'Width staid null');
242
    }
243
244
    public function testMetadata(): void
245
    {
246
        $provider = $this->getProvider();
247
248
        $this->assertSame('image', $provider->getProviderMetadata()->getTitle());
249
        $this->assertSame('image.description', $provider->getProviderMetadata()->getDescription());
250
        $this->assertNotNull($provider->getProviderMetadata()->getImage());
251
        $this->assertSame('fa fa-picture-o', $provider->getProviderMetadata()->getOption('class'));
252
        $this->assertSame('SonataMediaBundle', $provider->getProviderMetadata()->getDomain());
253
    }
254
}
255