Completed
Pull Request — master (#175)
by
unknown
13:09
created

testDescribesSingleValuedAssociationProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 1
eloc 10
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\DoctrineMongoDBAdminBundle\Tests\Admin;
13
14
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
15
use Sonata\DoctrineMongoDBAdminBundle\Admin\FieldDescription;
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
          'placeholder' => 'short_object_description_placeholder',
64
          'array' => array(
65
            'key1' => 'key_1',
66
            'key2' => 'key_2',
67
            'key3' => 'key_3',
68
          ),
69
          'non_existant' => array(
70
            'key1' => 'key_1',
71
            'key2' => 'key_2',
72
          ),
73
          'integer' => 1,
74
          'final' => 'test',
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('position', $field->getFieldName());
91
92
        // cannot overwrite defined definition
93
        $field->setAssociationMapping(array(
94
            'type' => 'overwrite?',
95
            'fieldName' => 'overwritten',
96
        ));
97
98
        $this->assertEquals('integer', $field->getType());
99
        $this->assertEquals('overwritten', $field->getFieldName());
100
101
        $field->setMappingType('string');
102
        $this->assertEquals('string', $field->getMappingType());
103
        $this->assertEquals('integer', $field->getType());
104
    }
105
106
    public function testCamelize()
107
    {
108
        $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...
109
        $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...
110
        $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...
111
    }
112
113
    public function testSetName()
114
    {
115
        $field = new FieldDescription();
116
        $field->setName('New field description name');
117
118
        $this->assertEquals($field->getName(), 'New field description name');
119
    }
120
121
    public function testSetNameSetFieldNameToo()
122
    {
123
        $field = new FieldDescription();
124
        $field->setName('New field description name');
125
126
        $this->assertEquals($field->getFieldName(), 'New field description name');
127
    }
128
129
    public function testSetNameDoesNotSetFieldNameWhenSetBefore()
130
    {
131
        $field = new FieldDescription();
132
        $field->setFieldName('field name');
133
        $field->setName('New field description name');
134
135
        $this->assertEquals($field->getFieldName(), 'field name');
136
    }
137
138
    public function testGetParent()
139
    {
140
        $adminMock = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
141
        $field = new FieldDescription();
142
        $field->setParent($adminMock);
143
144
        $this->assertSame($adminMock, $field->getParent());
145
    }
146
147
    public function testGetHelp()
148
    {
149
        $field = new FieldDescription();
150
        $field->setHelp('help message');
151
152
        $this->assertEquals($field->getHelp(), 'help message');
153
    }
154
155
    public function testGetAdmin()
156
    {
157
        $adminMock = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
158
        $field = new FieldDescription();
159
        $field->setAdmin($adminMock);
160
161
        $this->assertSame($adminMock, $field->getAdmin());
162
    }
163
164
    public function testGetAssociationAdmin()
