Completed
Pull Request — master (#1163)
by Grégoire
02:51
created

YouTubeProviderTest::testForm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 0
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\MediaBundle\Tests\Provider;
13
14
use Buzz\Browser;
15
use Buzz\Message\Response;
16
use Imagine\Image\Box;
17
use Sonata\MediaBundle\Provider\YouTubeProvider;
18
use Sonata\MediaBundle\Tests\Entity\Media;
19
use Sonata\MediaBundle\Thumbnail\FormatThumbnail;
20
21
class YouTubeProviderTest extends AbstractProviderTest
22
{
23
    public function getProvider(Browser $browser = null)
24
    {
25
        if (!$browser) {
26
            $browser = $this->getMockBuilder('Buzz\Browser')->getMock();
27
        }
28
29
        $resizer = $this->getMock('Sonata\MediaBundle\Resizer\ResizerInterface');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
30
        $resizer->expects($this->any())->method('resize')->will($this->returnValue(true));
31
        $resizer->expects($this->any())->method('getBox')->will($this->returnValue(new Box(100, 100)));
32
33
        $adapter = $this->getMock('Gaufrette\Adapter');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
34
35
        $filesystem = $this->getMock('Gaufrette\Filesystem', array('get'), array($adapter));
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
36
        $file = $this->getMock('Gaufrette\File', array(), array('foo', $filesystem));
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
37
        $filesystem->expects($this->any())->method('get')->will($this->returnValue($file));
38
39
        $cdn = new \Sonata\MediaBundle\CDN\Server('/uploads/media');
40
41
        $generator = new \Sonata\MediaBundle\Generator\DefaultGenerator();
42
43
        $thumbnail = new FormatThumbnail('jpg');
44
45
        $metadata = $this->getMock('Sonata\MediaBundle\Metadata\MetadataBuilderInterface');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
46
47
        $provider = new YouTubeProvider('file', $filesystem, $cdn, $generator, $thumbnail, $browser, $metadata);
48
        $provider->setResizer($resizer);
49
50
        return $provider;
51
    }
52
53
    public function testProvider()
54
    {
55
        $provider = $this->getProvider();
56
57
        $media = new Media();
58
        $media->setName('Nono le petit robot');
59
        $media->setProviderName('youtube');
60
        $media->setProviderReference('BDYAbAtaDzA');
61
        $media->setContext('default');
62
        $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));
63
64
        $media->setId(1023457);
65
66
        $this->assertSame('http://i3.ytimg.com/vi/BDYAbAtaDzA/hqdefault.jpg', $provider->getReferenceImage($media));
67
68
        $this->assertSame('default/0011/24', $provider->generatePath($media));
69
        $this->assertSame('/uploads/media/default/0011/24/thumb_1023457_big.jpg', $provider->generatePublicUrl($media, 'big'));
70
    }
71
72
    public function testThumbnail()
73
    {
74
        $response = $this->getMock('Buzz\Message\AbstractMessage');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
75
        $response->expects($this->once())->method('getContent')->will($this->returnValue('content'));
76
77
        $browser = $this->getMockBuilder('Buzz\Browser')->getMock();
78
79
        $browser->expects($this->once())->method('get')->will($this->returnValue($response));
80
81
        $provider = $this->getProvider($browser);
82
83
        $media = new Media();
84
        $media->setProviderName('youtube');
85
        $media->setProviderReference('BDYAbAtaDzA');
86
        $media->setContext('default');
87
        $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));
88
89
        $media->setId(1023457);
90
91
        $this->assertTrue($provider->requireThumbnails($media));
0 ignored issues
show
Unused Code introduced by
The call to YouTubeProvider::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...
92
93
        $provider->addFormat('big', array('width' => 200, 'height' => 100, 'constraint' => true));
94
95
        $this->assertNotEmpty($provider->getFormats(), '::getFormats() return an array');
96
97
        $provider->generateThumbnails($media);
98
99
        $this->assertSame('default/0011/24/thumb_1023457_big.jpg', $provider->generatePrivateUrl($media, 'big'));
100
    }
101
102
    public function testTransformWithSig()
