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
|
|
|
|
54
|
|
|
try { |
55
|
|
|
$field->mergeOption('integer', array()); |
56
|
|
|
$this->fail('no exception raised !!'); |
57
|
|
|
} catch (\RuntimeException $e) { |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$field->mergeOptions(array('final' => 'test')); |
61
|
|
|
|
62
|
|
|
$expected = array( |
63
|
|
|
'misc' => 'foobar', |
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
|
|
|
'placeholder' => 'short_object_description_placeholder', |
76
|
|
|
'link_parameters' => array(), |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$this->assertEquals($expected, $field->getOptions()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testAssociationMapping() |
83
|
|
|
{ |
84
|
|
|
$field = new FieldDescription(); |
85
|
|
|
$field->setAssociationMapping(array( |
86
|
|
|
'type' => 'integer', |
87
|
|
|
'fieldName' => 'position', |
88
|
|
|
)); |
89
|
|
|
|
90
|
|
|
$this->assertEquals('integer', $field->getType()); |
91
|
|
|
$this->assertEquals('integer', $field->getMappingType()); |
92
|
|
|
$this->assertEquals('position', $field->getFieldName()); |
93
|
|
|
|
94
|
|
|
// cannot overwrite defined definition |
95
|
|
|
$field->setAssociationMapping(array( |
96
|
|
|
'type' => 'overwrite?', |
97
|
|
|
'fieldName' => 'overwritten', |
98
|
|
|
)); |
99
|
|
|
|
100
|
|
|
$this->assertEquals('integer', $field->getType()); |
101
|
|
|
$this->assertEquals('integer', $field->getMappingType()); |
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 testSetName() |
110
|
|
|
{ |
111
|
|
|
$field = new FieldDescription(); |
112
|
|
|
$field->setName('New field description name'); |
113
|
|
|
|
114
|
|
|
$this->assertEquals($field->getName(), 'New field description name'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testSetNameSetFieldNameToo() |
118
|
|
|
{ |
119
|
|
|
$field = new FieldDescription(); |
120
|
|
|
$field->setName('New field description name'); |
121
|
|
|
|
122
|
|
|
$this->assertEquals($field->getFieldName(), 'New field description name'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testSetNameDoesNotSetFieldNameWhenSetBefore() |
126
|
|
|
{ |
127
|
|
|
$field = new FieldDescription(); |
128
|
|
|
$field->setFieldName('field name'); |
129
|
|
|
$field->setName('New field description name'); |
130
|
|
|
|
131
|
|
|
$this->assertEquals($field->getFieldName(), 'field name'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testGetParent() |
135
|
|
|
{ |
136
|
|
|
$adminMock = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface'); |
137
|
|
|
$field = new FieldDescription(); |
138
|
|
|
$field->setParent($adminMock); |
139
|
|
|
|
140
|
|
|
$this->assertSame($adminMock, $field->getParent()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testGetHelp() |
144
|
|
|
{ |
145
|
|
|
$field = new FieldDescription(); |
146
|
|
|
$field->setHelp('help message'); |
147
|
|
|
|
148
|
|
|
$this->assertEquals($field->getHelp(), 'help message'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testGetAdmin() |
152
|
|
|
{ |
153
|
|
|
$adminMock = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface'); |
154
|
|
|
$field = new FieldDescription(); |
155
|
|
|
$field->setAdmin($adminMock); |
156
|
|
|
|
157
|
|
|
$this->assertSame($adminMock, $field->getAdmin()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function testGetAssociationAdmin() |
161
|
|
|
{ |
162
|
|
|
$adminMock = $this->createMock('Sonata\AdminBundle\Admin\AbstractAdmin'); |
163
|
|
|
$adminMock->expects($this->once()) |
164
|
|
|
->method('setParentFieldDescription') |
165
|
|
|
->with($this->isInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface')); |
166
|
|
|
|
167
|
|
|
$field = new FieldDescription(); |
168
|
|
|
$field->setAssociationAdmin($adminMock); |
169
|
|
|
|
170
|
|
|
$this->assertSame($adminMock, $field->getAssociationAdmin()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testHasAssociationAdmin() |
174
|
|
|
{ |
175
|
|
|
$adminMock = $this->createMock('Sonata\AdminBundle\Admin\AbstractAdmin'); |
176
|
|
|
$adminMock->expects($this->once()) |
177
|
|
|
->method('setParentFieldDescription') |
178
|
|
|
->with($this->isInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface')); |
179
|
|
|
|
180
|
|
|
$field = new FieldDescription(); |
181
|
|
|
|
182
|
|
|
$this->assertFalse($field->hasAssociationAdmin()); |
183
|
|
|
|
184
|
|
|
$field->setAssociationAdmin($adminMock); |
185
|
|
|
|
186
|
|
|
$this->assertTrue($field->hasAssociationAdmin()); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function testGetValue() |
190
|
|
|
{ |
191
|
|
|
$mockedObject = $this->getMockBuilder('stdClass') |
192
|
|
|
->setMethods(array('myMethod')) |
193
|
|
|
->getMock(); |
194
|
|
|
$mockedObject->expects($this->once()) |
195
|
|
|
->method('myMethod') |
196
|
|
|
->will($this->returnValue('myMethodValue')); |
197
|
|
|
|
198
|
|
|
$field = new FieldDescription(); |
199
|
|
|
$field->setOption('code', 'myMethod'); |
200
|
|
|
|
201
|
|
|
$this->assertEquals($field->getValue($mockedObject), 'myMethodValue'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @expectedException \Sonata\AdminBundle\Exception\NoValueException |
206
|
|
|
*/ |
207
|
|
|
public function testGetValueWhenCannotRetrieve() |
208
|
|
|
{ |
209
|
|
|
$mockedObject = $this->getMockBuilder('stdClass') |
210
|
|
|
->setMethods(array('myMethod')) |
211
|
|
|
->getMock(); |
212
|
|
|
$mockedObject->expects($this->never()) |
213
|
|
|
->method('myMethod') |
214
|
|
|
->will($this->returnValue('myMethodValue')); |
215
|
|
|
|
216
|
|
|
$field = new FieldDescription(); |
217
|
|
|
|
218
|
|
|
$this->assertEquals($field->getValue($mockedObject), 'myMethodValue'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testGetAssociationMapping() |
222
|
|
|
{ |
223
|
|
|
$assocationMapping = array( |
224
|
|
|
'type' => 'integer', |
225
|
|
|
'fieldName' => 'position', |
226
|
|
|
); |
227
|
|
|
|
228
|
|
|
$field = new FieldDescription(); |
229
|
|
|
$field->setAssociationMapping($assocationMapping); |
230
|
|
|
|
231
|
|
|
$this->assertEquals($assocationMapping, $field->getAssociationMapping()); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @expectedException \RuntimeException |
236
|
|
|
*/ |
237
|
|
|
public function testSetAssociationMappingAllowOnlyForArray() |
238
|
|
|
{ |
239
|
|
|
$field = new FieldDescription(); |
240
|
|
|
$field->setAssociationMapping('test'); |
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @expectedException \RuntimeException |
245
|
|
|
*/ |
246
|
|
|
public function testSetFieldMappingAllowOnlyForArray() |
247
|
|
|
{ |
248
|
|
|
$field = new FieldDescription(); |
249
|
|
|
$field->setFieldMapping('test'); |
|
|
|
|
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function testSetFieldMappingSetType() |
253
|
|
|
{ |
254
|
|
|
$fieldMapping = array( |
255
|
|
|
'type' => 'integer', |
256
|
|
|
'fieldName' => 'position', |
257
|
|
|
); |
258
|
|
|
|
259
|
|
|
$field = new FieldDescription(); |
260
|
|
|
$field->setFieldMapping($fieldMapping); |
261
|
|
|
|
262
|
|
|
$this->assertEquals('integer', $field->getType()); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function testSetFieldMappingSetMappingType() |
266
|
|
|
{ |
267
|
|
|
$fieldMapping = array( |
268
|
|
|
'type' => 'integer', |
269
|
|
|
'fieldName' => 'position', |
270
|
|
|
); |
271
|
|
|
|
272
|
|
|
$field = new FieldDescription(); |
273
|
|
|
$field->setFieldMapping($fieldMapping); |
274
|
|
|
|
275
|
|
|
$this->assertEquals('integer', $field->getMappingType()); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function testSetFieldMappingSetFieldName() |
279
|
|
|
{ |
280
|
|
|
$fieldMapping = array( |
281
|
|
|
'type' => 'integer', |
282
|
|
|
'fieldName' => 'position', |
283
|
|
|
); |
284
|
|
|
|
285
|
|
|
$field = new FieldDescription(); |
286
|
|
|
$field->setFieldMapping($fieldMapping); |
287
|
|
|
|
288
|
|
|
$this->assertEquals('position', $field->getFieldName()); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
public function testGetTargetEntity() |
292
|
|
|
{ |
293
|
|
|
$assocationMapping = array( |
294
|
|
|
'type' => 'integer', |
295
|
|
|
'fieldName' => 'position', |
296
|
|
|
'targetEntity' => 'someValue', |
297
|
|
|
); |
298
|
|
|
|
299
|
|
|
$field = new FieldDescription(); |
300
|
|
|
|
301
|
|
|
$this->assertNull($field->getTargetEntity()); |
302
|
|
|
|
303
|
|
|
$field->setAssociationMapping($assocationMapping); |
304
|
|
|
|
305
|
|
|
$this->assertEquals('someValue', $field->getTargetEntity()); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function testIsIdentifierFromFieldMapping() |
309
|
|
|
{ |
310
|
|
|
$fieldMapping = array( |
311
|
|
|
'type' => 'integer', |
312
|
|
|
'fieldName' => 'position', |
313
|
|
|
'id' => 'someId', |
314
|
|
|
); |
315
|
|
|
|
316
|
|
|
$field = new FieldDescription(); |
317
|
|
|
$field->setFieldMapping($fieldMapping); |
318
|
|
|
|
319
|
|
|
$this->assertEquals('someId', $field->isIdentifier()); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function testGetFieldMapping() |
323
|
|
|
{ |
324
|
|
|
$fieldMapping = array( |
325
|
|
|
'type' => 'integer', |
326
|
|
|
'fieldName' => 'position', |
327
|
|
|
'id' => 'someId', |
328
|
|
|
); |
329
|
|
|
|
330
|
|
|
$field = new FieldDescription(); |
331
|
|
|
$field->setFieldMapping($fieldMapping); |
332
|
|
|
|
333
|
|
|
$this->assertEquals($fieldMapping, $field->getFieldMapping()); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
public function testGetValueForEmbeddedObject() |
337
|
|
|
{ |
338
|
|
|
$mockedEmbeddedObject = $this->getMockBuilder('stdClass') |
339
|
|
|
->setMethods(array('myMethod')) |
340
|
|
|
->getMock(); |
341
|
|
|
$mockedEmbeddedObject->expects($this->once()) |
342
|
|
|
->method('myMethod') |
343
|
|
|
->will($this->returnValue('myMethodValue')); |
344
|
|
|
|
345
|
|
|
$mockedObject = $this->getMockBuilder('stdClass') |
346
|
|
|
->setMethods(array('getMyEmbeddedObject')) |
347
|
|
|
->getMock(); |
348
|
|
|
$mockedObject->expects($this->once()) |
349
|
|
|
->method('getMyEmbeddedObject') |
350
|
|
|
->will($this->returnValue($mockedEmbeddedObject)); |
351
|
|
|
|
352
|
|
|
$field = new FieldDescription(); |
353
|
|
|
$field->setFieldMapping(array( |
354
|
|
|
'declaredField' => 'myEmbeddedObject', 'type' => 'string', 'fieldName' => 'myEmbeddedObject.myMethod', |
355
|
|
|
)); |
356
|
|
|
$field->setFieldName('myMethod'); |
357
|
|
|
$field->setOption('code', 'myMethod'); |
358
|
|
|
|
359
|
|
|
$this->assertEquals('myMethodValue', $field->getValue($mockedObject)); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
public function testGetValueForMultiLevelEmbeddedObject() |
363
|
|
|
{ |
364
|
|
|
$mockedChildEmbeddedObject = $this->getMockBuilder('stdClass') |
365
|
|
|
->setMethods(array('myMethod')) |
366
|
|
|
->getMock(); |
367
|
|
|
$mockedChildEmbeddedObject->expects($this->once()) |
368
|
|
|
->method('myMethod') |
369
|
|
|
->will($this->returnValue('myMethodValue')); |
370
|
|
|
$mockedEmbeddedObject = $this->getMockBuilder('stdClass') |
371
|
|
|
->setMethods(array('getChild')) |
372
|
|
|
->getMock(); |
373
|
|
|
$mockedEmbeddedObject->expects($this->once()) |
374
|
|
|
->method('getChild') |
375
|
|
|
->will($this->returnValue($mockedChildEmbeddedObject)); |
376
|
|
|
$mockedObject = $this->getMockBuilder('stdClass') |
377
|
|
|
->setMethods(array('getMyEmbeddedObject')) |
378
|
|
|
->getMock(); |
379
|
|
|
$mockedObject->expects($this->once()) |
380
|
|
|
->method('getMyEmbeddedObject') |
381
|
|
|
->will($this->returnValue($mockedEmbeddedObject)); |
382
|
|
|
$field = new FieldDescription(); |
383
|
|
|
$field->setFieldMapping(array( |
384
|
|
|
'declaredField' => 'myEmbeddedObject.child', 'type' => 'string', 'fieldName' => 'myMethod', |
385
|
|
|
)); |
386
|
|
|
$field->setFieldName('myMethod'); |
387
|
|
|
$field->setOption('code', 'myMethod'); |
388
|
|
|
$this->assertEquals('myMethodValue', $field->getValue($mockedObject)); |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
|