Completed
Push — master ( aed54c...5a7b0e )
by Grégoire
13s queued 10s
created

testGetMetadataException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
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\DailyMotionProvider;
29
use Sonata\MediaBundle\Resizer\ResizerInterface;
30
use Sonata\MediaBundle\Tests\Entity\Media;
31
use Sonata\MediaBundle\Thumbnail\FormatThumbnail;
32
33
class DailyMotionProviderTest 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 DailyMotionProvider('file', $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('les tests fonctionnels - Symfony Live 2009');
76
        $media->setProviderName('dailymotion');
77
        $media->setProviderReference('x9wjql');
78
        $media->setContext('default');
79
        $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));
80
81
        $this->assertSame('http://ak2.static.dailymotion.com/static/video/711/536/16635117:jpeg_preview_large.jpg?20100801072241', $provider->getReferenceImage($media));
82
83
        $media->setId(1023458);
84
85
        $this->assertSame('default/0011/24', $provider->generatePath($media));
86
        $this->assertSame('/uploads/media/default/0011/24/thumb_1023458_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->setName('les tests fonctionnels - Symfony Live 2009');
102
        $media->setProviderName('dailymotion');
103
        $media->setProviderReference('x9wjql');
104
        $media->setContext('default');
105
        $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));
106
107
        $media->setId(1023458);
108
109
        $this->assertTrue($provider->requireThumbnails($media));
0 ignored issues
show
Unused Code introduced by
The call to DailyMotionProvider::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...
110
111
        $provider->addFormat('big', ['width' => 200, 'height' => null, 'constraint' => true]);
112
113
        $this->assertNotEmpty($provider->getFormats(), '::getFormats() return an array');
114
115
        $provider->generateThumbnails($media);
116
117
        $this->assertSame('default/0011/24/thumb_1023458_big.jpg', $provider->generatePrivateUrl($media, 'big'));
118
    }
119
120
    public function testTransformWithSig(): void
121
    {
122
        $response = new Response();
123
        $response->setContent(file_get_contents(__DIR__.'/../fixtures/valid_dailymotion.txt'));
124
125
        $browser = $this->createMock(Browser::class);
126
        $browser->expects($this->once())->method('get')->willReturn($response);
127
128
        $provider = $this->getProvider($browser);
129
130
        $provider->addFormat('big', ['width' => 200, 'height' => null, 'constraint' => true]);
131
132
        $media = new Media();
133
        $media->setContext('default');
134
        $media->setBinaryContent('x9wjql');
135
        $media->setId(1023456);
136
137
        // pre persist the media
138
        $provider->transform($media);
139
140
        $this->assertSame('Thomas Rabaix - les tests fonctionnels - Symfony Live 2009', $media->getName(), '::getName() return the file name');
141
        $this->assertSame('x9wjql', $media->getProviderReference(), '::getProviderReference() is set');
142
    }
143
144
    /**
145
     * @dataProvider dataTransformWithUrl
146
     */
147
    public function testTransformWithUrl(string $url): void
148
    {
149
        $response = new Response();
150
        $response->setContent(file_get_contents(__DIR__.'/../fixtures/valid_dailymotion.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' => null, 'constraint' => true]);
158
159
        $media = new Media();
160
        $media->setContext('default');
161
        $media->setBinaryContent($url);
162
        $media->setId(1023456);
163
164
        // pre persist the media
165
        $provider->transform($media);
166
167
        $this->assertSame('Thomas Rabaix - les tests fonctionnels - Symfony Live 2009', $media->getName(), '::getName() return the file name');
168
        $this->assertSame('x9wjql', $media->getProviderReference(), '::getProviderReference() is set');
169
    }
170
171
    public function dataTransformWithUrl()
172
    {
173
        return [
174
            ['http://www.dailymotion.com/video/x9wjql_asdasdasdsa_asdsds'],
175
            ['http://www.dailymotion.com/video/x9wjql'],
176
            ['https://www.dailymotion.com/video/x9wjql'],
177
            ['www.dailymotion.com/video/x9wjql'],
178
            ['x9wjql'],
179
        ];
180
    }
181
182
    public function testGetMetadataException(): void
183
    {
184
        $this->expectException(\RuntimeException::class);
185
        $this->expectExceptionMessage('Unable to retrieve the video information for :x9wjql');
186
        $this->expectExceptionCode(12);
187
188
        $response = new Response();
189
        $response->setContent(file_get_contents(__DIR__.'/../fixtures/valid_dailymotion.txt'));
190
191
        $browser = $this->createMock(Browser::class);
192
        $browser->expects($this->once())->method('get')->will($this->throwException(new \RuntimeException('First error on get', 12)));
193
194
        $provider = $this->getProvider($browser);
195
196
        $provider->addFormat('big', ['width' => 200, 'height' => 100, 'constraint' => true]);
197
198
        $media = new Media();
199
        $media->setBinaryContent('x9wjql');
200
        $media->setId(1023456);
201
202
        $method = new \ReflectionMethod($provider, 'getMetadata');
203
        $method->setAccessible(true);
204
205
        $method->invokeArgs($provider, [$media, 'x9wjql']);
206
    }
207
208
    public function testForm(): void
209
    {
210
        $provider = $this->getProvider();
211
212
        $admin = $this->createMock(AdminInterface::class);
213
        $admin->expects($this->any())
214
            ->method('trans')
215
            ->willReturn('message');
216
217
        $formMapper = $this->getMockBuilder(FormMapper::class)
218
            ->setMethods(['add', 'getAdmin'])
219
            ->disableOriginalConstructor()
220
            ->getMock();
221
        $formMapper->expects($this->exactly(8))
222
            ->method('add')
223
            ->willReturn(null);
224
225
        $provider->buildCreateForm($formMapper);
226
227
        $provider->buildEditForm($formMapper);
228
    }
229
230
    public function testHelperProperies(): void
231
    {
232
        $provider = $this->getProvider();
233
234
        $provider->addFormat('admin', ['width' => 100]);
235
        $media = new Media();
236
        $media->setName('Les tests');
237
        $media->setProviderReference('ASDASDAS.png');
238
        $media->setId(10);
239
        $media->setHeight(100);
240
        $media->setWidth(100);
241
242
        $properties = $provider->getHelperProperties($media, 'admin');
243
244
        $this->assertInternalType('array', $properties);
245
        $this->assertSame(100, $properties['height']);
246
        $this->assertSame(100, $properties['width']);
247
    }
248
249
    public function testGetReferenceUrl(): void
250
    {
251
        $media = new Media();
252
        $media->setProviderReference('123456');
253
        $this->assertSame('http://www.dailymotion.com/video/123456', $this->getProvider()->getReferenceUrl($media));
254
    }
255
}
256