Completed
Push — master ( 52b06e...08177a )
by
unknown
04:10 queued 10s
created

YouTubeProviderTest::testMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 Buzz\Browser;
17
use Buzz\Message\AbstractMessage;
18
use Buzz\Message\Response;
19
use Gaufrette\Adapter;
20
use Gaufrette\File;
21
use Gaufrette\Filesystem;
22
use Imagine\Image\Box;
23
use Sonata\AdminBundle\Admin\AdminInterface;
24
use Sonata\AdminBundle\Form\FormMapper;
25
use Sonata\MediaBundle\CDN\Server;
26
use Sonata\MediaBundle\Generator\DefaultGenerator;
27
use Sonata\MediaBundle\Metadata\MetadataBuilderInterface;
28
use Sonata\MediaBundle\Provider\YouTubeProvider;
29
use Sonata\MediaBundle\Resizer\ResizerInterface;
30
use Sonata\MediaBundle\Tests\Entity\Media;
31
use Sonata\MediaBundle\Thumbnail\FormatThumbnail;
32
33
class YouTubeProviderTest extends AbstractProviderTest
34
{
35
    public function getProvider(Browser $browser = null)
36
    {
37
        if (!$browser) {
38
            $browser = $this->createMock(Browser::class);
39
        }
40
41
        $resizer = $this->createMock(ResizerInterface::class);
42
        $resizer->expects($this->any())->method('resize')->will($this->returnValue(true));
43
        $resizer->expects($this->any())->method('getBox')->will($this->returnValue(new Box(100, 100)));
44
45
        $adapter = $this->createMock(Adapter::class);
46
47
        $filesystem = $this->getMockBuilder(Filesystem::class)
48
            ->setMethods(['get'])
49
            ->setConstructorArgs([$adapter])
50
            ->getMock();
51
        $file = $this->getMockBuilder(File::class)
52
            ->setConstructorArgs(['foo', $filesystem])
53
            ->getMock();
54
        $filesystem->expects($this->any())->method('get')->will($this->returnValue($file));
55
56
        $cdn = new Server('/uploads/media');
57
58
        $generator = new DefaultGenerator();
59
60
        $thumbnail = new FormatThumbnail('jpg');
61
62
        $metadata = $this->createMock(MetadataBuilderInterface::class);
63
64
        $provider = new YouTubeProvider('youtube', $filesystem, $cdn, $generator, $thumbnail, $browser, $metadata);
65
        $provider->setResizer($resizer);
66
67
        return $provider;
68
    }
69
70
    public function testProvider(): void
71
    {
72
        $provider = $this->getProvider();
73
74
        $media = new Media();
75
        $media->setName('Nono le petit robot');
76
        $media->setProviderName('youtube');
77
        $media->setProviderReference('BDYAbAtaDzA');
78
        $media->setContext('default');
79
        $media->setProviderMetadata(json_decode('{"provider_url": "http:\/\/www.youtube.com\/", "title": "Nono le petit robot", "html": "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/BDYAbAtaDzA?fs=1\"><\/param><param name=\"allowFullScreen\" value=\"true\"><\/param><param name=\"allowscriptaccess\" value=\"always\"><\/param><embed src=\"http:\/\/www.youtube.com\/v\/BDYAbAtaDzA?fs=1\" type=\"application\/x-shockwave-flash\" width=\"425\" height=\"344\" allowscriptaccess=\"always\" allowfullscreen=\"true\"><\/embed><\/object>", "author_name": "timan38", "height": 344, "thumbnail_width": 480, "width": 425, "version": "1.0", "author_url": "http:\/\/www.youtube.com\/user\/timan38", "provider_name": "YouTube", "thumbnail_url": "http:\/\/i3.ytimg.com\/vi\/BDYAbAtaDzA\/hqdefault.jpg", "type": "video", "thumbnail_height": 360}', true));
80
81
        $media->setId(1023457);
82
83
        $this->assertSame('http://i3.ytimg.com/vi/BDYAbAtaDzA/hqdefault.jpg', $provider->getReferenceImage($media));
84
85
        $this->assertSame('default/0011/24', $provider->generatePath($media));
86
        $this->assertSame('/uploads/media/default/0011/24/thumb_1023457_big.jpg', $provider->generatePublicUrl($media, 'big'));
87
    }
88
89
    public function testThumbnail(): void
90
    {
91
        $response = $this->createMock(AbstractMessage::class);
92
        $response->expects($this->once())->method('getContent')->will($this->returnValue('content'));
93
94
        $browser = $this->createMock(Browser::class);
95
96
        $browser->expects($this->once())->method('get')->will($this->returnValue($response));
97
98
        $provider = $this->getProvider($browser);
99
100
        $media = new Media();
101
        $media->setProviderName('youtube');
102
        $media->setProviderReference('BDYAbAtaDzA');
103
        $media->setContext('default');
104
        $media->setProviderMetadata(json_decode('{"provider_url": "http:\/\/www.youtube.com\/", "title": "Nono le petit robot", "html": "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/BDYAbAtaDzA?fs=1\"><\/param><param name=\"allowFullScreen\" value=\"true\"><\/param><param name=\"allowscriptaccess\" value=\"always\"><\/param><embed src=\"http:\/\/www.youtube.com\/v\/BDYAbAtaDzA?fs=1\" type=\"application\/x-shockwave-flash\" width=\"425\" height=\"344\" allowscriptaccess=\"always\" allowfullscreen=\"true\"><\/embed><\/object>", "author_name": "timan38", "height": 344, "thumbnail_width": 480, "width": 425, "version": "1.0", "author_url": "http:\/\/www.youtube.com\/user\/timan38", "provider_name": "YouTube", "thumbnail_url": "http:\/\/i3.ytimg.com\/vi\/BDYAbAtaDzA\/hqdefault.jpg", "type": "video", "thumbnail_height": 360}', true));
105
106
        $media->setId(1023457);
107
108
        $this->assertTrue($provider->requireThumbnails($media));
0 ignored issues
show
Unused Code introduced by
The call to YouTubeProvider::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...
109
110
        $provider->addFormat('big', ['width' => 200, 'height' => 100, 'constraint' => true]);
111
112
        $this->assertNotEmpty($provider->getFormats(), '::getFormats() return an array');
113
114
        $provider->generateThumbnails($media);
115
116
        $this->assertSame('default/0011/24/thumb_1023457_big.jpg', $provider->generatePrivateUrl($media, 'big'));
117
    }
118
119
    public function testTransformWithSig(): void
120
    {
121
        $response = new Response();
122
        $response->setContent(file_get_contents(__DIR__.'/../fixtures/valid_youtube.txt'));
123
124
        $browser = $this->createMock(Browser::class);
125
        $browser->expects($this->once())->method('get')->will($this->returnValue($response));
126
127
        $provider = $this->getProvider($browser);
128
129
        $provider->addFormat('big', ['width' => 200, 'height' => 100, 'constraint' => true]);
130
131
        $media = new Media();
132
        $media->setContext('default');
133
        $media->setBinaryContent('BDYAbAtaDzA');
134
        $media->setId(1023456);
135
136
        // pre persist the media
137
        $provider->transform($media);
138
139
        $this->assertSame('Nono le petit robot', $media->getName(), '::getName() return the file name');
140
        $this->assertSame('BDYAbAtaDzA', $media->getProviderReference(), '::getProviderReference() is set');
141
    }
142
143
    /**
144
     * @dataProvider getUrls
145
     */
146
    public function testTransformWithUrl($url): void
147
    {
148
        $response = new Response();
149
        $response->setContent(file_get_contents(__DIR__.'/../fixtures/valid_youtube.txt'));
150
151
        $browser = $this->createMock(Browser::class);
152
        $browser->expects($this->once())->method('get')->will($this->returnValue($response));
153
154
        $provider = $this->getProvider($browser);
155
156
        $provider->addFormat('big', ['width' => 200, 'height' => 100, 'constraint' => true]);
157
158
        $media = new Media();
159
        $media->setContext('default');
160
        $media->setBinaryContent($url);
161
        $media->setId(1023456);
162
163
        // pre persist the media
164
        $provider->transform($media);
165
166
        $this->assertSame('Nono le petit robot', $media->getName(), '::getName() return the file name');
167
        $this->assertSame('BDYAbAtaDzA', $media->getProviderReference(), '::getProviderReference() is set');
168
    }
169
170
    public static function getUrls()
171
    {
172
        return [
173
        ['BDYAbAtaDzA'],
174
        ['http://www.youtube.com/watch?v=BDYAbAtaDzA&feature=feedrec_grec_index'],
175
        ['http://www.youtube.com/v/BDYAbAtaDzA?fs=1&amp;hl=en_US&amp;rel=0'],
176
        ['http://www.youtube.com/watch?v=BDYAbAtaDzA#t=0m10s'],
177
        ['http://www.youtube.com/embed/BDYAbAtaDzA?rel=0'],
178
        ['http://www.youtube.com/watch?v=BDYAbAtaDzA'],
179
        ['http://www.m.youtube.com/watch?v=BDYAbAtaDzA'],
180
        ['http://m.youtube.com/watch?v=BDYAbAtaDzA'],
181
        ['https://www.m.youtube.com/watch?v=BDYAbAtaDzA'],
182
        ['https://m.youtube.com/watch?v=BDYAbAtaDzA'],
183
        ['http://youtu.be/BDYAbAtaDzA'],
184
        ];
185
    }
186
187
    public function testForm(): void
188
    {
189
        $provider = $this->getProvider();
190
191
        $admin = $this->createMock(AdminInterface::class);
192
        $admin->expects($this->any())
193
            ->method('trans')
194
            ->will($this->returnValue('message'));
195
196
        $formMapper = $this->getMockBuilder(FormMapper::class)
197
            ->setMethods(['add', 'getAdmin'])
198
            ->disableOriginalConstructor()
199
            ->getMock();
200
        $formMapper->expects($this->exactly(8))
201
            ->method('add')
202
            ->will($this->returnValue(null));
203
204
        $provider->buildCreateForm($formMapper);
205
206
        $provider->buildEditForm($formMapper);
207
    }
208
209
    public function testHelperProperties(): void
210
    {
211
        $provider = $this->getProvider();
212
213
        $provider->addFormat('admin', ['width' => 100]);
214
        $media = new Media();
215
        $media->setName('Les tests');
216
        $media->setProviderReference('ASDASDAS.png');
217
        $media->setId(10);
218
        $media->setHeight(100);
219
        $media->setWidth(100);
220
221
        $properties = $provider->getHelperProperties($media, 'admin');
222
223
        $this->assertInternalType('array', $properties);
224
        $this->assertSame(100, $properties['player_parameters']['height']);
225
        $this->assertSame(100, $properties['player_parameters']['width']);
226
    }
227
228
    public function testGetReferenceUrl(): void
229
    {
230
        $media = new Media();
231
        $media->setProviderReference('123456');
232
        $this->assertEquals('https://www.youtube.com/watch?v=123456', $this->getProvider()->getReferenceUrl($media));
233
    }
234
235
    public function testMetadata()
236
    {
237
        $provider = $this->getProvider();
238
239
        $this->assertSame('youtube', $provider->getProviderMetadata()->getTitle());
240
        $this->assertSame('youtube.description', $provider->getProviderMetadata()->getDescription());
241
        $this->assertNotNull($provider->getProviderMetadata()->getImage());
242
        $this->assertSame('fa fa-youtube', $provider->getProviderMetadata()->getOption('class'));
243
        $this->assertSame('SonataMediaBundle', $provider->getProviderMetadata()->getDomain());
244
    }
245
}
246