Completed
Push — master ( 3b4b20...e80e3c )
by
unknown
12:23
created

tests/Admin/FieldDescriptionTest.php (2 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\DoctrineMongoDBAdminBundle\Tests\Admin;
15
16
use Doctrine\Common\Util\Inflector;
17
use PHPUnit\Framework\TestCase;
18
use Sonata\DoctrineMongoDBAdminBundle\Admin\FieldDescription;
19
20
class FieldDescriptionTest extends TestCase
21
{
22
    public function testOptions(): void
23
    {
24
        $field = new FieldDescription();
25
        $field->setOptions([
26
            'template' => 'foo',
27
            'type' => 'bar',
28
            'misc' => 'foobar',
29
        ]);
30
31
        // test method shortcut
32
        $this->assertNull($field->getOption('template'));
33
        $this->assertNull($field->getOption('type'));
34
35
        $this->assertEquals('foo', $field->getTemplate());
36
        $this->assertEquals('bar', $field->getType());
37
38
        // test the default value option
39
        $this->assertEquals('default', $field->getOption('template', 'default'));
40
41
        // test the merge options
42
        $field->setOption('array', ['key1' => 'val1']);
43
        $field->mergeOption('array', ['key1' => 'key_1', 'key2' => 'key_2']);
44
45
        $this->assertEquals(['key1' => 'key_1', 'key2' => 'key_2'], $field->getOption('array'));
46
47
        $field->mergeOption('non_existant', ['key1' => 'key_1', 'key2' => 'key_2']);
48
49
        $this->assertEquals(['key1' => 'key_1', 'key2' => 'key_2'], $field->getOption('array'));
50
51
        $field->mergeOptions(['array' => ['key3' => 'key_3']]);
52
53
        $this->assertEquals(['key1' => 'key_1', 'key2' => 'key_2', 'key3' => 'key_3'], $field->getOption('array'));
54
55
        $field->setOption('integer', 1);
56
57
        try {
58
            $field->mergeOption('integer', []);
59
            $this->fail('no exception raised !!');
60
        } catch (\RuntimeException $e) {
61
        }
62
63
        $field->mergeOptions(['final' => 'test']);
64
65
        $expected = [
66
          'misc' => 'foobar',
67
          'placeholder' => 'short_object_description_placeholder',
68
          'array' => [
69
            'key1' => 'key_1',
70
            'key2' => 'key_2',
71
            'key3' => 'key_3',
72
          ],
73
          'non_existant' => [
74
            'key1' => 'key_1',
75
            'key2' => 'key_2',
76
          ],
77
          'integer' => 1,
78
          'final' => 'test',
79
          'link_parameters' => [],
80
        ];
81
82
        $this->assertEquals($expected, $field->getOptions());
83
    }
84
85
    public function testAssociationMapping(): void
86
    {
87
        $field = new FieldDescription();
88
        $field->setAssociationMapping([
89
            'type' => 'integer',
90
            'fieldName' => 'position',
91
        ]);
92
93
        $this->assertEquals('integer', $field->getType());
94
        $this->assertEquals('position', $field->getFieldName());
95
96
        // cannot overwrite defined definition
97
        $field->setAssociationMapping([
98
            'type' => 'overwrite?',
99
            'fieldName' => 'overwritten',
100
        ]);
101
102
        $this->assertEquals('integer', $field->getType());
103
        $this->assertEquals('overwritten', $field->getFieldName());
104
105
        $field->setMappingType('string');
106
        $this->assertEquals('string', $field->getMappingType());
107
        $this->assertEquals('integer', $field->getType());
108
    }
109
110
    public function testCamelize(): void
111
    {
112
        $this->assertEquals('FooBar', Inflector::classify('foo_bar'));
113
        $this->assertEquals('FooBar', Inflector::classify('foo bar'));
114
        $this->assertEquals('FOoBar', Inflector::classify('fOo bar'));
115
    }
116
117
    public function testSetName(): void
118
    {
119
        $field = new FieldDescription();
120
        $field->setName('New field description name');
121
122
        $this->assertEquals($field->getName(), 'New field description name');
123
    }
124
125
    public function testSetNameSetFieldNameToo(): void
126
    {
127
        $field = new FieldDescription();
128
        $field->setName('New field description name');
129
130
        $this->assertEquals($field->getFieldName(), 'New field description name');
131
    }
132
133
    public function testSetNameDoesNotSetFieldNameWhenSetBefore(): void
134
    {
135
        $field = new FieldDescription();
136
        $field->setFieldName('field name');
137
        $field->setName('New field description name');
138
139
        $this->assertEquals($field->getFieldName(), 'field name');
140
    }
141
142
    public function testGetParent(): void
143
    {
144
        $adminMock = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
145
        $field = new FieldDescription();
146
        $field->setParent($adminMock);
147
148
        $this->assertSame($adminMock, $field->getParent());
149
    }
150
151
    public function testGetHelp(): void
152
    {
153
        $field = new FieldDescription();
154
        $field->setHelp('help message');
155
156
        $this->assertEquals($field->getHelp(), 'help message');
157
    }
158
159
    public function testGetAdmin(): void
160
    {
161
        $adminMock = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
162
        $field = new FieldDescription();
163
        $field->setAdmin($adminMock);
164
165
        $this->assertSame($adminMock, $field->getAdmin());
166
    }
167
168
    public function testGetAssociationAdmin(): void
169
    {
170
        $adminMock = $this->createMock('Sonata\AdminBundle\Admin\AbstractAdmin');
171
        $adminMock->expects($this->once())
172
            ->method('setParentFieldDescription')
173
            ->with($this->isInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface'));
174
175
        $field = new FieldDescription();
176
        $field->setAssociationAdmin($adminMock);
177
178
        $this->assertSame($adminMock, $field->getAssociationAdmin());
179
    }
180
181
    public function testHasAssociationAdmin(): void
182
    {
183
        $adminMock = $this->createMock('Sonata\AdminBundle\Admin\AbstractAdmin');
184
        $adminMock->expects($this->once())
185
            ->method('setParentFieldDescription')
186
            ->with($this->isInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface'));
187
188
        $field = new FieldDescription();
189
190
        $this->assertFalse($field->hasAssociationAdmin());
191
192
        $field->setAssociationAdmin($adminMock);
193
194
        $this->assertTrue($field->hasAssociationAdmin());
195
    }
196
197
    public function testGetValue(): void
198
    {
199
        $mockedObject = $this->getMockBuilder('MockedTestObject')
200
            ->setMethods(['myMethod'])
201
            ->getMock();
202
        $mockedObject->expects($this->once())
203
            ->method('myMethod')
204
            ->will($this->returnValue('myMethodValue'));
205
206
        $field = new FieldDescription();
207
        $field->setOption('code', 'myMethod');
208
209
        $this->assertEquals($field->getValue($mockedObject), 'myMethodValue');
210
    }
211
212
    public function testGetValueWhenCannotRetrieve(): void
213
    {
214
        $this->expectException(\Sonata\AdminBundle\Exception\NoValueException::class);
215
216
        $mockedObject = $this->getMockBuilder('MockedTestObject')
217
            ->setMethods(['myMethod'])
218
            ->getMock();
219
        $mockedObject->expects($this->never())
220
            ->method('myMethod')
221
            ->will($this->returnValue('myMethodValue'));
222
223
        $field = new FieldDescription();
224
225
        $this->assertEquals($field->getValue($mockedObject), 'myMethodValue');
226
    }
227
228
    public function testGetAssociationMapping(): void
229
    {
230
        $assocationMapping = [
231
            'type' => 'integer',
232
            'fieldName' => 'position',
233
        ];
234
235
        $field = new FieldDescription();
236
        $field->setAssociationMapping($assocationMapping);
237
238
        $this->assertEquals($assocationMapping, $field->getAssociationMapping());
239
    }
240
241
    public function testSetAssociationMappingAllowOnlyForArray(): void
242
    {
243
        $this->expectException(\RuntimeException::class);
244
245
        $field = new FieldDescription();
246
        $field->setAssociationMapping('test');
0 ignored issues
show
'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...
247
    }
248
249
    public function testSetFieldMappingAllowOnlyForArray(): void
250
    {
251
        $this->expectException(\RuntimeException::class);
252
253
        $field = new FieldDescription();
254
        $field->setFieldMapping('test');
0 ignored issues
show
'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...
255
    }
256
257
    public function testSetFieldMappingSetType(): void
258
    {
259
        $fieldMapping = [
260
            'type' => 'integer',
261
            'fieldName' => 'position',
262
        ];
263
264
        $field = new FieldDescription();
265
        $field->setFieldMapping($fieldMapping);
266
267
        $this->assertEquals('integer', $field->getType());
268
    }
269
270
    public function testSetFieldMappingSetMappingType(): void
271
    {
272
        $fieldMapping = [
273
            'type' => 'integer',
274
            'fieldName' => 'position',
275
        ];
276
277
        $field = new FieldDescription();
278
        $field->setFieldMapping($fieldMapping);
279
280
        $this->assertEquals('integer', $field->getMappingType());
281
    }
282
283
    public function testSetFieldMappingSetFieldName(): void
284
    {
285
        $fieldMapping = [
286
            'type' => 'integer',
287
            'fieldName' => 'position',
288
        ];
289
290
        $field = new FieldDescription();
291
        $field->setFieldMapping($fieldMapping);
292
293
        $this->assertEquals('position', $field->getFieldName());
294
    }
295
296
    public function testGetTargetEntity(): void
297
    {
298
        $assocationMapping = [
299
            'type' => 'integer',
300
            'fieldName' => 'position',
301
            'targetDocument' => 'someValue',
302
        ];
303
304
        $field = new FieldDescription();
305
306
        $this->assertNull($field->getTargetEntity());
307
308
        $field->setAssociationMapping($assocationMapping);
309
310
        $this->assertEquals('someValue', $field->getTargetEntity());
311
    }
312
313
    public function testIsIdentifierFromFieldMapping(): void
314
    {
315
        $fieldMapping = [
316
            'type' => 'integer',
317
            'fieldName' => 'position',
318
            'id' => 'someId',
319
        ];
320
321
        $field = new FieldDescription();
322
        $field->setFieldMapping($fieldMapping);
323
324
        $this->assertEquals('someId', $field->isIdentifier());
325
    }
326
327
    public function testGetFieldMapping(): void
328
    {
329
        $fieldMapping = [
330
            'type' => 'integer',
331
            'fieldName' => 'position',
332
            'id' => 'someId',
333
        ];
334
335
        $field = new FieldDescription();
336
        $field->setFieldMapping($fieldMapping);
337
338
        $this->assertEquals($fieldMapping, $field->getFieldMapping());
339
    }
340
}
341