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\VimeoProvider; |
29
|
|
|
use Sonata\MediaBundle\Resizer\ResizerInterface; |
30
|
|
|
use Sonata\MediaBundle\Tests\Entity\Media; |
31
|
|
|
use Sonata\MediaBundle\Thumbnail\FormatThumbnail; |
32
|
|
|
|
33
|
|
|
class VimeoProviderTest 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 VimeoProvider('vimeo', $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('Blinky™'); |
76
|
|
|
$media->setProviderName('vimeo'); |
77
|
|
|
$media->setProviderReference('21216091'); |
78
|
|
|
$media->setContext('default'); |
79
|
|
|
$media->setProviderMetadata(json_decode('{"type":"video","version":"1.0","provider_name":"Vimeo","provider_url":"http:\/\/vimeo.com\/","title":"Blinky\u2122","author_name":"Ruairi Robinson","author_url":"http:\/\/vimeo.com\/ruairirobinson","is_plus":"1","html":"<iframe src=\"http:\/\/player.vimeo.com\/video\/21216091\" width=\"1920\" height=\"1080\" frameborder=\"0\"><\/iframe>","width":"1920","height":"1080","duration":"771","description":"","thumbnail_url":"http:\/\/b.vimeocdn.com\/ts\/136\/375\/136375440_1280.jpg","thumbnail_width":1280,"thumbnail_height":720,"video_id":"21216091"}', true)); |
80
|
|
|
|
81
|
|
|
$media->setId(1023457); |
82
|
|
|
$this->assertSame('http://b.vimeocdn.com/ts/136/375/136375440_1280.jpg', $provider->getReferenceImage($media)); |
83
|
|
|
|
84
|
|
|
$this->assertSame('default/0011/24', $provider->generatePath($media)); |
85
|
|
|
$this->assertSame('/uploads/media/default/0011/24/thumb_1023457_big.jpg', $provider->generatePublicUrl($media, 'big')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testThumbnail(): void |
89
|
|
|
{ |
90
|
|
|
$response = $this->createMock(AbstractMessage::class); |
91
|
|
|
$response->expects($this->once())->method('getContent')->willReturn('content'); |
92
|
|
|
|
93
|
|
|
$browser = $this->createMock(Browser::class); |
94
|
|
|
|
95
|
|
|
$browser->expects($this->once())->method('get')->willReturn($response); |
96
|
|
|
|
97
|
|
|
$provider = $this->getProvider($browser); |
98
|
|
|
|
99
|
|
|
$media = new Media(); |
100
|
|
|
$media->setName('Blinky™'); |
101
|
|
|
$media->setProviderName('vimeo'); |
102
|
|
|
$media->setProviderReference('21216091'); |
103
|
|
|
$media->setContext('default'); |
104
|
|
|
$media->setProviderMetadata(json_decode('{"type":"video","version":"1.0","provider_name":"Vimeo","provider_url":"http:\/\/vimeo.com\/","title":"Blinky\u2122","author_name":"Ruairi Robinson","author_url":"http:\/\/vimeo.com\/ruairirobinson","is_plus":"1","html":"<iframe src=\"http:\/\/player.vimeo.com\/video\/21216091\" width=\"1920\" height=\"1080\" frameborder=\"0\"><\/iframe>","width":"1920","height":"1080","duration":"771","description":"","thumbnail_url":"http:\/\/b.vimeocdn.com\/ts\/136\/375\/136375440_1280.jpg","thumbnail_width":1280,"thumbnail_height":720,"video_id":"21216091"}', 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_vimeo.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
|
|
|
$provider->prePersist($media); |
139
|
|
|
|
140
|
|
|
$this->assertSame('Blinky™', $media->getName(), '::getName() return the file name'); |
141
|
|
|
$this->assertSame('BDYAbAtaDzA', $media->getProviderReference(), '::getProviderReference() is set'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @dataProvider getTransformWithUrlMedia |
146
|
|
|
*/ |
147
|
|
|
public function testTransformWithUrl($media): void |
148
|
|
|
{ |
149
|
|
|
$response = new Response(); |
150
|
|
|
$response->setContent(file_get_contents(__DIR__.'/../fixtures/valid_vimeo.txt')); |
151
|
|
|
|
152
|
|
|
$browser = $this->createMock(Browser::class); |
153
|
|
|
$browser->expects($this->once())->method('get')->willReturn($response); |
154
|
|
|
|
155
|
|
|
$provider = $this->getProvider($browser); |
156
|
|
|
|
157
|
|
|
$provider->addFormat('big', ['width' => 200, 'height' => 100, 'constraint' => true]); |
158
|
|
|
|
159
|
|
|
// pre persist the media |
160
|
|
|
$provider->transform($media); |
161
|
|
|
$provider->prePersist($media); |
162
|
|
|
|
163
|
|
|
$this->assertSame('Blinky™', $media->getName(), '::getName() return the file name'); |
164
|
|
|
$this->assertSame('012341231', $media->getProviderReference(), '::getProviderReference() is set'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function getTransformWithUrlMedia() |
168
|
|
|
{ |
169
|
|
|
$mediaWebsite = new Media(); |
170
|
|
|
$mediaWebsite->setContext('default'); |
171
|
|
|
$mediaWebsite->setBinaryContent('https://vimeo.com/012341231'); |
172
|
|
|
$mediaWebsite->setId(1023456); |
173
|
|
|
|
174
|
|
|
$mediaPlayer = new Media(); |
175
|
|
|
$mediaPlayer->setContext('default'); |
176
|
|
|
$mediaPlayer->setBinaryContent('https://player.vimeo.com/video/012341231'); |
177
|
|
|
$mediaPlayer->setId(1023456); |
178
|
|
|
|
179
|
|
|
return [ |
180
|
|
|
'transform with website url' => [$mediaWebsite], |
181
|
|
|
'transform with player url' => [$mediaPlayer], |
182
|
|
|
]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testGetMetadataException(): void |
186
|
|
|
{ |
187
|
|
|
$this->expectException(\RuntimeException::class); |
188
|
|
|
$this->expectExceptionMessage('Unable to retrieve the video information for :012341231'); |
189
|
|
|
$this->expectExceptionCode(12); |
190
|
|
|
|
191
|
|
|
$response = new Response(); |
192
|
|
|
$response->setContent(file_get_contents(__DIR__.'/../fixtures/valid_vimeo.txt')); |
193
|
|
|
|
194
|
|
|
$browser = $this->createMock(Browser::class); |
195
|
|
|
$browser->expects($this->once())->method('get')->will($this->throwException(new \RuntimeException('First error on get', 12))); |
196
|
|
|
|
197
|
|
|
$provider = $this->getProvider($browser); |
198
|
|
|
|
199
|
|
|
$provider->addFormat('big', ['width' => 200, 'height' => 100, 'constraint' => true]); |
200
|
|
|
|
201
|
|
|
$media = new Media(); |
202
|
|
|
$media->setBinaryContent('https://vimeo.com/012341231'); |
203
|
|
|
$media->setId(1023456); |
204
|
|
|
|
205
|
|
|
$method = new \ReflectionMethod($provider, 'getMetadata'); |
206
|
|
|
$method->setAccessible(true); |
207
|
|
|
|
208
|
|
|
$method->invokeArgs($provider, [$media, '012341231']); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function testForm(): void |
212
|
|
|
{ |
213
|
|
|
$provider = $this->getProvider(); |
214
|
|
|
|
215
|
|
|
$admin = $this->createMock(AdminInterface::class); |
216
|
|
|
$admin->expects($this->any()) |
217
|
|
|
->method('trans') |
218
|
|
|
->willReturn('message'); |
219
|
|
|
|
220
|
|
|
$formMapper = $this->getMockBuilder(FormMapper::class) |
221
|
|
|
->setMethods(['add', 'getAdmin']) |
222
|
|
|
->disableOriginalConstructor() |
223
|
|
|
->getMock(); |
224
|
|
|
$formMapper->expects($this->exactly(8)) |
225
|
|
|
->method('add') |
226
|
|
|
->willReturn(null); |
227
|
|
|
|
228
|
|
|
$provider->buildCreateForm($formMapper); |
229
|
|
|
|
230
|
|
|
$provider->buildEditForm($formMapper); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function testHelperProperies(): void |
234
|
|
|
{ |
235
|
|
|
$provider = $this->getProvider(); |
236
|
|
|
|
237
|
|
|
$provider->addFormat('admin', ['width' => 100]); |
238
|
|
|
$media = new Media(); |
239
|
|
|
$media->setName('Les tests'); |
240
|
|
|
$media->setProviderReference('ASDASDAS.png'); |
241
|
|
|
$media->setId(10); |
242
|
|
|
$media->setHeight(100); |
243
|
|
|
$media->setWidth(100); |
244
|
|
|
|
245
|
|
|
$properties = $provider->getHelperProperties($media, 'admin'); |
246
|
|
|
|
247
|
|
|
$this->assertInternalType('array', $properties); |
248
|
|
|
$this->assertSame(100, $properties['height']); |
249
|
|
|
$this->assertSame(100, $properties['width']); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function testGetReferenceUrl(): void |
253
|
|
|
{ |
254
|
|
|
$media = new Media(); |
255
|
|
|
$media->setProviderReference('123456'); |
256
|
|
|
$this->assertSame('https://vimeo.com/123456', $this->getProvider()->getReferenceUrl($media)); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function testMetadata(): void |
260
|
|
|
{ |
261
|
|
|
$provider = $this->getProvider(); |
262
|
|
|
|
263
|
|
|
$this->assertSame('vimeo', $provider->getProviderMetadata()->getTitle()); |
264
|
|
|
$this->assertSame('vimeo.description', $provider->getProviderMetadata()->getDescription()); |
265
|
|
|
$this->assertNotNull($provider->getProviderMetadata()->getImage()); |
266
|
|
|
$this->assertSame('fa fa-vimeo-square', $provider->getProviderMetadata()->getOption('class')); |
267
|
|
|
$this->assertSame('SonataMediaBundle', $provider->getProviderMetadata()->getDomain()); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
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.