MediaTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetMetadataValue() 0 24 1
A testUnsetMetadataValue() 0 18 2
A getMediaPropertyReflection() 0 8 1
A getMedia() 0 9 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\Media;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\MediaBundle\Model\Media;
18
19
class MediaTest extends TestCase
20
{
21
    public function testSetMetadataValue(): Media
22
    {
23
        $media = $this->getMedia(853);
24
        $metadataProperty = $this->getMediaPropertyReflection('providerMetadata');
25
26
        $media->setMetadataValue('name', 'value');
27
        $metadata = $metadataProperty->getValue($media);
28
        $this->assertArrayHasKey('name', $metadata, 'name metadata should be stored in the empty array');
29
        $this->assertSame('value', $metadata['name'], 'the string value should be returned');
30
31
        $cropData = [
32
            'x' => 10,
33
            'y' => 20,
34
            'width' => 500,
35
            'height' => 500,
36
        ];
37
        $media->setMetadataValue('crop', $cropData);
38
        $metadata = $metadataProperty->getValue($media);
39
        $this->assertArrayHasKey('crop', $metadata, 'crop should be stored in the existing array');
40
        $this->assertArrayHasKey('name', $metadata, 'name metadata should still be in the array');
41
        $this->assertSame($cropData, $metadata['crop'], 'the crop data array should be returned');
42
43
        return $media;
44
    }
45
46
    /**
47
     * @depends testSetMetadataValue
48
     */
49
    public function testUnsetMetadataValue(Media $media): void
50
    {
51
        $metadataProperty = $this->getMediaPropertyReflection('providerMetadata');
52
53
        $media->unsetMetadataValue('crop');
54
        $metadata = $metadataProperty->getValue($media);
55
        $this->assertArrayNotHasKey('crop', $metadata, 'crop should not be in the metadata');
56
57
        $media->unsetMetadataValue('name');
58
        $metadata = $metadataProperty->getValue($media);
59
        $this->assertEmpty($metadata, 'crop should not be in the metadata');
60
61
        try {
62
            $media->unsetMetadataValue('bullshit');
63
        } catch (\InvalidArgumentException $expected) {
64
            $this->fail('an invalid key should be ignored');
65
        }
66
    }
67
68
    protected function getMediaPropertyReflection(string $propertyName): \ReflectionProperty
69
    {
70
        $rc = new \ReflectionClass(Media::class);
71
        $property = $rc->getProperty($propertyName);
72
        $property->setAccessible(true);
73
74
        return $property;
75
    }
76
77
    protected function getMedia($id): Media
78
    {
79
        $media = $this->getMockForAbstractClass(Media::class);
80
        $media
81
            ->method('getId')
82
            ->willReturn($id);
83
84
        return $media;
85
    }
86
}
87