103
    {
104
        $response = new Response();
105
        $response->setContent(file_get_contents(__DIR__.'/../fixtures/valid_youtube.txt'));
106
107
        $browser = $this->getMockBuilder('Buzz\Browser')->getMock();
108
        $browser->expects($this->once())->method('get')->will($this->returnValue($response));
109
110
        $provider = $this->getProvider($browser);
111
112
        $provider->addFormat('big', array('width' => 200, 'height' => 100, 'constraint' => true));
113
114
        $media = new Media();
115
        $media->setBinaryContent('BDYAbAtaDzA');
116
        $media->setId(1023456);
117
118
        // pre persist the media
119
        $provider->transform($media);
120
121
        $this->assertSame('Nono le petit robot', $media->getName(), '::getName() return the file name');
122
        $this->assertSame('BDYAbAtaDzA', $media->getProviderReference(), '::getProviderReference() is set');
123
    }
124
125
    /**
126
     * @dataProvider getUrls
127
     */
128
    public function testTransformWithUrl($url)
129
    {
130
        $response = new Response();
131
        $response->setContent(file_get_contents(__DIR__.'/../fixtures/valid_youtube.txt'));
132
133
        $browser = $this->getMockBuilder('Buzz\Browser')->getMock();
134
        $browser->expects($this->once())->method('get')->will($this->returnValue($response));
135
136
        $provider = $this->getProvider($browser);
137
138
        $provider->addFormat('big', array('width' => 200, 'height' => 100, 'constraint' => true));
139
140
        $media = new Media();
141
        $media->setBinaryContent($url);
142
        $media->setId(1023456);
143
144
        // pre persist the media
145
        $provider->transform($media);
146
147
        $this->assertSame('Nono le petit robot', $media->getName(), '::getName() return the file name');
148
        $this->assertSame('BDYAbAtaDzA', $media->getProviderReference(), '::getProviderReference() is set');
149
    }
150
151
    public static function getUrls()
152
    {
153
        return array(
154
        array('BDYAbAtaDzA'),
155
        array('http://www.youtube.com/watch?v=BDYAbAtaDzA&feature=feedrec_grec_index'),
156
        array('http://www.youtube.com/v/BDYAbAtaDzA?fs=1&amp;hl=en_US&amp;rel=0'),
157
        array('http://www.youtube.com/watch?v=BDYAbAtaDzA#t=0m10s'),
158
        array('http://www.youtube.com/embed/BDYAbAtaDzA?rel=0'),
159
        array('http://www.youtube.com/watch?v=BDYAbAtaDzA'),
160
        array('http://www.m.youtube.com/watch?v=BDYAbAtaDzA'),
161
        array('http://m.youtube.com/watch?v=BDYAbAtaDzA'),
162
        array('https://www.m.youtube.com/watch?v=BDYAbAtaDzA'),
163
        array('https://m.youtube.com/watch?v=BDYAbAtaDzA'),
164
        array('http://youtu.be/BDYAbAtaDzA'),
165
        );
166
    }
167
168
    public function testForm()
169
    {
170
        if (!class_exists('\Sonata\AdminBundle\Form\FormMapper')) {
171
            $this->markTestSkipped("AdminBundle doesn't seem to be installed");
172
        }
173
174
        $provider = $this->getProvider();
175
176
        $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
177
        $admin->expects($this->any())
178
            ->method('trans')
179
            ->will($this->returnValue('message'));
180
181
        $formMapper = $this->getMock('Sonata\AdminBundle\Form\FormMapper', array('add', 'getAdmin'), array(), '', false);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
182
        $formMapper->expects($this->exactly(8))
183
            ->method('add')
184
            ->will($this->returnValue(null));
185
186
        $provider->buildCreateForm($formMapper);
187
188
        $provider->buildEditForm($formMapper);
189
    }
190
191
    public function testHelperProperties()
192
    {
193
        $provider = $this->getProvider();
194
195
        $provider->addFormat('admin', array('width' => 100));
196
        $media = new Media();
197
        $media->setName('Les tests');
198
        $media->setProviderReference('ASDASDAS.png');
199
        $media->setId(10);
200
        $media->setHeight(100);
201
        $media->setWidth(100);
202
203
        $properties = $provider->getHelperProperties($media, 'admin');
204
205
        $this->assertInternalType('array', $properties);
206
        $this->assertSame(100, $properties['player_parameters']['height']);
207
        $this->assertSame(100, $properties['player_parameters']['width']);
208
    }
209
210
    public function testGetReferenceUrl()
211
    {
212
        $media = new Media();
213
        $media->setProviderReference('123456');
214
        $this->assertEquals('http://www.youtube.com/watch?v=123456', $this->getProvider()->getReferenceUrl($media));
215
    }
216
}
217