LiipImagineThumbnailTest::testGenerate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 9.069
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\Security;
15
16
use Gaufrette\Adapter\InMemory;
17
use Gaufrette\File;
18
use Gaufrette\Filesystem;
19
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
20
use PHPUnit\Framework\TestCase;
21
use Sonata\MediaBundle\Model\MediaInterface;
22
use Sonata\MediaBundle\Provider\MediaProviderInterface;
23
use Sonata\MediaBundle\Resizer\ResizerInterface;
24
use Sonata\MediaBundle\Tests\Entity\Media;
25
use Sonata\MediaBundle\Thumbnail\LiipImagineThumbnail;
26
use Symfony\Component\Routing\RouterInterface;
27
28
class LiipImagineThumbnailTest extends TestCase
29
{
30
    public function testGenerate(): void
31
    {
32
        $cacheManager = $this->prophesize(CacheManager::class);
33
        $cacheManager->getBrowserPath()->willReturn('cache/media/default/0011/24/ASDASDAS.png');
34
35
        $thumbnail = new LiipImagineThumbnail($cacheManager);
36
37
        $filesystem = new Filesystem(new InMemory(['myfile' => 'content']));
38
        $referenceFile = new File('myfile', $filesystem);
39
40
        $formats = [
41
          'admin' => ['height' => 50, 'width' => 50, 'quality' => 100],
42
          'mycontext_medium' => ['height' => 500, 'width' => 500, 'quality' => 100],
43
          'anothercontext_large' => ['height' => 500, 'width' => 500, 'quality' => 100],
44
        ];
45
46
        $resizer = $this->prophesize(ResizerInterface::class);
47
        $resizer->resize()->willReturn(true);
48
49
        $media = new Media();
50
        $media->setName('ASDASDAS.png');
51
        $media->setProviderReference('ASDASDAS.png');
52
        $media->setId(1023456);
53
        $media->setContext('default');
54
55
        $provider = $this->prophesize(MediaProviderInterface::class);
56
        $provider->requireThumbnails()->willReturn(true);
57
        $provider->getReferenceFile()->willReturn($referenceFile);
58
        $provider->getFormats()->willReturn($formats);
59
        $provider->getResizer()->willReturn($resizer);
60
        $provider->generatePrivateUrl()->willReturn('/my/private/path');
61
        $provider->generatePublicUrl()->willReturn('/my/public/path');
62
        $provider->getFilesystem()->willReturn($filesystem);
63
        $provider->getReferenceImage($media)->willReturn('default/0011/24/ASDASDAS.png');
64
        $provider->getCdnPath(
65
            'default/0011/24/ASDASDAS.png',
66
            null
67
        )->willReturn('cache/media/default/0011/24/ASDASDAS.png');
68
69
        $thumbnail->generate($provider->reveal(), $media);
70
        $this->assertSame('default/0011/24/ASDASDAS.png', $thumbnail->generatePublicUrl(
71
            $provider->reveal(),
72
            $media,
73
            MediaProviderInterface::FORMAT_ADMIN
74
        ));
75
        $this->assertSame('cache/media/default/0011/24/ASDASDAS.png', $thumbnail->generatePublicUrl(
76
            $provider->reveal(),
77
            $media,
78
            'mycontext_medium'
79
        ));
80
    }
81
82
    /**
83
     * @group legacy
84
     * @expectedDeprecation Using an instance of Symfony\Component\Routing\RouterInterface is deprecated since version 3.3 and will be removed in 4.0. Use Liip\ImagineBundle\Imagine\Cache\CacheManager.
85
     */
86
    public function testLegacyGenerate(): void
87
    {
88
        $router = $this->prophesize(RouterInterface::class);
89
        $router->generate(
90
            '_imagine_medium',
91
            ['path' => '/some/path/42_medium.jpg']
92
        )->willReturn('/imagine/medium/some/path/42_medium.jpg');
93
        $thumbnail = new LiipImagineThumbnail($router->reveal());
94
        $provider = $this->prophesize(MediaProviderInterface::class);
95
        $media = $this->prophesize(MediaInterface::class);
96
        $media->getId()->willReturn(42);
97
        $media->getCdnIsFlushable()->willReturn(true);
98
        $format = 'medium';
99
        $provider->getReferenceImage($media->reveal())->willReturn('/some/image.jpg');
100
        $provider->generatePath($media->reveal())->willReturn('/some/path');
101
        $provider->getCdnPath(
102
            '/imagine/medium/some/path/42_medium.jpg',
103
            true
104
        )->willReturn('some/cdn/path');
105
        $this->assertSame(
106
            'some/cdn/path',
107
            $thumbnail->generatePublicUrl($provider->reveal(), $media->reveal(), $format)
108
        );
109
    }
110
}
111