FieldDescriptionTest   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 321
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 23
lcom 0
cbo 2
dl 0
loc 321
rs 10
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
B testOptions() 0 62 2
A testAssociationMapping() 0 24 1
A testCamelize() 0 6 1
A testSetName() 0 7 1
A testSetNameSetFieldNameToo() 0 7 1
A testSetNameDoesNotSetFieldNameWhenSetBefore() 0 8 1
A testGetParent() 0 8 1
A testGetHelp() 0 7 1
A testGetAdmin() 0 8 1
A testGetAssociationAdmin() 0 12 1
A testHasAssociationAdmin() 0 15 1
A testGetValue() 0 14 1
A testGetValueWhenCannotRetrieve() 0 15 1
A testGetAssociationMapping() 0 12 1
A testSetAssociationMappingAllowOnlyForArray() 0 7 1
A testSetFieldMappingAllowOnlyForArray() 0 7 1
A testSetFieldMappingSetType() 0 12 1
A testSetFieldMappingSetMappingType() 0 12 1
A testSetFieldMappingSetFieldName() 0 12 1
A testGetTargetEntity() 0 16 1
A testIsIdentifierFromFieldMapping() 0 13 1
A testGetFieldMapping() 0 13 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\DoctrineMongoDBAdminBundle\Tests\Admin;
15
16
use Doctrine\Common\Inflector\Inflector;
17
use PHPUnit\Framework\TestCase;
18
use Sonata\AdminBundle\Admin\AbstractAdmin;
19
use Sonata\AdminBundle\Admin\AdminInterface;
20
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
21
use Sonata\DoctrineMongoDBAdminBundle\Admin\FieldDescription;
22
23
class FieldDescriptionTest extends TestCase
24
{
25
    public function testOptions(): void
26
    {
27
        $field = new FieldDescription();
28
        $field->setOptions([
29
            'template' => 'foo',
30
            'type' => 'bar',
31
            'misc' => 'foobar',
32
        ]);
33
34
        // test method shortcut
35
        $this->assertNull($field->getOption('template'));
36
        $this->assertNull($field->getOption('type'));
37
38
        $this->assertSame('foo', $field->getTemplate());
39
        $this->assertSame('bar', $field->getType());
40
41
        // test the default value option
42
        $this->assertSame('default', $field->getOption('template', 'default'));
43
44
        // test the merge options
45
        $field->setOption('array', ['key1' => 'val1']);
46
        $field->mergeOption('array', ['key1' => 'key_1', 'key2' => 'key_2']);
47
48
        $this->assertSame(['key1' => 'key_1', 'key2' => 'key_2'], $field->getOption('array'));
49
50
        $field->mergeOption('non_existant', ['key1' => 'key_1', 'key2' => 'key_2']);
51
52
        $this->assertSame(['key1' => 'key_1', 'key2' => 'key_2'], $field->getOption('array'));
53
54
        $field->mergeOptions(['array' => ['key3' => 'key_3']]);
55
56
        $this->assertSame(['key1' => 'key_1', 'key2' => 'key_2', 'key3' => 'key_3'], $field->getOption('array'));
57
58
        $field->setOption('integer', 1);
59
60
        try {
61
            $field->mergeOption('integer', []);
62
            $this->fail('no exception raised !!');
63
        } catch (\RuntimeException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by Kévin Dunglas
Consider adding a comment why this CATCH block is empty.
Loading history...
64
        }
65
66
        $field->mergeOptions(['final' => 'test']);
67
68
        $expected = [
69
          'misc' => 'foobar',
70
          'placeholder' => 'short_object_description_placeholder',
71
          'link_parameters' => [],
72
          'array' => [
73
            'key1' => 'key_1',
74
            'key2' => 'key_2',
75
            'key3' => 'key_3',
76
          ],
77
          'non_existant' => [
78
            'key1' => 'key_1',
79
            'key2' => 'key_2',
80
          ],
81
          'integer' => 1,
82
          'final' => 'test',
83
        ];
84
85
        $this->assertSame($expected, $field->getOptions());
86
    }
87
88
    public function testAssociationMapping(): void
89
    {
90
        $field = new FieldDescription();
91
        $field->setAssociationMapping([
92
            'type' => 'integer',
93
            'fieldName' => 'position',
94
        ]);
95
96
        $this->assertSame('integer', $field->getType());
97
        $this->assertSame('position', $field->getFieldName());
98
99
        // cannot overwrite defined definition
100
        $field->setAssociationMapping([
101
            'type' => 'overwrite?',
102
            'fieldName' => 'overwritten',
103
        ]);
104
105
        $this->assertSame('integer', $field->getType());
106
        $this->assertSame('overwritten', $field->getFieldName());
107
108
        $field->setMappingType('string');
109
        $this->assertSame('string', $field->getMappingType());
110
        $this->assertSame('integer', $field->getType());
111
    }
112
113
    public function testCamelize(): void
114
    {
115
        $this->assertSame('FooBar', Inflector::classify('foo_bar'));
116
        $this->assertSame('FooBar', Inflector::classify('foo bar'));
117
        $this->assertSame('FOoBar', Inflector::classify('fOo bar'));
118
    }
119
120
    public function testSetName(): void
121
    {
122
        $field = new FieldDescription();
123
        $field->setName('New field description name');
124
125
        $this->assertSame($field->getName(), 'New field description name');
126
    }
127
128
    public function testSetNameSetFieldNameToo(): void
129
    {
130
        $field = new FieldDescription();
131
        $field->setName('New field description name');
132
133
        $this->assertSame($field->getFieldName(), 'New field description name');
134
    }
135
136
    public function testSetNameDoesNotSetFieldNameWhenSetBefore(): void
137
    {
138
        $field = new FieldDescription();
139
        $field->setFieldName('field name');
140
        $field->setName('New field description name');
141
142
        $this->assertSame($field->getFieldName(), 'field name');
143
    }
144
145
    public function testGetParent(): void
146
    {
147
        $adminMock = $this->createMock(AdminInterface::class);
148
        $field = new FieldDescription();
149
        $field->setParent($adminMock);
150
151
        $this->assertSame($adminMock, $field->getParent());
152
    }
153
154
    public function testGetHelp(): void
155
    {
156
        $field = new FieldDescription();
157
        $field->setHelp('help message');
158
159
        $this->assertSame($field->getHelp(), 'help message');
160
    }
161
162
    public function testGetAdmin(): void
163
    {
164
        $adminMock = $this->createMock(AdminInterface::class);
165
        $field = new FieldDescription();
166
        $field->setAdmin($adminMock);
167
168
        $this->assertSame($adminMock, $field->getAdmin());
169
    }
170
171
    public function testGetAssociationAdmin(): void
172
    {
173
        $adminMock = $this->createMock(AbstractAdmin::class);
174
        $adminMock->expects($this->once())
175
            ->method('setParentFieldDescription')
176
            ->with($this->isInstanceOf(FieldDescriptionInterface::class));
177
178
        $field = new FieldDescription();
179
        $field->setAssociationAdmin($adminMock);
180
181
        $this->assertSame($adminMock, $field->getAssociationAdmin());
182
    }
183
184
    public function testHasAssociationAdmin(): void
185
    {
186
        $adminMock = $this->createMock(AbstractAdmin::class);
187
        $adminMock->expects($this->once())
188
            ->method('setParentFieldDescription')
189
            ->with($this->isInstanceOf(FieldDescriptionInterface::class));
190
191
        $field = new FieldDescription();
192
193
        $this->assertFalse($field->hasAssociationAdmin());
194
195
        $field->setAssociationAdmin($adminMock);
196
197
        $this->assertTrue($field->hasAssociationAdmin());
198
    }
199
200
    public function testGetValue(): void
201
    {
202
        $mockedObject = $this->getMockBuilder('MockedTestObject')
203
            ->setMethods(['myMethod'])
204
            ->getMock();
205
        $mockedObject->expects($this->once())
206
            ->method('myMethod')
207
            ->willReturn('myMethodValue');
208
209
        $field = new FieldDescription();
210
        $field->setOption('code', 'myMethod');
211
212
        $this->assertSame($field->getValue($mockedObject), 'myMethodValue');
213
    }
214
215
    public function testGetValueWhenCannotRetrieve(): void
216
    {
217
        $this->expectException(\Sonata\AdminBundle\Exception\NoValueException::class);
218
219
        $mockedObject = $this->getMockBuilder('MockedTestObject')
220
            ->setMethods(['myMethod'])
221
            ->getMock();
222
        $mockedObject->expects($this->never())
223
            ->method('myMethod')
224
            ->willReturn('myMethodValue');
225
226
        $field = new FieldDescription();
227
228
        $this->assertSame($field->getValue($mockedObject), 'myMethodValue');
229
    }
230
231
    public function testGetAssociationMapping(): void
232
    {
233
        $assocationMapping = [
234
            'type' => 'integer',
235
            'fieldName' => 'position',
236
        ];
237
238
        $field = new FieldDescription();
239
        $field->setAssociationMapping($assocationMapping);
240
241
        $this->assertSame($assocationMapping, $field->getAssociationMapping());
242
    }
243
244
    public function testSetAssociationMappingAllowOnlyForArray(): void
245
    {
246
        $this->expectException(\RuntimeException::class);
247
248
        $field = new FieldDescription();
249
        $field->setAssociationMapping('test');
0 ignored issues
show
Documentation introduced by Kévin Dunglas
'test' is of type string, but the function expects a array.

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...
250
    }
251
252
    public function testSetFieldMappingAllowOnlyForArray(): void
253
    {
254
        $this->expectException(\RuntimeException::class);
255
256
        $field = new FieldDescription();
257
        $field->setFieldMapping('test');
0 ignored issues
show
Documentation introduced by Kévin Dunglas
'test' is of type string, but the function expects a array.

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...
258
    }
259
260
    public function testSetFieldMappingSetType(): void
261
    {
262
        $fieldMapping = [
263
            'type' => 'integer',
264
            'fieldName' => 'position',
265
        ];
266
267
        $field = new FieldDescription();
268
        $field->setFieldMapping($fieldMapping);
269
270
        $this->assertSame('integer', $field->getType());
271
    }
272
273
    public function testSetFieldMappingSetMappingType(): void
274
    {
275
        $fieldMapping = [
276
            'type' => 'integer',
277
            'fieldName' => 'position',
278
        ];
279
280
        $field = new FieldDescription();
281
        $field->setFieldMapping($fieldMapping);
282
283
        $this->assertSame('integer', $field->getMappingType());
284
    }
285
286
    public function testSetFieldMappingSetFieldName(): void
287
    {
288
        $fieldMapping = [
289
            'type' => 'integer',
290
            'fieldName' => 'position',
291
        ];
292
293
        $field = new FieldDescription();
294
        $field->setFieldMapping($fieldMapping);
295
296
        $this->assertSame('position', $field->getFieldName());
297
    }
298
299
    public function testGetTargetEntity(): void
300
    {
301
        $assocationMapping = [
302
            'type' => 'integer',
303
            'fieldName' => 'position',
304
            'targetDocument' => 'someValue',
305
        ];
306
307
        $field = new FieldDescription();
308
309
        $this->assertNull($field->getTargetEntity());
310
311
        $field->setAssociationMapping($assocationMapping);
312
313
        $this->assertSame('someValue', $field->getTargetEntity());
314
    }
315
316
    public function testIsIdentifierFromFieldMapping(): void
317
    {
318
        $fieldMapping = [
319
            'type' => 'integer',
320
            'fieldName' => 'position',
321
            'id' => 'someId',
322
        ];
323
324
        $field = new FieldDescription();
325
        $field->setFieldMapping($fieldMapping);
326
327
        $this->assertSame('someId', $field->isIdentifier());
328
    }
329
330
    public function testGetFieldMapping(): void
331
    {
332
        $fieldMapping = [
333
            'type' => 'integer',
334
            'fieldName' => 'position',
335
            'id' => 'someId',
336
        ];
337
338
        $field = new FieldDescription();
339
        $field->setFieldMapping($fieldMapping);
340
341
        $this->assertSame($fieldMapping, $field->getFieldMapping());
342
    }
343
}
344