165
    {
166
        $adminMock = $this->getMockBuilder('Sonata\AdminBundle\Admin\AbstractAdmin')
167
            ->disableOriginalConstructor()
168
            ->getMock();
169
        $adminMock->expects($this->once())
170
            ->method('setParentFieldDescription')
171
            ->with($this->isInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface'));
172
173
        $field = new FieldDescription();
174
        $field->setAssociationAdmin($adminMock);
175
176
        $this->assertSame($adminMock, $field->getAssociationAdmin());
177
    }
178
179
    public function testHasAssociationAdmin()
180
    {
181
        $adminMock = $this->getMockBuilder('Sonata\AdminBundle\Admin\AbstractAdmin')
182
            ->disableOriginalConstructor()
183
            ->getMock();
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()
198
    {
199
        $mockedObject = $this->getMock('MockedTestObject', array('myMethod'));
200
        $mockedObject->expects($this->once())
201
            ->method('myMethod')
202
            ->will($this->returnValue('myMethodValue'));
203
204
        $field = new FieldDescription();
205
        $field->setOption('code', 'myMethod');
206
207
        $this->assertEquals($field->getValue($mockedObject), 'myMethodValue');
208
    }
209
210
    /**
211
     * @expectedException Sonata\AdminBundle\Exception\NoValueException
212
     */
213
    public function testGetValueWhenCannotRetrieve()
214
    {
215
        $mockedObject = $this->getMock('MockedTestObject', array('myMethod'));
216
        $mockedObject->expects($this->never())
217
            ->method('myMethod')
218
            ->will($this->returnValue('myMethodValue'));
219
220
        $field = new FieldDescription();
221
222
        $this->assertEquals($field->getValue($mockedObject), 'myMethodValue');
223
    }
224
225
    public function testGetAssociationMapping()
226
    {
227
        $assocationMapping = array(
228
            'type' => 'integer',
229
            'fieldName' => 'position',
230
        );
231
232
        $field = new FieldDescription();
233
        $field->setAssociationMapping($assocationMapping);
234
235
        $this->assertEquals($assocationMapping, $field->getAssociationMapping());
236
    }
237
238
    /**
239
     * @expectedException \RuntimeException
240
     */
241
    public function testSetAssociationMappingAllowOnlyForArray()
242
    {
243
        $field = new FieldDescription();
244
        $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...
245
    }
246
247
    /**
248
     * @expectedException \RuntimeException
249
     */
250
    public function testSetFieldMappingAllowOnlyForArray()
251
    {
252
        $field = new FieldDescription();
253
        $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...
254
    }
255
256
    public function testSetFieldMappingSetType()
257
    {
258
        $fieldMapping = array(
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()
270
    {
271
        $fieldMapping = array(
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()
283
    {
284
        $fieldMapping = array(
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()
296
    {
297
        $assocationMapping = array(
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()
313
    {
314
        $fieldMapping = array(
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()
327
    {
328
        $fieldMapping = array(
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
    /**
341
     * @dataProvider testDescribesSingleValuedAssociationProvider
342
     *
343
     * @param mixed $mappingType
344
     * @param bool  $expected
345
     */
346
    public function testDescribesSingleValuedAssociation($mappingType, $expected)
347
    {
348
        $fd = new FieldDescription();
349
        $fd->setAssociationMapping(array(
350
            'fieldName' => 'foo',
351
            'type' => $mappingType,
352
        ));
353
        $this->assertSame($expected, $fd->describesSingleValuedAssociation());
354
    }
355
356
    public function testDescribesSingleValuedAssociationProvider()
357
    {
358
        return array(
359
            'one' => array(ClassMetadata::ONE, true),
360
            'embed one' => array(ClassMetadata::EMBED_ONE, true),
361
            'reference one' => array(ClassMetadata::REFERENCE_ONE, true),
362
            'many' => array(ClassMetadata::MANY, false),
363
            'embed many' => array(ClassMetadata::EMBED_MANY, false),
364
            'reference many' => array(ClassMetadata::REFERENCE_MANY, false),
365
            'string' => array('string', false),
366
            'null' => array(null, false),
367
        );
368
    }
369
370
    /**
371
     * @dataProvider testDescribesCollectionValuedAssociationProvider
372
     *
373
     * @param mixed $mappingType
374
     * @param bool  $expected
375
     */
376
    public function testDescribesCollectionValuedAssociation($mappingType, $expected)
377
    {
378
        $fd = new FieldDescription();
379
        $fd->setAssociationMapping(array(
380
            'fieldName' => 'foo',
381
            'type' => $mappingType,
382
        ));
383
        $this->assertSame($expected, $fd->describesCollectionValuedAssociation());
384
    }
385
386
    public function testDescribesCollectionValuedAssociationProvider()
387
    {
388
        return array(
389
            'one' => array(ClassMetadata::ONE, false),
390
            'embed one' => array(ClassMetadata::EMBED_ONE, false),
391
            'reference one' => array(ClassMetadata::REFERENCE_ONE, false),
392
            'many' => array(ClassMetadata::MANY, true),
393
            'embed many' => array(ClassMetadata::EMBED_MANY, true),
394
            'reference many' => array(ClassMetadata::REFERENCE_MANY, true),
395
            'string' => array('string', false),
396
            'null' => array(null, false),
397
        );
398
    }
399
}
400