BaseAdminModelManagerTest::testObject()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 3
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\AdminBundle\Tests\Admin;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
18
use Sonata\AdminBundle\Model\ModelManagerInterface;
19
use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
20
21
class BaseAdminModelManagerTest extends TestCase
22
{
23
    public function testHook(): void
24
    {
25
        $securityHandler = $this->getMockForAbstractClass(SecurityHandlerInterface::class);
26
27
        $modelManager = $this->getMockForAbstractClass(ModelManagerInterface::class);
28
        $modelManager->expects($this->once())->method('create');
29
        $modelManager->expects($this->once())->method('update');
30
        $modelManager->expects($this->once())->method('delete');
31
32
        $admin = new BaseAdminModelManager_Admin('code', 'class', 'controller');
33
        $admin->setModelManager($modelManager);
34
        $admin->setSecurityHandler($securityHandler);
35
36
        $t = new \stdClass();
37
38
        $admin->update($t);
0 ignored issues
show
Documentation introduced by
$t is of type object<stdClass>, but the function expects a object<Sonata\AdminBundle\Admin\object>.

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...
39
        $admin->create($t);
0 ignored issues
show
Documentation introduced by
$t is of type object<stdClass>, but the function expects a object<Sonata\AdminBundle\Admin\object>.

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...
40
        $admin->delete($t);
0 ignored issues
show
Documentation introduced by
$t is of type object<stdClass>, but the function expects a object<Sonata\AdminBundle\Admin\object>.

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...
41
    }
42
43
    public function testObject(): void
44
    {
45
        $modelManager = $this->getMockForAbstractClass(ModelManagerInterface::class);
46
        $modelManager->expects($this->once())->method('find')->willReturnCallback(static function (string $class, int $id): void {
47
            if ('class' !== $class) {
48
                throw new \RuntimeException('Invalid class argument');
49
            }
50
51
            if (10 !== $id) {
52
                throw new \RuntimeException('Invalid id argument');
53
            }
54
        });
55
56
        $admin = new BaseAdminModelManager_Admin('code', 'class', 'controller');
57
        $admin->setModelManager($modelManager);
58
        $admin->getObject(10);
59
    }
60
61
    public function testCreateQuery(): void
62
    {
63
        $query = $this->createMock(ProxyQueryInterface::class);
64
        $modelManager = $this->getMockForAbstractClass(ModelManagerInterface::class);
65
        $modelManager
66
            ->expects($this->once())
67
            ->method('createQuery')
68
            ->with('class')
69
            ->willReturn($query);
70
71
        $admin = new BaseAdminModelManager_Admin('code', 'class', 'controller');
72
        $admin->setModelManager($modelManager);
73
        $admin->createQuery();
74
    }
75
76
    public function testId(): void
77
    {
78
        $modelManager = $this->createMock(ModelManagerInterface::class);
79
        $modelManager
80
            ->expects($this->exactly(2))
81
            ->method('getNormalizedIdentifier')
82
            ->willReturn('42');
83
84
        $admin = new BaseAdminModelManager_Admin('code', 'class', 'controller');
85
        $admin->setModelManager($modelManager);
86
87
        $admin->id('Entity');
0 ignored issues
show
Documentation introduced by
'Entity' is of type string, but the function expects a object.

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...
88
        $admin->getNormalizedIdentifier('Entity');
89
    }
90
}
91