Completed
Push — master ( 56e6e4...3e9305 )
by Grégoire
11s
created

testSetCategoryWithoutAnActualCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 9.4285
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\Entity;
13
14
use Sonata\MediaBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
15
16
class MediaTest extends PHPUnit_Framework_TestCase
17
{
18
    public function testMetadata()
19
    {
20
        $media = new Media();
21
22
        $media->setProviderMetadata(array('thumbnail_url' => 'http://pasloin.com/thumb.png'));
23
24
        $this->assertSame($media->getMetadataValue('thumbnail_url'), 'http://pasloin.com/thumb.png', '::getMetadataValue() return the good value');
25
        $this->assertSame($media->getMetadataValue('thumbnail_url1', 'default'), 'default', '::getMetadataValue() return the default');
26
        $this->assertSame($media->getMetadataValue('thumbnail_url1'), null, '::getMetadataValue() return the null value');
27
    }
28
29
    public function testStatusList()
30
    {
31
        $status = Media::getStatusList();
32
33
        $this->assertInternalType('array', $status);
34
    }
35
36
    public function testSetGet()
37
    {
38
        $category = $this->prophesize();
39
        $category->willExtend('Sonata\MediaBundle\Tests\Controller\EntityWithGetId');
40
        $category->willImplement('Sonata\ClassificationBundle\Model\CategoryInterface');
41
42
        $media = new Media();
43
        $media->setName('MediaBundle');
44
        $media->setSize(12);
45
        $media->setDescription('description');
46
        $media->setEnabled(true);
47
        $media->setProviderName('name');
48
        $media->setLength(2);
49
        $media->setCategory($category->reveal());
50
        $media->setCopyright('copyleft');
51
        $media->setAuthorName('Thomas');
52
        $media->setCdnIsFlushable(true);
53
        $media->setCdnFlushIdentifier('identifier_123');
0 ignored issues
show
Documentation introduced by
'identifier_123' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
        $media->setCdnFlushAt(new \DateTime());
55
        $media->setContentType('sonata/media');
56
        $media->setCreatedAt(new \DateTime());
57
58
        $this->assertSame(12, $media->getSize());
59
        $this->assertSame('description', $media->getDescription());
60
        $this->assertTrue($media->getEnabled());
61
        $this->assertSame('name', $media->getProviderName());
62
        $this->assertSame(2, $media->getLength());
63
        $this->assertSame($category->reveal(), $media->getCategory());
64
        $this->assertSame('copyleft', $media->getCopyright());
65
        $this->assertSame('Thomas', $media->getAuthorName());
66
        $this->assertTrue($media->getCdnIsFlushable());
67
        $this->assertSame('identifier_123', $media->getCdnFlushIdentifier());
68
        $this->assertInstanceOf('DateTime', $media->getCdnFlushAt());
69
        $this->assertInstanceOf('DateTime', $media->getCreatedAt());
70
        $this->assertSame('sonata/media', $media->getContentType());
71
        $this->assertSame('MediaBundle', (string) $media);
72
73
        $this->assertNull($media->getMetadataValue('foo'));
74
    }
75
76
    public function testGetMediaFileExtension()
77
    {
78
        $media = new Media();
79
80
        $media->setProviderReference('https://sonata-project.org/bundles/sonatageneral/images/logo-small.png?some-query-string=1');
81
        $this->assertSame('png', $media->getExtension(), 'extension should not contain query strings');
82
83
        $media->setProviderReference('https://sonata-project.org/bundles/sonatageneral/images/logo-small.png#some-hash');
84
        $this->assertSame('png', $media->getExtension(), 'extension should not contain hashes');
85
86
        $media->setProviderReference('https://sonata-project.org/bundles/sonatageneral/images/logo-small.png?some-query-string=1#with-some-hash');
87
        $this->assertSame('png', $media->getExtension(), 'extension should not contain query strings or hashes');
88
    }
89
90
    public function testSetCategoryWithoutAnActualCategory()
91
    {
92
        $this->expectException('\InvalidArgumentException');
93
94
        $media = new Media();
95
96
        $media->setCategory(new \stdClass());
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a object<Sonata\MediaBundl...CategoryInterface>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
97
    }
98
}
99