Completed
Push — master ( 14bd4d...eb12ea )
by Thomas
22:31
created

MediaExtensionTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 106
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B testThumbnailCanRenderHtmlAttributesGivenByTheProvider() 0 27 1
A getMediaService() 0 9 1
A getMediaManager() 0 4 1
A getProvider() 0 9 2
A getTemplate() 0 8 2
A getEnvironment() 0 11 2
A getMedia() 0 9 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
use Sonata\MediaBundle\Model\MediaInterface;
13
use Sonata\MediaBundle\Twig\Extension\MediaExtension;
14
15
/**
16
 * Class MediaExtensionTest.
17
 *
18
 * Unit test of MediaExtension class.
19
 *
20
 * @author Geza Buza <[email protected]>
21
 */
22
class MediaExtensionTest extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
23
{
24
    /**
25
     * @var Sonata\MediaBundle\Provider\MediaProviderInterface
26
     */
27
    private $provider;
28
29
    /**
30
     * @var Twig_TemplateInterface
31
     */
32
    private $template;
33
34
    /**
35
     * @var Twig_Environment
36
     */
37
    private $environment;
38
39
    /**
40
     * @var Sonata\MediaBundle\Model\Media
41
     */
42
    private $media;
43
44
    public function testThumbnailCanRenderHtmlAttributesGivenByTheProvider()
45
    {
46
        $mediaExtension = new MediaExtension($this->getMediaService(), $this->getMediaManager());
47
        $mediaExtension->initRuntime($this->getEnvironment());
48
49
        $media = $this->getMedia();
50
        $format = 'png';
51
        $options = array('title' => 'Test title');
52
53
        $provider = $this->getProvider();
54
        $provider->expects($this->once())->method('getHelperProperties')->with($media, $format, $options)
55
            ->willReturn(array('title' => 'Test title', 'data-custom' => 'foo'));
56
57
        $template = $this->getTemplate();
58
        $template->expects($this->once())
59
            ->method('render')
60
            ->with(
61
                $this->equalTo(
62
                    array(
63
                        'media'   => $media,
64
                        'options' => array('title' => 'Test title', 'data-custom' => 'foo'),
65
                    )
66
                )
67
            );
68
69
        $mediaExtension->thumbnail($media, $format, $options);
70
    }
71
72
    public function getMediaService()
73
    {
74
        $mediaService = $this->getMockBuilder('Sonata\MediaBundle\Provider\Pool')
75
            ->disableOriginalConstructor()
76
            ->getMock();
77
        $mediaService->method('getProvider')->willReturn($this->getProvider());
78
79
        return $mediaService;
80
    }
81
82
    public function getMediaManager()
83
    {
84
        return $this->getMock('Sonata\CoreBundle\Model\ManagerInterface');
85
    }
86
87
    public function getProvider()
88
    {
89
        if (is_null($this->provider)) {
90
            $this->provider = $this->getMock('Sonata\MediaBundle\Provider\MediaProviderInterface');
91
            $this->provider->method('getFormatName')->will($this->returnArgument(1));
92
        }
93
94
        return $this->provider;
95
    }
96
97
    public function getTemplate()
98
    {
99
        if (is_null($this->template)) {
100
            $this->template = $this->getMock('Twig_TemplateInterface');
101
        }
102
103
        return $this->template;
104
    }
105
106
    public function getEnvironment()
107
    {
108
        if (is_null($this->environment)) {
109
            $this->environment = $this->getMockBuilder('Twig_Environment')
110
                ->disableOriginalConstructor()
111
                ->getMock();
112
            $this->environment->method('loadTemplate')->willReturn($this->getTemplate());
113
        }
114
115
        return $this->environment;
116
    }
117
118
    public function getMedia()
119
    {
120
        if (is_null($this->media)) {
121
            $this->media = $this->getMock('Sonata\MediaBundle\Model\Media');
122
            $this->media->method('getProviderStatus')->willReturn(MediaInterface::STATUS_OK);
123
        }
124
125
        return $this->media;
126
    }
127
}
128