|
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\Entity; |
|
15
|
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
use Sonata\ClassificationBundle\Model\CategoryInterface; |
|
18
|
|
|
use Sonata\MediaBundle\Tests\Fixtures\EntityWithGetId; |
|
19
|
|
|
|
|
20
|
|
|
class MediaTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
public function testMetadata(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$media = new Media(); |
|
25
|
|
|
|
|
26
|
|
|
$media->setProviderMetadata(['thumbnail_url' => 'http://pasloin.com/thumb.png']); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertSame($media->getMetadataValue('thumbnail_url'), 'http://pasloin.com/thumb.png', '::getMetadataValue() return the good value'); |
|
29
|
|
|
$this->assertSame($media->getMetadataValue('thumbnail_url1', 'default'), 'default', '::getMetadataValue() return the default'); |
|
30
|
|
|
$this->assertNull($media->getMetadataValue('thumbnail_url1'), '::getMetadataValue() return the null value'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testStatusList(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$status = Media::getStatusList(); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertIsArray($status); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testSetGet(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$category = $this->prophesize(); |
|
43
|
|
|
$category->willExtend(EntityWithGetId::class); |
|
44
|
|
|
$category->willImplement(CategoryInterface::class); |
|
45
|
|
|
|
|
46
|
|
|
$media = new Media(); |
|
47
|
|
|
$media->setName('MediaBundle'); |
|
48
|
|
|
$media->setSize(12); |
|
49
|
|
|
$media->setDescription('description'); |
|
50
|
|
|
$media->setEnabled(true); |
|
51
|
|
|
$media->setProviderName('name'); |
|
52
|
|
|
$media->setLength(2); |
|
53
|
|
|
$media->setCategory($category->reveal()); |
|
54
|
|
|
$media->setCopyright('copyleft'); |
|
55
|
|
|
$media->setAuthorName('Thomas'); |
|
56
|
|
|
$media->setCdnIsFlushable(true); |
|
57
|
|
|
$media->setCdnFlushIdentifier('identifier_123'); |
|
58
|
|
|
$media->setCdnFlushAt(new \DateTime()); |
|
59
|
|
|
$media->setContentType('sonata/media'); |
|
60
|
|
|
$media->setCreatedAt(new \DateTime()); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertSame(12, $media->getSize()); |
|
63
|
|
|
$this->assertSame('description', $media->getDescription()); |
|
64
|
|
|
$this->assertTrue($media->getEnabled()); |
|
65
|
|
|
$this->assertSame('name', $media->getProviderName()); |
|
66
|
|
|
$this->assertSame(2, $media->getLength()); |
|
67
|
|
|
$this->assertSame($category->reveal(), $media->getCategory()); |
|
68
|
|
|
$this->assertSame('copyleft', $media->getCopyright()); |
|
69
|
|
|
$this->assertSame('Thomas', $media->getAuthorName()); |
|
70
|
|
|
$this->assertTrue($media->getCdnIsFlushable()); |
|
71
|
|
|
$this->assertSame('identifier_123', $media->getCdnFlushIdentifier()); |
|
72
|
|
|
$this->assertInstanceOf('DateTime', $media->getCdnFlushAt()); |
|
73
|
|
|
$this->assertInstanceOf('DateTime', $media->getCreatedAt()); |
|
74
|
|
|
$this->assertSame('sonata/media', $media->getContentType()); |
|
75
|
|
|
$this->assertSame('MediaBundle', (string) $media); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertNull($media->getMetadataValue('foo')); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testGetMediaFileExtension(): void |
|
81
|
|
|
{ |
|
82
|
|
|
$media = new Media(); |
|
83
|
|
|
|
|
84
|
|
|
$media->setProviderReference('https://sonata-project.org/bundles/sonatageneral/images/logo-small.png?some-query-string=1'); |
|
85
|
|
|
$this->assertSame('png', $media->getExtension(), 'extension should not contain query strings'); |
|
86
|
|
|
|
|
87
|
|
|
$media->setProviderReference('https://sonata-project.org/bundles/sonatageneral/images/logo-small.png#some-hash'); |
|
88
|
|
|
$this->assertSame('png', $media->getExtension(), 'extension should not contain hashes'); |
|
89
|
|
|
|
|
90
|
|
|
$media->setProviderReference('https://sonata-project.org/bundles/sonatageneral/images/logo-small.png?some-query-string=1#with-some-hash'); |
|
91
|
|
|
$this->assertSame('png', $media->getExtension(), 'extension should not contain query strings or hashes'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testSetCategoryWithoutAnActualCategory(): void |
|
95
|
|
|
{ |
|
96
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
97
|
|
|
|
|
98
|
|
|
$media = new Media(); |
|
99
|
|
|
|
|
100
|
|
|
$media->setCategory(new \stdClass()); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
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: