Completed
Push — master ( e6c9d7...cc6bd7 )
by Grégoire
12:23
created

tests/Admin/BaseFieldDescriptionTest.php (3 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\AdminInterface;
18
use Sonata\AdminBundle\Exception\NoValueException;
19
use Sonata\AdminBundle\Tests\Fixtures\Admin\FieldDescription;
20
use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo;
21
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooBoolean;
22
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooCall;
23
24
class BaseFieldDescriptionTest extends TestCase
25
{
26
    public function testSetName(): void
27
    {
28
        $description = new FieldDescription();
29
        $description->setName('foo');
30
31
        $this->assertSame('foo', $description->getFieldName());
32
        $this->assertSame('foo', $description->getName());
33
    }
34
35
    public function testOptions(): void
36
    {
37
        $description = new FieldDescription();
38
        $description->setOption('foo', 'bar');
39
40
        $this->assertNull($description->getOption('bar'));
41
        $this->assertSame('bar', $description->getOption('foo'));
42
43
        $description->mergeOptions(['settings' => ['value_1', 'value_2']]);
44
        $description->mergeOptions(['settings' => ['value_1', 'value_3']]);
45
46
        $this->assertSame(['value_1', 'value_2', 'value_1', 'value_3'], $description->getOption('settings'));
47
48
        $description->mergeOption('settings', ['value_4']);
49
        $this->assertSame(['value_1', 'value_2', 'value_1', 'value_3', 'value_4'], $description->getOption('settings'));
50
51
        $description->mergeOption('bar', ['hello']);
52
53
        $this->assertCount(1, $description->getOption('bar'));
54
55
        $description->setOption('label', 'trucmuche');
56
        $this->assertSame('trucmuche', $description->getLabel());
57
        $this->assertNull($description->getTemplate());
58
        $description->setOptions(['type' => 'integer', 'template' => 'foo.twig.html', 'help' => 'fooHelp']);
59
60
        $this->assertSame('integer', $description->getType());
61
        $this->assertSame('foo.twig.html', $description->getTemplate());
62
        $this->assertSame('fooHelp', $description->getHelp());
63
64
        $this->assertCount(2, $description->getOptions());
65
66
        $description->setHelp('Please enter an integer');
67
        $this->assertSame('Please enter an integer', $description->getHelp());
68
69
        $description->setMappingType('int');
70
        $this->assertSame('int', $description->getMappingType());
71
72
        $this->assertSame('short_object_description_placeholder', $description->getOption('placeholder'));
73
        $description->setOptions(['placeholder' => false]);
74
        $this->assertFalse($description->getOption('placeholder'));
75
76
        $description->setOption('sortable', false);
77
        $this->assertFalse($description->isSortable());
78
79
        $description->setOption('sortable', 'field_name');
80
        $this->assertTrue($description->isSortable());
81
    }
82
83
    public function testAdmin(): void
84
    {
85
        $description = new FieldDescription();
86
87
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
88
        $description->setAdmin($admin);
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...
89
        $this->isInstanceOf(AdminInterface::class, $description->getAdmin());
90
91
        $associationAdmin = $this->getMockForAbstractClass(AdminInterface::class);
92
        $associationAdmin->expects($this->once())->method('setParentFieldDescription');
93
94
        $this->assertFalse($description->hasAssociationAdmin());
95
        $description->setAssociationAdmin($associationAdmin);
0 ignored issues
show
$associationAdmin 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...
96
        $this->assertTrue($description->hasAssociationAdmin());
97
        $this->isInstanceOf(AdminInterface::class, $description->getAssociationAdmin());
98
99
        $parent = $this->getMockForAbstractClass(AdminInterface::class);
100
        $description->setParent($parent);
0 ignored issues
show
$parent 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...
101
        $this->isInstanceOf(AdminInterface::class, $description->getParent());
102
    }
103
104
    public function testGetValue(): void
105
    {
106
        $description = new FieldDescription();
107
        $description->setOption('code', 'getFoo');
108
109
        $mock = $this->getMockBuilder('stdClass')
110
            ->setMethods(['getFoo'])
111
            ->getMock();
112
        $mock->expects($this->once())->method('getFoo')->willReturn(42);
113
114
        $this->assertSame(42, $description->getFieldValue($mock, 'fake'));
115
116
        /*
117
         * Test with One parameter int
118
         */
119
        $arg1 = 38;
120
        $oneParameter = [$arg1];
121
        $description1 = new FieldDescription();
122
        $description1->setOption('code', 'getWithOneParameter');
123
        $description1->setOption('parameters', $oneParameter);
124
125
        $mock1 = $this->getMockBuilder('stdClass')
126
            ->setMethods(['getWithOneParameter'])
127
            ->getMock();
128
        $returnValue1 = $arg1 + 2;
129
        $mock1->expects($this->once())->method('getWithOneParameter')->with($this->equalTo($arg1))->willReturn($returnValue1);
130
131
        $this->assertSame(40, $description1->getFieldValue($mock1, 'fake'));
132
133
        /*
134
         * Test with Two parameters int
135
         */
136
        $arg2 = 4;
137
        $twoParameters = [$arg1, $arg2];
138
        $description2 = new FieldDescription();
139
        $description2->setOption('code', 'getWithTwoParameters');
140
        $description2->setOption('parameters', $twoParameters);
141
142
        $mock2 = $this->getMockBuilder('stdClass')
143
            ->setMethods(['getWithTwoParameters'])
144
            ->getMock();
145
        $returnValue2 = $arg1 + $arg2;
146
        $mock2->expects($this->any())->method('getWithTwoParameters')->with($this->equalTo($arg1), $this->equalTo($arg2))->willReturn($returnValue2);
147
        $this->assertSame(42, $description2->getFieldValue($mock2, 'fake'));
148
149
        /*
150
         * Test with underscored attribute name
151
         */
152
        foreach (['getFake', 'isFake', 'hasFake'] as $method) {
153
            $description3 = new FieldDescription();
154
            $mock3 = $this->getMockBuilder('stdClass')
155
                ->setMethods([$method])
156
                ->getMock();
157
158
            $mock3->expects($this->once())->method($method)->willReturn(42);
159
            $this->assertSame(42, $description3->getFieldValue($mock3, '_fake'));
160
        }
161
162
        $mock4 = $this->getMockBuilder('MockedTestObject')
163
            ->setMethods(['myMethod'])
164
            ->getMock();
165
        $mock4->expects($this->once())
166
            ->method('myMethod')
167
            ->willReturn('myMethodValue');
168
169
        $description4 = new FieldDescription();
170
        $description4->setOption('code', 'myMethod');
171
172
        $this->assertSame($description4->getFieldValue($mock4, null), 'myMethodValue');
173
    }
174
175
    public function testGetValueNoValueException(): void
176
    {
177
        $this->expectException(\Sonata\AdminBundle\Exception\NoValueException::class);
178
179
        $description = new FieldDescription();
180
        $mock = $this->getMockBuilder('stdClass')
181
            ->setMethods(['getFoo'])
182
            ->getMock();
183
184
        $description->getFieldValue($mock, 'fake');
185
    }
186
187
    /**
188
     * @doesNotPerformAssertions
189
     */
190
    public function testGetVirtualValue(): void
191
    {
192
        $description = new FieldDescription();
193
        $mock = $this->getMockBuilder('stdClass')
194
            ->setMethods(['getFoo'])
195
            ->getMock();
196
197
        $description->setOption('virtual_field', true);
198
        $description->getFieldValue($mock, 'fake');
199
    }
200
201
    public function testExceptionOnNonArrayOption(): void
202
    {
203
        $this->expectException(\RuntimeException::class);
204
205
        $description = new FieldDescription();
206
        $description->setOption('bar', 'hello');
207
        $description->mergeOption('bar', ['exception']);
208
    }
209
210
    public function testGetTranslationDomain(): void
211
    {
212
        $description = new FieldDescription();
213
214
        $admin = $this->createMock(AdminInterface::class);
215
        $description->setAdmin($admin);
216
217
        $admin->expects($this->once())
218
            ->method('getTranslationDomain')
219
            ->willReturn('AdminDomain');
220
221
        $this->assertSame('AdminDomain', $description->getTranslationDomain());
222
223
        $admin->expects($this->never())
224
            ->method('getTranslationDomain');
225
        $description->setOption('translation_domain', 'ExtensionDomain');
226
        $this->assertSame('ExtensionDomain', $description->getTranslationDomain());
227
    }
228
229
    public function testGetInaccessibleValue(): void
230
    {
231
        $quux = 'quuX';
232
        $foo = new Foo();
233
        $foo->setQuux($quux);
234
        $ro = new \ReflectionObject($foo);
235
        $rm = $ro->getMethod('getQuux');
236
        $rm->setAccessible(true);
237
        $this->assertSame($quux, $rm->invokeArgs($foo, []));
238
239
        $description = new FieldDescription();
240
241
        $this->expectException(NoValueException::class);
242
        $description->getFieldValue($foo, 'quux');
243
    }
244
245
    public function testGetFieldValue(): void
246
    {
247
        $foo = new Foo();
248
        $foo->setBar('Bar');
249
250
        $description = new FieldDescription();
251
        $this->assertSame('Bar', $description->getFieldValue($foo, 'bar'));
252
        $foo->setBar('baR');
253
        $this->assertSame('baR', $description->getFieldValue($foo, 'bar'));
254
255
        $foo->qux = 'Qux';
256
        $this->assertSame('Qux', $description->getFieldValue($foo, 'qux'));
257
        $foo->qux = 'quX';
258
        $this->assertSame('quX', $description->getFieldValue($foo, 'qux'));
259
260
        $foo = new FooBoolean();
261
        $foo->setBar(true);
262
        $foo->setBaz(false);
263
264
        $description = new FieldDescription();
265
        $this->assertTrue($description->getFieldValue($foo, 'bar'));
266
        $this->assertFalse($description->getFieldValue($foo, 'baz'));
267
268
        $this->expectException(NoValueException::class);
269
        $description->getFieldValue($foo, 'inexistantMethod');
270
    }
271
272
    public function testGetFieldValueWithCodeOption(): void
273
    {
274
        $foo = new Foo();
275
        $foo->setBaz('Baz');
276
277
        $description = new FieldDescription();
278
279
        $description->setOption('code', 'getBaz');
280
        $this->assertSame('Baz', $description->getFieldValue($foo, 'inexistantMethod'));
281
282
        $description->setOption('code', 'inexistantMethod');
283
        $this->expectException(NoValueException::class);
284
        $description->getFieldValue($foo, 'inexistantMethod');
285
    }
286
287
    public function testGetFieldValueMagicCall(): void
288
    {
289
        $parameters = ['foo', 'bar'];
290
        $foo = new FooCall();
291
292
        $description = new FieldDescription();
293
        $description->setOption('parameters', $parameters);
294
        $this->assertSame(['inexistantMethod', $parameters], $description->getFieldValue($foo, 'inexistantMethod'));
295
296
        // repeating to cover retrieving cached getter
297
        $this->assertSame(['inexistantMethod', $parameters], $description->getFieldValue($foo, 'inexistantMethod'));
298
    }
299
300
    public function testGetFieldValueWithNullObject(): void
301
    {
302
        $foo = null;
303
        $description = new FieldDescription();
304
        $this->assertNull($description->getFieldValue($foo, 'bar'));
305
    }
306
}
307