Completed
Pull Request — 3.x (#677)
by
unknown
01:53
created

testSetAssociationMappingAllowOnlyForArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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