Completed
Push — 3.x ( b23d83...26a887 )
by Jordi Sala
02:57
created

BaseFieldDescriptionTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 305
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 5
dl 0
loc 305
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetName() 0 8 1
A testOptions() 0 43 1
A testHelpOptions() 0 10 1
A testAdmin() 0 20 1
B testGetValue() 0 70 2
A testGetValueNoValueException() 0 11 1
A testGetVirtualValue() 0 10 1
A testExceptionOnNonArrayOption() 0 8 1
A testGetTranslationDomain() 0 18 1
A testCamelize() 0 6 1
A testGetInaccessibleValue() 0 15 1
A testGetFieldValue() 0 26 1
A testGetFieldValueWithCodeOption() 0 14 1
A testGetFieldValueMagicCall() 0 12 1
A testGetFieldValueWithNullObject() 0 6 1
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\Admin\BaseFieldDescription;
19
use Sonata\AdminBundle\Exception\NoValueException;
20
use Sonata\AdminBundle\Tests\Fixtures\Admin\FieldDescription;
21
use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo;
22
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooBoolean;
23
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooCall;
24
25
class BaseFieldDescriptionTest extends TestCase
26
{
27
    public function testSetName(): void
28
    {
29
        $description = new FieldDescription();
30
        $description->setName('foo');
31
32
        $this->assertSame('foo', $description->getFieldName());
33
        $this->assertSame('foo', $description->getName());
34
    }
35
36
    public function testOptions(): void
37
    {
38
        $description = new FieldDescription();
39
        $description->setOption('foo', 'bar');
40
41
        $this->assertNull($description->getOption('bar'));
42
        $this->assertSame('bar', $description->getOption('foo'));
43
44
        $description->mergeOptions(['settings' => ['value_1', 'value_2']]);
45
        $description->mergeOptions(['settings' => ['value_1', 'value_3']]);
46
47
        $this->assertSame(['value_1', 'value_2', 'value_1', 'value_3'], $description->getOption('settings'));
48
49
        $description->mergeOption('settings', ['value_4']);
50
        $this->assertSame(['value_1', 'value_2', 'value_1', 'value_3', 'value_4'], $description->getOption('settings'));
51
52
        $description->mergeOption('bar', ['hello']);
53
54
        $this->assertCount(1, $description->getOption('bar'));
55
56
        $description->setOption('label', 'trucmuche');
57
        $this->assertSame('trucmuche', $description->getLabel());
58
        $this->assertNull($description->getTemplate());
59
        $description->setOptions(['type' => 'integer', 'template' => 'foo.twig.html']);
60
61
        $this->assertSame('integer', $description->getType());
62
        $this->assertSame('foo.twig.html', $description->getTemplate());
63
64
        $this->assertCount(2, $description->getOptions());
65
66
        $description->setMappingType('int');
67
        $this->assertSame('int', $description->getMappingType());
68
69
        $this->assertSame('short_object_description_placeholder', $description->getOption('placeholder'));
70
        $description->setOptions(['placeholder' => false]);
71
        $this->assertFalse($description->getOption('placeholder'));
72
73
        $description->setOption('sortable', false);
74
        $this->assertFalse($description->isSortable());
75
76
        $description->setOption('sortable', 'field_name');
77
        $this->assertTrue($description->isSortable());
78
    }
79
80
    /**
81
     * NEXT_MAJOR: Remove this test.
82
     *
83
     * @group legacy
84
     */
85
    public function testHelpOptions(): void
86
    {
87
        $description = new FieldDescription();
88
89
        $description->setHelp('Please enter an integer');
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...dDescription::setHelp() has been deprecated with message: since sonata-project/admin-bundle 3.x and will be removed in version 4.0. Use Symfony Form "help" option instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
90
        $this->assertSame('Please enter an integer', $description->getHelp());
91
92
        $description->setOptions(['help' => 'fooHelp']);
93
        $this->assertSame('fooHelp', $description->getHelp());
94
    }
95
96
    public function testAdmin(): void
97
    {
98
        $description = new FieldDescription();
99
100
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
101
        $description->setAdmin($admin);
102
        $this->assertInstanceOf(AdminInterface::class, $description->getAdmin());
103
104
        $associationAdmin = $this->getMockForAbstractClass(AdminInterface::class);
105
        $associationAdmin->expects($this->once())->method('setParentFieldDescription');
106
107
        $this->assertFalse($description->hasAssociationAdmin());
108
        $description->setAssociationAdmin($associationAdmin);
109
        $this->assertTrue($description->hasAssociationAdmin());
110
        $this->assertInstanceOf(AdminInterface::class, $description->getAssociationAdmin());
111
112
        $parent = $this->getMockForAbstractClass(AdminInterface::class);
113
        $description->setParent($parent);
114
        $this->assertInstanceOf(AdminInterface::class, $description->getParent());
115
    }
116
117
    public function testGetValue(): void
118
    {
119
        $description = new FieldDescription();
120
        $description->setOption('code', 'getFoo');
121
122
        $mock = $this->getMockBuilder(\stdClass::class)
123
            ->setMethods(['getFoo'])
124
            ->getMock();
125
        $mock->expects($this->once())->method('getFoo')->willReturn(42);
126
127
        $this->assertSame(42, $description->getFieldValue($mock, 'fake'));
128
129
        /*
130
         * Test with One parameter int
131
         */
132
        $arg1 = 38;
133
        $oneParameter = [$arg1];
134
        $description1 = new FieldDescription();
135
        $description1->setOption('code', 'getWithOneParameter');
136
        $description1->setOption('parameters', $oneParameter);
137
138
        $mock1 = $this->getMockBuilder(\stdClass::class)
139
            ->setMethods(['getWithOneParameter'])
140
            ->getMock();
141
        $returnValue1 = $arg1 + 2;
142
        $mock1->expects($this->once())->method('getWithOneParameter')->with($this->equalTo($arg1))->willReturn($returnValue1);
143
144
        $this->assertSame(40, $description1->getFieldValue($mock1, 'fake'));
145
146
        /*
147
         * Test with Two parameters int
148
         */
149
        $arg2 = 4;
150
        $twoParameters = [$arg1, $arg2];
151
        $description2 = new FieldDescription();
152
        $description2->setOption('code', 'getWithTwoParameters');
153
        $description2->setOption('parameters', $twoParameters);
154
155
        $mock2 = $this->getMockBuilder(\stdClass::class)
156
            ->setMethods(['getWithTwoParameters'])
157
            ->getMock();
158
        $returnValue2 = $arg1 + $arg2;
159
        $mock2->method('getWithTwoParameters')->with($this->equalTo($arg1), $this->equalTo($arg2))->willReturn($returnValue2);
160
        $this->assertSame(42, $description2->getFieldValue($mock2, 'fake'));
161
162
        /*
163
         * Test with underscored attribute name
164
         */
165
        foreach (['getFake', 'isFake', 'hasFake'] as $method) {
166
            $description3 = new FieldDescription();
167
            $mock3 = $this->getMockBuilder(\stdClass::class)
168
                ->setMethods([$method])
169
                ->getMock();
170
171
            $mock3->expects($this->once())->method($method)->willReturn(42);
172
            $this->assertSame(42, $description3->getFieldValue($mock3, '_fake'));
173
        }
174
175
        $mock4 = $this->getMockBuilder('MockedTestObject')
176
            ->setMethods(['myMethod'])
177
            ->getMock();
178
        $mock4->expects($this->once())
179
            ->method('myMethod')
180
            ->willReturn('myMethodValue');
181
182
        $description4 = new FieldDescription();
183
        $description4->setOption('code', 'myMethod');
184
185
        $this->assertSame($description4->getFieldValue($mock4, null), 'myMethodValue');
186
    }
187
188
    public function testGetValueNoValueException(): void
189
    {
190
        $this->expectException(\Sonata\AdminBundle\Exception\NoValueException::class);
191
192
        $description = new FieldDescription();
193
        $mock = $this->getMockBuilder(\stdClass::class)
194
            ->setMethods(['getFoo'])
195
            ->getMock();
196
197
        $description->getFieldValue($mock, 'fake');
198
    }
199
200
    /**
201
     * @doesNotPerformAssertions
202
     */
203
    public function testGetVirtualValue(): void
204
    {
205
        $description = new FieldDescription();
206
        $mock = $this->getMockBuilder(\stdClass::class)
207
            ->setMethods(['getFoo'])
208
            ->getMock();
209
210
        $description->setOption('virtual_field', true);
211
        $description->getFieldValue($mock, 'fake');
212
    }
213
214
    public function testExceptionOnNonArrayOption(): void
215
    {
216
        $this->expectException(\RuntimeException::class);
217
218
        $description = new FieldDescription();
219
        $description->setOption('bar', 'hello');
220
        $description->mergeOption('bar', ['exception']);
221
    }
222
223
    public function testGetTranslationDomain(): void
224
    {
225
        $description = new FieldDescription();
226
227
        $admin = $this->createMock(AdminInterface::class);
228
        $description->setAdmin($admin);
229
230
        $admin->expects($this->once())
231
            ->method('getTranslationDomain')
232
            ->willReturn('AdminDomain');
233
234
        $this->assertSame('AdminDomain', $description->getTranslationDomain());
235
236
        $admin->expects($this->never())
237
            ->method('getTranslationDomain');
238
        $description->setOption('translation_domain', 'ExtensionDomain');
239
        $this->assertSame('ExtensionDomain', $description->getTranslationDomain());
240
    }
241
242
    /**
243
     * @group legacy
244
     */
245
    public function testCamelize(): void
246
    {
247
        $this->assertSame('FooBar', BaseFieldDescription::camelize('foo_bar'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...Description::camelize() has been deprecated with message: since sonata-project/admin-bundle 3.1. Use \Doctrine\Inflector\Inflector::classify() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
248
        $this->assertSame('FooBar', BaseFieldDescription::camelize('foo bar'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...Description::camelize() has been deprecated with message: since sonata-project/admin-bundle 3.1. Use \Doctrine\Inflector\Inflector::classify() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
249
        $this->assertSame('FOoBar', BaseFieldDescription::camelize('fOo bar'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...Description::camelize() has been deprecated with message: since sonata-project/admin-bundle 3.1. Use \Doctrine\Inflector\Inflector::classify() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
250
    }
251
252
    public function testGetInaccessibleValue(): void
253
    {
254
        $quux = 'quuX';
255
        $foo = new Foo();
256
        $foo->setQuux($quux);
257
        $ro = new \ReflectionObject($foo);
258
        $rm = $ro->getMethod('getQuux');
259
        $rm->setAccessible(true);
260
        $this->assertSame($quux, $rm->invokeArgs($foo, []));
261
262
        $description = new FieldDescription();
263
264
        $this->expectException(NoValueException::class);
265
        $description->getFieldValue($foo, 'quux');
266
    }
267
268
    public function testGetFieldValue(): void
269
    {
270
        $foo = new Foo();
271
        $foo->setBar('Bar');
272
273
        $description = new FieldDescription();
274
        $this->assertSame('Bar', $description->getFieldValue($foo, 'bar'));
275
        $foo->setBar('baR');
276
        $this->assertSame('baR', $description->getFieldValue($foo, 'bar'));
277
278
        $foo->qux = 'Qux';
279
        $this->assertSame('Qux', $description->getFieldValue($foo, 'qux'));
280
        $foo->qux = 'quX';
281
        $this->assertSame('quX', $description->getFieldValue($foo, 'qux'));
282
283
        $foo = new FooBoolean();
284
        $foo->setBar(true);
285
        $foo->setBaz(false);
286
287
        $description = new FieldDescription();
288
        $this->assertTrue($description->getFieldValue($foo, 'bar'));
289
        $this->assertFalse($description->getFieldValue($foo, 'baz'));
290
291
        $this->expectException(NoValueException::class);
292
        $description->getFieldValue($foo, 'inexistantMethod');
293
    }
294
295
    public function testGetFieldValueWithCodeOption(): void
296
    {
297
        $foo = new Foo();
298
        $foo->setBaz('Baz');
299
300
        $description = new FieldDescription();
301
302
        $description->setOption('code', 'getBaz');
303
        $this->assertSame('Baz', $description->getFieldValue($foo, 'inexistantMethod'));
304
305
        $description->setOption('code', 'inexistantMethod');
306
        $this->expectException(NoValueException::class);
307
        $description->getFieldValue($foo, 'inexistantMethod');
308
    }
309
310
    public function testGetFieldValueMagicCall(): void
311
    {
312
        $parameters = ['foo', 'bar'];
313
        $foo = new FooCall();
314
315
        $description = new FieldDescription();
316
        $description->setOption('parameters', $parameters);
317
        $this->assertSame(['inexistantMethod', $parameters], $description->getFieldValue($foo, 'inexistantMethod'));
318
319
        // repeating to cover retrieving cached getter
320
        $this->assertSame(['inexistantMethod', $parameters], $description->getFieldValue($foo, 'inexistantMethod'));
321
    }
322
323
    public function testGetFieldValueWithNullObject(): void
324
    {
325
        $foo = null;
326
        $description = new FieldDescription();
327
        $this->assertNull($description->getFieldValue($foo, 'bar'));
328
    }
329
}
330