Completed
Push — master ( d8d13a...e3b085 )
by
unknown
14:53
created

tests/Builder/ListBuilderTest.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\DoctrineORMAdminBundle\Tests\Builder;
15
16
use PHPUnit\Framework\TestCase;
17
use Prophecy\Argument;
18
use Sonata\AdminBundle\Admin\AdminInterface;
19
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
20
use Sonata\DoctrineMongoDBAdminBundle\Admin\FieldDescription;
21
use Sonata\DoctrineMongoDBAdminBundle\Builder\ListBuilder;
22
use Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager;
23
use Symfony\Component\Form\Guess\Guess;
24
use Symfony\Component\Form\Guess\TypeGuess;
25
26
/**
27
 * @author Andrew Mor-Yaroslavtsev <[email protected]>
28
 */
29
class ListBuilderTest extends TestCase
30
{
31
    /**
32
     * @var TypeGuesserInterface|\Prophecy\Prophecy\ObjectProphecy
33
     */
34
    protected $typeGuesser;
35
36
    /**
37
     * @var ListBuilder
38
     */
39
    protected $listBuilder;
40
41
    /**
42
     * @var AdminInterface|\Prophecy\Prophecy\ObjectProphecy
43
     */
44
    protected $admin;
45
46
    /**
47
     * @var ModelManager|\Prophecy\Prophecy\ObjectProphecy
48
     */
49
    protected $modelManager;
50
51
    protected function setUp(): void
52
    {
53
        $this->typeGuesser = $this->prophesize('Sonata\AdminBundle\Guesser\TypeGuesserInterface');
54
55
        $this->modelManager = $this->prophesize('Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager');
56
        $this->modelManager->hasMetadata(Argument::any())->willReturn(false);
57
58
        $this->admin = $this->prophesize('Sonata\AdminBundle\Admin\AbstractAdmin');
59
        $this->admin->getClass()->willReturn('Foo');
60
        $this->admin->getModelManager()->willReturn($this->modelManager);
61
        $this->admin->addListFieldDescription(Argument::any(), Argument::any())
62
            ->willReturn();
63
64
        $this->listBuilder = new ListBuilder($this->typeGuesser->reveal());
65
    }
66
67
    public function testAddListActionField(): void
68
    {
69
        $fieldDescription = new FieldDescription();
70
        $fieldDescription->setName('foo');
71
        $list = $this->listBuilder->getBaseList();
72
        $this->listBuilder
73
            ->addField($list, 'actions', $fieldDescription, $this->admin->reveal());
0 ignored issues
show
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Sonata\AdminBundle\Admin\AdminInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
74
75
        $this->assertSame(
76
            '@SonataAdmin/CRUD/list__action.html.twig',
77
            $list->get('foo')->getTemplate(),
78
            'Custom list action field has a default list action template assigned'
79
        );
80
    }
81
82
    public function testCorrectFixedActionsFieldType(): void
83
    {
84
        $this->typeGuesser->guessType(
0 ignored issues
show
The method guessType does only exist in Sonata\AdminBundle\Guesser\TypeGuesserInterface, but not in Prophecy\Prophecy\ObjectProphecy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
85
            Argument::any(), Argument::any(), Argument::any()
0 ignored issues
show
\Prophecy\Argument::any() is of type object<Prophecy\Argument\Token\AnyValueToken>, but the function expects a object<Sonata\AdminBundl...\ModelManagerInterface>.

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...
86
        )->willReturn(
87
            new TypeGuess('actions', [], Guess::LOW_CONFIDENCE)
88
        );
89
90
        $fieldDescription = new FieldDescription();
91
        $fieldDescription->setName('_action');
92
        $list = $this->listBuilder->getBaseList();
93
        $this->listBuilder->addField($list, null, $fieldDescription, $this->admin->reveal());
0 ignored issues
show
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Sonata\AdminBundle\Admin\AdminInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
94
95
        $this->assertSame(
96
            'actions',
97
            $list->get('_action')->getType(),
98
            'Standard list _action field has "actions" type'
99
        );
100
    }
101
}
102