Completed
Push — 3.x ( e95e95...638cd1 )
by Oskar
05:54
created

tests/Admin/AdminHelperTest.php (3 issues)

Severity

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');
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);
49
50
        $childFormBuilder = new FormBuilder('elementId', 'stdClass', $eventDispatcher, $formFactory);
51
        $formBuilder->add($childFormBuilder);
52
53
        $this->assertNull($this->helper->getChildFormBuilder($formBuilder, 'foo'));
54
        $this->isInstanceOf(FormBuilder::class, $this->helper->getChildFormBuilder($formBuilder, 'test_elementId'));
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'));
66
    }
67
68
    public function testAddNewInstance(): void
69
    {
70
        $admin = $this->createMock(AdminInterface::class);
71
        $admin->expects($this->once())->method('getNewInstance')->willReturn(new \stdClass());
72
73
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
74
        $fieldDescription->expects($this->once())->method('getAssociationAdmin')->willReturn($admin);
75
        $fieldDescription->expects($this->once())->method('getAssociationMapping')->willReturn(['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')->willReturn(new \stdClass());
89
90
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
91
        $fieldDescription->expects($this->once())->method('getAssociationAdmin')->willReturn($admin);
92
        $fieldDescription->expects($this->once())->method('getAssociationMapping')->willReturn(['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')->willReturn(new \stdClass());
106
107
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
108
        $fieldDescription->expects($this->once())->method('getAssociationAdmin')->willReturn($admin);
109
        $fieldDescription->expects($this->once())->method('getAssociationMapping')->willReturn(['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')->willReturn([$subObject]);
132
        $subObject->expects($this->atLeastOnce())->method('getAnother')->willReturn($sub2Object);
133
        $sub2Object->expects($this->atLeastOnce())->method('getMoreThings')->willReturn('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')->willReturn([$subObject]);
151
        $subObject->expects($this->atLeastOnce())->method('getMore')->willReturn('Value');
152
153
        $this->expectException(\Exception::class, 'Could not get element id from '.$path.' Failing part: calls');
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);
180
        $childFormBuilder = new FormBuilder('subObject', \get_class($subObject), $eventDispatcher, $formFactory);
181
182
        $object->expects($this->atLeastOnce())->method('getSubObject')->willReturn([$subObject]);
183
        $subObject->expects($this->atLeastOnce())->method('getAnd')->willReturn($sub2Object);
184
        $sub2Object->expects($this->atLeastOnce())->method('getMore')->willReturn([$sub3Object]);
185
        $sub3Object->expects($this->atLeastOnce())->method('getFinalData')->willReturn('value');
186
187
        $formBuilder->setCompound(true);
188
        $formBuilder->setDataMapper($dataMapper);
189
        $formBuilder->add($childFormBuilder);
190
191
        $admin->expects($this->once())->method('getFormBuilder')->willReturn($formBuilder);
192
        $admin->expects($this->once())->method('getSubject')->willReturn($object);
193
194
        $this->expectException(\Exception::class, 'unknown collection class');
195
196
        $this->helper->appendFormFieldElement($admin, $simpleObject, 'uniquePartOfId_sub_object_0_and_more_0_final_data');
197
    }
198
}
199