|
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\IdGenerator; |
|
27
|
|
|
use Sonata\MediaBundle\Metadata\MetadataBuilderInterface; |
|
28
|
|
|
use Sonata\MediaBundle\Provider\DailyMotionProvider; |
|
29
|
|
|
use Sonata\MediaBundle\Provider\MediaProviderInterface; |
|
30
|
|
|
use Sonata\MediaBundle\Resizer\ResizerInterface; |
|
31
|
|
|
use Sonata\MediaBundle\Tests\Entity\Media; |
|
32
|
|
|
use Sonata\MediaBundle\Thumbnail\FormatThumbnail; |
|
33
|
|
|
|
|
34
|
|
|
class DailyMotionProviderTest extends AbstractProviderTest |
|
35
|
|
|
{ |
|
36
|
|
|
public function getProvider(?Browser $browser = null): MediaProviderInterface |
|
37
|
|
|
{ |
|
38
|
|
|
if (!$browser) { |
|
39
|
|
|
$browser = $this->createMock(Browser::class); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$resizer = $this->createMock(ResizerInterface::class); |
|
43
|
|
|
$resizer->method('resize')->willReturn(true); |
|
44
|
|
|
$resizer->method('getBox')->willReturn(new Box(100, 100)); |
|
45
|
|
|
|
|
46
|
|
|
$adapter = $this->createMock(Adapter::class); |
|
47
|
|
|
|
|
48
|
|
|
$filesystem = $this->getMockBuilder(Filesystem::class) |
|
49
|
|
|
->onlyMethods(['get']) |
|
50
|
|
|
->setConstructorArgs([$adapter]) |
|
51
|
|
|
->getMock(); |
|
52
|
|
|
$file = $this->getMockBuilder(File::class) |
|
53
|
|
|
->setConstructorArgs(['foo', $filesystem]) |
|
54
|
|
|
->getMock(); |
|
55
|
|
|
$filesystem->method('get')->willReturn($file); |
|
56
|
|
|
|
|
57
|
|
|
$cdn = new Server('/uploads/media'); |
|
58
|
|
|
|
|
59
|
|
|
$generator = new IdGenerator(); |
|
60
|
|
|
|
|
61
|
|
|
$thumbnail = new FormatThumbnail('jpg'); |
|
62
|
|
|
|
|
63
|
|
|
$metadata = $this->createMock(MetadataBuilderInterface::class); |
|
64
|
|
|
|
|
65
|
|
|
$provider = new DailyMotionProvider('file', $filesystem, $cdn, $generator, $thumbnail, $browser, $metadata); |
|
66
|
|
|
$provider->setResizer($resizer); |
|
67
|
|
|
|
|
68
|
|
|
return $provider; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testProvider(): void |
|
72
|
|
|
{ |
|
73
|
|
|
$media = new Media(); |
|
74
|
|
|
$media->setName('les tests fonctionnels - Symfony Live 2009'); |
|
75
|
|
|
$media->setProviderName('dailymotion'); |
|
76
|
|
|
$media->setProviderReference('x9wjql'); |
|
77
|
|
|
$media->setContext('default'); |
|
78
|
|
|
$media->setProviderMetadata(json_decode('{"type":"video","version":"1.0","provider_name":"Dailymotion","provider_url":"http:\/\/www.dailymotion.com","title":"Thomas Rabaix - les tests fonctionnels - Symfony Live 2009","author_name":"Guillaume Pon\u00e7on","author_url":"http:\/\/www.dailymotion.com\/phptv","width":480,"height":270,"html":"<iframe src=\"http:\/\/www.dailymotion.com\/embed\/video\/x9wjql\" width=\"480\" height=\"270\" frameborder=\"0\"><\/iframe>","thumbnail_url":"http:\/\/ak2.static.dailymotion.com\/static\/video\/711\/536\/16635117:jpeg_preview_large.jpg?20100801072241","thumbnail_width":426.666666667,"thumbnail_height":240}', true)); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertSame('http://ak2.static.dailymotion.com/static/video/711/536/16635117:jpeg_preview_large.jpg?20100801072241', $this->provider->getReferenceImage($media)); |
|
81
|
|
|
|
|
82
|
|
|
$media->setId(1023458); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertSame('default/0011/24', $this->provider->generatePath($media)); |
|
85
|
|
|
$this->assertSame('/uploads/media/default/0011/24/thumb_1023458_big.jpg', $this->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('les tests fonctionnels - Symfony Live 2009'); |
|
101
|
|
|
$media->setProviderName('dailymotion'); |
|
102
|
|
|
$media->setProviderReference('x9wjql'); |
|
103
|
|
|
$media->setContext('default'); |
|
104
|
|
|
$media->setProviderMetadata(json_decode('{"type":"video","version":"1.0","provider_name":"Dailymotion","provider_url":"http:\/\/www.dailymotion.com","title":"Thomas Rabaix - les tests fonctionnels - Symfony Live 2009","author_name":"Guillaume Pon\u00e7on","author_url":"http:\/\/www.dailymotion.com\/phptv","width":480,"height":270,"html":"<iframe src=\"http:\/\/www.dailymotion.com\/embed\/video\/x9wjql\" width=\"480\" height=\"270\" frameborder=\"0\"><\/iframe>","thumbnail_url":"http:\/\/ak2.static.dailymotion.com\/static\/video\/711\/536\/16635117:jpeg_preview_large.jpg?20100801072241","thumbnail_width":426.666666667,"thumbnail_height":240}', true)); |
|
105
|
|
|
|
|
106
|
|
|
$media->setId(1023458); |
|
107
|
|
|
|
|
108
|
|
|
$this->assertTrue($provider->requireThumbnails()); |
|
109
|
|
|
|
|
110
|
|
|
$provider->addFormat('big', ['width' => 200, 'height' => null, '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_1023458_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_dailymotion.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' => null, 'constraint' => true]); |
|
130
|
|
|
|
|
131
|
|
|
$media = new Media(); |
|
132
|
|
|
$media->setContext('default'); |
|
133
|
|
|
$media->setBinaryContent('x9wjql'); |
|
134
|
|
|
$media->setId(1023456); |
|
135
|
|
|
|
|
136
|
|
|
// pre persist the media |
|
137
|
|
|
$provider->transform($media); |
|
138
|
|
|
|
|
139
|
|
|
$this->assertSame('Thomas Rabaix - les tests fonctionnels - Symfony Live 2009', $media->getName(), '::getName() return the file name'); |
|
140
|
|
|
$this->assertSame('x9wjql', $media->getProviderReference(), '::getProviderReference() is set'); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @dataProvider dataTransformWithUrl |
|
145
|
|
|
*/ |
|
146
|
|
|
public function testTransformWithUrl(string $url): void |
|
147
|
|
|
{ |
|
148
|
|
|
$response = new Response(); |
|
149
|
|
|
$response->setContent(file_get_contents(__DIR__.'/../fixtures/valid_dailymotion.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' => null, '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('Thomas Rabaix - les tests fonctionnels - Symfony Live 2009', $media->getName(), '::getName() return the file name'); |
|
167
|
|
|
$this->assertSame('x9wjql', $media->getProviderReference(), '::getProviderReference() is set'); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function dataTransformWithUrl(): array |
|
171
|
|
|
{ |
|
172
|
|
|
return [ |
|
173
|
|
|
['http://www.dailymotion.com/video/x9wjql_asdasdasdsa_asdsds'], |
|
174
|
|
|
['http://www.dailymotion.com/video/x9wjql'], |
|
175
|
|
|
['https://www.dailymotion.com/video/x9wjql'], |
|
176
|
|
|
['www.dailymotion.com/video/x9wjql'], |
|
177
|
|
|
['x9wjql'], |
|
178
|
|
|
]; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function testGetMetadataException(): void |
|
182
|
|
|
{ |
|
183
|
|
|
$this->expectException(\RuntimeException::class); |
|
184
|
|
|
$this->expectExceptionMessage('Unable to retrieve the video information for :x9wjql'); |
|
185
|
|
|
$this->expectExceptionCode(12); |
|
186
|
|
|
|
|
187
|
|
|
$response = new Response(); |
|
188
|
|
|
$response->setContent(file_get_contents(__DIR__.'/../fixtures/valid_dailymotion.txt')); |
|
189
|
|
|
|
|
190
|
|
|
$browser = $this->createMock(Browser::class); |
|
191
|
|
|
$browser->expects($this->once())->method('get')->will($this->throwException(new \RuntimeException('First error on get', 12))); |
|
192
|
|
|
|
|
193
|
|
|
$provider = $this->getProvider($browser); |
|
194
|
|
|
|
|
195
|
|
|
$provider->addFormat('big', ['width' => 200, 'height' => 100, 'constraint' => true]); |
|
196
|
|
|
|
|
197
|
|
|
$media = new Media(); |
|
198
|
|
|
$media->setBinaryContent('x9wjql'); |
|
199
|
|
|
$media->setId(1023456); |
|
200
|
|
|
|
|
201
|
|
|
$method = new \ReflectionMethod($provider, 'getMetadata'); |
|
202
|
|
|
$method->setAccessible(true); |
|
203
|
|
|
|
|
204
|
|
|
$method->invokeArgs($provider, [$media, 'x9wjql']); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function testForm(): void |
|
208
|
|
|
{ |
|
209
|
|
|
$provider = $this->getProvider(); |
|
210
|
|
|
|
|
211
|
|
|
$admin = $this->createMock(AdminInterface::class); |
|
212
|
|
|
$admin |
|
213
|
|
|
->method('trans') |
|
214
|
|
|
->willReturn('message'); |
|
215
|
|
|
|
|
216
|
|
|
$formMapper = $this->createMock(FormMapper::class); |
|
217
|
|
|
$formMapper->expects($this->exactly(8)) |
|
218
|
|
|
->method('add') |
|
219
|
|
|
->willReturn(null); |
|
220
|
|
|
|
|
221
|
|
|
$provider->buildCreateForm($formMapper); |
|
222
|
|
|
|
|
223
|
|
|
$provider->buildEditForm($formMapper); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function testHelperProperties(): void |
|
227
|
|
|
{ |
|
228
|
|
|
$this->provider->addFormat('admin', ['width' => 100]); |
|
229
|
|
|
$media = new Media(); |
|
230
|
|
|
$media->setName('Les tests'); |
|
231
|
|
|
$media->setProviderReference('ASDASDAS.png'); |
|
232
|
|
|
$media->setId(10); |
|
233
|
|
|
$media->setHeight(100); |
|
234
|
|
|
$media->setWidth(100); |
|
235
|
|
|
|
|
236
|
|
|
$properties = $this->provider->getHelperProperties($media, 'admin'); |
|
237
|
|
|
|
|
238
|
|
|
$this->assertIsArray($properties); |
|
239
|
|
|
$this->assertSame(100, $properties['height']); |
|
240
|
|
|
$this->assertSame(100, $properties['width']); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
public function testGetReferenceUrl(): void |
|
244
|
|
|
{ |
|
245
|
|
|
$media = new Media(); |
|
246
|
|
|
$media->setProviderReference('123456'); |
|
247
|
|
|
$this->assertSame('http://www.dailymotion.com/video/123456', $this->provider->getReferenceUrl($media)); |
|
|
|
|
|
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: