Completed
Push — master ( 369610...186bbc )
by
unknown
04:30 queued 11s
created

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