Completed
Push — 3.x ( b75183...b1c847 )
by Oskar
04:45
created

tests/Admin/AdminHelperTest.php (18 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\AdminBundle\Tests\Admin;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Admin\AdminHelper;
18
use Sonata\AdminBundle\Admin\AdminInterface;
19
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
20
use Sonata\AdminBundle\Admin\Pool;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
use Symfony\Component\Form\DataMapperInterface;
24
use Symfony\Component\Form\FormBuilder;
25
use Symfony\Component\Form\FormFactoryInterface;
26
use Symfony\Component\Form\FormView;
27
28
class AdminHelperTest extends TestCase
29
{
30
    /**
31
     * @var AdminHelper
32
     */
33
    protected $helper;
34
35
    public function setUp(): void
36
    {
37
        $container = $this->createMock(ContainerInterface::class);
38
39
        $pool = new Pool($container, 'title', 'logo.png');
0 ignored issues
show
$container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...ion\ContainerInterface>.

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
        $this->helper = new AdminHelper($pool);
41
    }
42
43
    public function testGetChildFormBuilder(): void
44
    {
45
        $formFactory = $this->createMock(FormFactoryInterface::class);
46
        $eventDispatcher = $this->createMock(EventDispatcherInterface::class);
47
48
        $formBuilder = new FormBuilder('test', 'stdClass', $eventDispatcher, $formFactory);
0 ignored issues
show
$eventDispatcher is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...entDispatcherInterface>.

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...
$formFactory is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...m\FormFactoryInterface>.

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...
49
50
        $childFormBuilder = new FormBuilder('elementId', 'stdClass', $eventDispatcher, $formFactory);
0 ignored issues
show
$eventDispatcher is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...entDispatcherInterface>.

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...
$formFactory is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...m\FormFactoryInterface>.

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...
51
        $formBuilder->add($childFormBuilder);
52
53
        $this->assertNull($this->helper->getChildFormBuilder($formBuilder, 'foo'));
54
        $this->isInstanceOf(FormBuilder::class, $this->helper->getChildFormBuilder($formBuilder, 'test_elementId'));
0 ignored issues
show
The call to AdminHelperTest::isInstanceOf() has too many arguments starting with $this->helper->getChildF...lder, 'test_elementId').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
55
    }
56
57
    public function testGetChildFormView(): void
58
    {
59
        $formView = new FormView();
60
        $formView->vars['id'] = 'test';
61
        $child = new FormView($formView);
62
        $child->vars['id'] = 'test_elementId';
63
64
        $this->assertNull($this->helper->getChildFormView($formView, 'foo'));
65
        $this->isInstanceOf(FormView::class, $this->helper->getChildFormView($formView, 'test_elementId'));
0 ignored issues
show
The call to AdminHelperTest::isInstanceOf() has too many arguments starting with $this->helper->getChildF...View, 'test_elementId').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
66
    }
67
68
    public function testAddNewInstance(): void
69
    {
70
        $admin = $this->createMock(AdminInterface::class);
71
        $admin->expects($this->once())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
72
73
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
74
        $fieldDescription->expects($this->once())->method('getAssociationAdmin')->will($this->returnValue($admin));
75
        $fieldDescription->expects($this->once())->method('getAssociationMapping')->will($this->returnValue(['fieldName' => 'fooBar']));
76
77
        $object = $this->getMockBuilder('stdClass')
78
            ->setMethods(['addFooBar'])
79
            ->getMock();
80
        $object->expects($this->once())->method('addFooBar');
81
82
        $this->helper->addNewInstance($object, $fieldDescription);
0 ignored issues
show
$fieldDescription is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...ldDescriptionInterface>.

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...
83
    }
84
85
    public function testAddNewInstancePlural(): void
86
    {
87
        $admin = $this->createMock(AdminInterface::class);
88
        $admin->expects($this->once())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
89
90
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
91
        $fieldDescription->expects($this->once())->method('getAssociationAdmin')->will($this->returnValue($admin));
92
        $fieldDescription->expects($this->once())->method('getAssociationMapping')->will($this->returnValue(['fieldName' => 'fooBars']));
93
94
        $object = $this->getMockBuilder('stdClass')
95
            ->setMethods(['addFooBar'])
96
            ->getMock();
97
        $object->expects($this->once())->method('addFooBar');
98
99
        $this->helper->addNewInstance($object, $fieldDescription);
0 ignored issues
show
$fieldDescription is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...ldDescriptionInterface>.

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...
100
    }
101
102
    public function testAddNewInstanceInflector(): void
103
    {
104
        $admin = $this->createMock(AdminInterface::class);
105
        $admin->expects($this->once())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
106
107
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
108
        $fieldDescription->expects($this->once())->method('getAssociationAdmin')->will($this->returnValue($admin));
109
        $fieldDescription->expects($this->once())->method('getAssociationMapping')->will($this->returnValue(['fieldName' => 'entries']));
110
111
        $object = $this->getMockBuilder('stdClass')
112
            ->setMethods(['addEntry'])
113
            ->getMock();
114
        $object->expects($this->once())->method('addEntry');
115
116
        $this->helper->addNewInstance($object, $fieldDescription);
0 ignored issues
show
$fieldDescription is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...ldDescriptionInterface>.

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...
117
    }
