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')->willReturn(true); |
43
|
|
|
$resizer->expects($this->any())->method('getBox')->willReturn(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')->willReturn($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')->willReturn('content'); |
93
|
|
|
|
94
|
|
|
$browser = $this->createMock(Browser::class); |
95
|
|
|
|
96
|
|
|
$browser->expects($this->once())->method('get')->willReturn($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)); |
|
|
|
|
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')->willReturn($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')->willReturn($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&hl=en_US&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 testGetMetadataException(): void |
188
|
|
|
{ |
189
|
|
|
$this->expectException(\RuntimeException::class); |
190
|
|
|
$this->expectExceptionMessage('Unable to retrieve the video information for :BDYAbAtaDzA'); |
191
|
|
|
$this->expectExceptionCode(12); |
192
|
|
|
|
193
|
|
|
$response = new Response(); |
194
|
|
|
$response->setContent(file_get_contents(__DIR__.'/../fixtures/valid_youtube.txt')); |
195
|
|
|
|
196
|
|
|
$browser = $this->createMock(Browser::class); |
197
|
|
|
$browser->expects($this->once())->method('get')->will($this->throwException(new \RuntimeException('First error on get', 12))); |
198
|
|
|
|
199
|
|
|
$provider = $this->getProvider($browser); |
200
|
|
|
|
201
|
|
|
$provider->addFormat('big', ['width' => 200, 'height' => 100, 'constraint' => true]); |
202
|
|
|
|
203
|
|
|
$media = new Media(); |
204
|
|
|
$media->setBinaryContent('BDYAbAtaDzA'); |
205
|
|
|
$media->setId(1023456); |
206
|
|
|
|
207
|
|
|
$method = new \ReflectionMethod($provider, 'getMetadata'); |
208
|
|
|
$method->setAccessible(true); |
209
|
|
|
|
210
|
|
|
$method->invokeArgs($provider, [$media, 'BDYAbAtaDzA']); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function testForm(): void |
214
|
|
|
{ |
215
|
|
|
$provider = $this->getProvider(); |
216
|
|
|
|
217
|
|
|
$admin = $this->createMock(AdminInterface::class); |
218
|
|
|
$admin->expects($this->any()) |
219
|
|
|
->method('trans') |
220
|
|
|
->willReturn('message'); |
221
|
|
|
|
222
|
|
|
$formMapper = $this->getMockBuilder(FormMapper::class) |
223
|
|
|
->setMethods(['add', 'getAdmin']) |
224
|
|
|
->disableOriginalConstructor() |
225
|
|
|
->getMock(); |
226
|
|
|
$formMapper->expects($this->exactly(8)) |
227
|
|
|
->method('add') |
228
|
|
|
->willReturn(null); |
229
|
|
|
|
230
|
|
|
$provider->buildCreateForm($formMapper); |
231
|
|
|
|
232
|
|
|
$provider->buildEditForm($formMapper); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function testHelperProperties(): void |
236
|
|
|
{ |
237
|
|
|
$provider = $this->getProvider(); |
238
|
|
|
|
239
|
|
|
$provider->addFormat('admin', ['width' => 100]); |
240
|
|
|
$media = new Media(); |
241
|
|
|
$media->setName('Les tests'); |
242
|
|
|
$media->setProviderReference('ASDASDAS.png'); |
243
|
|
|
$media->setId(10); |
244
|
|
|
$media->setHeight(100); |
245
|
|
|
$media->setWidth(100); |
246
|
|
|
|
247
|
|
|
$properties = $provider->getHelperProperties($media, 'admin'); |
248
|
|
|
|
249
|
|
|
$this->assertInternalType('array', $properties); |
250
|
|
|
$this->assertSame(100, $properties['player_parameters']['height']); |
251
|
|
|
$this->assertSame(100, $properties['player_parameters']['width']); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function testGetReferenceUrl(): void |
255
|
|
|
{ |
256
|
|
|
$media = new Media(); |
257
|
|
|
$media->setProviderReference('123456'); |
258
|
|
|
$this->assertSame('https://www.youtube.com/watch?v=123456', $this->getProvider()->getReferenceUrl($media)); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function testMetadata(): void |
262
|
|
|
{ |
263
|
|
|
$provider = $this->getProvider(); |
264
|
|
|
|
265
|
|
|
$this->assertSame('youtube', $provider->getProviderMetadata()->getTitle()); |
266
|
|
|
$this->assertSame('youtube.description', $provider->getProviderMetadata()->getDescription()); |
267
|
|
|
$this->assertNotNull($provider->getProviderMetadata()->getImage()); |
268
|
|
|
$this->assertSame('fa fa-youtube', $provider->getProviderMetadata()->getOption('class')); |
269
|
|
|
$this->assertSame('SonataMediaBundle', $provider->getProviderMetadata()->getDomain()); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
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.