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