Completed
Push — master ( 5482a9...c97bf1 )
by Grégoire
02:48
created

DailyMotionProviderTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 0
loc 197
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getProvider() 0 34 2
A testProvider() 0 18 1
A testThumbnail() 0 30 1
A testTransformWithSig() 0 23 1
A testTransformWithUrl() 0 23 1
A dataTransformWithUrl() 0 10 1
A testForm() 0 21 1
A testHelperProperies() 0 18 1
A testGetReferenceUrl() 0 6 1
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')->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 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')->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->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')->will($this->returnValue($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')->will($this->returnValue($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 testForm(): void
183
    {
184
        $provider = $this->getProvider();
185
186
        $admin = $this->createMock(AdminInterface::class);
187
        $admin->expects($this->any())
188
            ->method('trans')
189
            ->will($this->returnValue('message'));
190
191
        $formMapper = $this->getMockBuilder(FormMapper::class)
192
            ->setMethods(['add', 'getAdmin'])
193
            ->disableOriginalConstructor()
194
            ->getMock();
195
        $formMapper->expects($this->exactly(8))
196
            ->method('add')
197
            ->will($this->returnValue(null));
198
199
        $provider->buildCreateForm($formMapper);
200
201
        $provider->buildEditForm($formMapper);
202
    }
203
204
    public function testHelperProperies(): void
205
    {
206
        $provider = $this->getProvider();
207
208
        $provider->addFormat('admin', ['width' => 100]);
209
        $media = new Media();
210
        $media->setName('Les tests');
211
        $media->setProviderReference('ASDASDAS.png');
212
        $media->setId(10);
213
        $media->setHeight(100);
214
        $media->setWidth(100);
215
216
        $properties = $provider->getHelperProperties($media, 'admin');
217
218
        $this->assertInternalType('array', $properties);
219
        $this->assertSame(100, $properties['height']);
220
        $this->assertSame(100, $properties['width']);
221
    }
222
223
    public function testGetReferenceUrl(): void
224
    {
225
        $media = new Media();
226
        $media->setProviderReference('123456');
227
        $this->assertEquals('http://www.dailymotion.com/video/123456', $this->getProvider()->getReferenceUrl($media));
228
    }
229
}
230