118
119
    public function testGetElementAccessPath(): void
120
    {
121
        $object = $this->getMockBuilder('stdClass')
122
            ->setMethods(['getPathToObject'])
123
            ->getMock();
124
        $subObject = $this->getMockBuilder('stdClass')
125
            ->setMethods(['getAnother'])
126
            ->getMock();
127
        $sub2Object = $this->getMockBuilder('stdClass')
128
            ->setMethods(['getMoreThings'])
129
            ->getMock();
130
131
        $object->expects($this->atLeastOnce())->method('getPathToObject')->will($this->returnValue([$subObject]));
132
        $subObject->expects($this->atLeastOnce())->method('getAnother')->will($this->returnValue($sub2Object));
133
        $sub2Object->expects($this->atLeastOnce())->method('getMoreThings')->will($this->returnValue('Value'));
134
135
        $path = $this->helper->getElementAccessPath('uniquePartOfId_path_to_object_0_another_more_things', $object);
136
137
        $this->assertSame('path_to_object[0].another.more_things', $path);
138
    }
139
140
    public function testItThrowsExceptionWhenDoesNotFindTheFullPath(): void
141
    {
142
        $path = 'uniquePartOfId_path_to_object_0_more_calls';
143
        $object = $this->getMockBuilder('stdClass')
144
            ->setMethods(['getPathToObject'])
145
            ->getMock();
146
        $subObject = $this->getMockBuilder('stdClass')
147
            ->setMethods(['getMore'])
148
            ->getMock();
149
150
        $object->expects($this->atLeastOnce())->method('getPathToObject')->will($this->returnValue([$subObject]));
151
        $subObject->expects($this->atLeastOnce())->method('getMore')->will($this->returnValue('Value'));
152
153
        $this->expectException(\Exception::class, 'Could not get element id from '.$path.' Failing part: calls');
0 ignored issues
show
The call to AdminHelperTest::expectException() has too many arguments starting with 'Could not get element i... ' Failing part: calls'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
154
155
        $this->helper->getElementAccessPath($path, $object);
156
    }
157
158
    public function testAppendFormFieldElementNested(): void
159
    {
160
        $admin = $this->createMock(AdminInterface::class);
161
        $object = $this->getMockBuilder('stdClass')
162
            ->setMethods(['getSubObject'])
163
            ->getMock();
164
        $simpleObject = $this->getMockBuilder('stdClass')
165
            ->setMethods(['getSubObject'])
166
            ->getMock();
167
        $subObject = $this->getMockBuilder('stdClass')
168
            ->setMethods(['getAnd'])
169
            ->getMock();
170
        $sub2Object = $this->getMockBuilder('stdClass')
171
            ->setMethods(['getMore'])
172
            ->getMock();
173
        $sub3Object = $this->getMockBuilder('stdClass')
174
            ->setMethods(['getFinalData'])
175
            ->getMock();
176
        $dataMapper = $this->createMock(DataMapperInterface::class);
177
        $formFactory = $this->createMock(FormFactoryInterface::class);
178
        $eventDispatcher = $this->createMock(EventDispatcherInterface::class);
179
        $formBuilder = new FormBuilder('test', \get_class($simpleObject), $eventDispatcher, $formFactory);
0 ignored issues
show
$eventDispatcher is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...entDispatcherInterface>.

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...
$formFactory is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...m\FormFactoryInterface>.

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...
180
        $childFormBuilder = new FormBuilder('subObject', \get_class($subObject), $eventDispatcher, $formFactory);
0 ignored issues
show
$eventDispatcher is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...entDispatcherInterface>.

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...
$formFactory is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...m\FormFactoryInterface>.

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...
181
182
        $object->expects($this->atLeastOnce())->method('getSubObject')->will($this->returnValue([$subObject]));
183
        $subObject->expects($this->atLeastOnce())->method('getAnd')->will($this->returnValue($sub2Object));
184
        $sub2Object->expects($this->atLeastOnce())->method('getMore')->will($this->returnValue([$sub3Object]));
185
        $sub3Object->expects($this->atLeastOnce())->method('getFinalData')->will($this->returnValue('value'));
186
187
        $formBuilder->setCompound(true);
188
        $formBuilder->setDataMapper($dataMapper);
0 ignored issues
show
$dataMapper is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<Symfony\Comp...rm\DataMapperInterface>.

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...
189
        $formBuilder->add($childFormBuilder);
190
191
        $admin->expects($this->once())->method('getFormBuilder')->will($this->returnValue($formBuilder));
192
        $admin->expects($this->once())->method('getSubject')->will($this->returnValue($object));
193
194
        $this->expectException(\Exception::class, 'unknown collection class');
0 ignored issues
show
The call to AdminHelperTest::expectException() has too many arguments starting with 'unknown collection class'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
195
196
        $this->helper->appendFormFieldElement($admin, $simpleObject, 'uniquePartOfId_sub_object_0_and_more_0_final_data');
0 ignored issues
show
$admin is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundle\Admin\AdminInterface>.

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...
197
    }
198
}
199