MediaExtensionTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 110
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getMediaManager() 0 4 1
A testThumbnailHasAllNecessaryAttributes() 0 34 1
A getMediaService() 0 7 1
A getProvider() 0 10 2
A getTemplate() 0 8 2
A getEnvironment() 0 9 2
A getMedia() 0 9 2
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
use PHPUnit\Framework\TestCase;
15
use Sonata\Doctrine\Model\ManagerInterface;
16
use Sonata\MediaBundle\Model\Media;
17
use Sonata\MediaBundle\Model\MediaInterface;
18
use Sonata\MediaBundle\Provider\MediaProviderInterface;
19
use Sonata\MediaBundle\Provider\Pool;
20
use Sonata\MediaBundle\Twig\Extension\MediaExtension;
21
use Twig\Environment;
22
use Twig\Template;
23
24
/**
25
 * @author Geza Buza <[email protected]>
26
 */
27
class MediaExtensionTest extends TestCase
28
{
29
    /**
30
     * @var Sonata\MediaBundle\Provider\MediaProviderInterface
31
     */
32
    private $provider;
33
34
    /**
35
     * @var Template
36
     */
37
    private $template;
38
39
    /**
40
     * @var Environment
41
     */
42
    private $environment;
43
44
    /**
45
     * @var Sonata\MediaBundle\Model\Media
46
     */
47
    private $media;
48
49
    public function testThumbnailHasAllNecessaryAttributes(): void
50
    {
51
        $mediaExtension = new MediaExtension($this->getMediaService(), $this->getMediaManager());
52
        $mediaExtension->initRuntime($this->getEnvironment());
53
54
        $media = $this->getMedia();
55
        $format = 'png';
56
        $options = [
57
            'title' => 'Test title',
58
            'alt' => 'Test title',
59
        ];
60
61
        $provider = $this->getProvider();
62
        $provider->expects($this->once())->method('generatePublicUrl')->with($media, $format)
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\MediaBundl...MediaProviderInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
            ->willReturn('http://some.url.com');
64
65
        $template = $this->getTemplate();
66
        $template->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Twig\Template>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
            ->method('render')
68
            ->with(
69
                $this->equalTo(
70
                    [
71
                        'media' => $media,
72
                        'options' => [
73
                            'title' => 'Test title',
74
                            'alt' => 'Test title',
75
                            'src' => 'http://some.url.com',
76
                        ],
77
                    ]
78
                )
79
            );
80
81
        $mediaExtension->thumbnail($media, $format, $options);
82
    }
83
84
    public function getMediaService(): Pool
85
    {
86
        $mediaService = $this->createMock(Pool::class);
87
        $mediaService->method('getProvider')->willReturn($this->getProvider());
88
89
        return $mediaService;
90
    }
91
92
    public function getMediaManager(): ManagerInterface
93
    {
94
        return $this->createMock(ManagerInterface::class);
95
    }
96
97
    public function getProvider(): MediaProviderInterface
98
    {
99
        if (null === $this->provider) {
100
            $this->provider = $this->createMock(MediaProviderInterface::class);
101
            $this->provider->method('getFormatName')->willReturnArgument(1);
102
            $this->provider->method('getFormat')->willReturn(false);
103
        }
104
105
        return $this->provider;
106
    }
107
108
    public function getTemplate(): Template
109
    {
110
        if (null === $this->template) {
111
            $this->template = $this->createMock(Template::class);
112
        }
113
114
        return $this->template;
115
    }
116
117
    public function getEnvironment(): Environment
118
    {
119
        if (null === $this->environment) {
120
            $this->environment = $this->createMock(Environment::class);
121
            $this->environment->method('loadTemplate')->willReturn($this->getTemplate());
122
        }
123
124
        return $this->environment;
125
    }
126
127
    public function getMedia(): Media
128
    {
129
        if (null === $this->media) {
130
            $this->media = $this->createMock(Media::class);
131
            $this->media->method('getProviderStatus')->willReturn(MediaInterface::STATUS_OK);
132
        }
133
134
        return $this->media;
135
    }
136
}
137