Completed
Push — master ( 938943...6205c9 )
by Jordi Sala
14:49 queued 11:43
created

BaseFieldDescriptionTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 295
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 4
dl 0
loc 295
rs 10
c 0
b 0
f 0

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