MediaManagerTest::createRegistryMock()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\PHPCR;
15
16
use Doctrine\Common\Persistence\ManagerRegistry;
17
use Doctrine\ODM\PHPCR\DocumentManager;
18
use PHPUnit\Framework\TestCase;
19
use Sonata\MediaBundle\Model\MediaInterface;
20
use Sonata\MediaBundle\PHPCR\MediaManager;
21
22
/**
23
 * @group document
24
 * @group PHPCR
25
 */
26
class MediaManagerTest extends TestCase
27
{
28
    /** @var MediaManager */
29
    private $manager;
30
31
    protected function setUp(): void
32
    {
33
        $this->manager = new MediaManager(MediaInterface::class, $this->createRegistryMock());
34
    }
35
36
    public function testSave(): void
37
    {
38
        $media = new Media();
39
        $this->manager->save($media, 'default', 'media.test');
40
41
        $this->assertSame('default', $media->getContext());
42
        $this->assertSame('media.test', $media->getProviderName());
43
44
        $media = new Media();
45
        $this->manager->save($media, true);
46
47
        $this->assertNull($media->getContext());
48
        $this->assertNull($media->getProviderName());
49
    }
50
51
    public function testSaveException(): void
52
    {
53
        $this->expectException(\InvalidArgumentException::class);
54
55
        $this->manager->save(null);
56
    }
57
58
    public function testDeleteException(): void
59
    {
60
        $this->expectException(\InvalidArgumentException::class);
61
62
        $this->manager->delete(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Sonata\Doctrine\Model\T>.

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...
63
    }
64
65
    protected function createRegistryMock(): ManagerRegistry
66
    {
67
        $dm = $this->getMockBuilder(DocumentManager::class)
68
            ->setMethods(['persist', 'flush'])
69
            ->disableOriginalConstructor()
70
            ->getMock();
71
        $registry = $this->createMock(ManagerRegistry::class);
72
73
        $dm->method('persist');
74
        $dm->method('flush');
75
        $registry->method('getManagerForClass')->willReturn($dm);
76
77
        return $registry;
78
    }
79
}
80