1 | <?php |
||
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() |
||
114 | |||
115 | public function testSetNameSetFieldNameToo() |
||
122 | |||
123 | public function testSetNameDoesNotSetFieldNameWhenSetBefore() |
||
131 | |||
132 | public function testGetParent() |
||
140 | |||
141 | public function testGetHelp() |
||
148 | |||
149 | public function testGetAdmin() |
||
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() |
||
231 | |||
232 | /** |
||
233 | * @expectedException \RuntimeException |
||
234 | */ |
||
235 | public function testSetAssociationMappingAllowOnlyForArray() |
||
240 | |||
241 | /** |
||
242 | * @expectedException \RuntimeException |
||
243 | */ |
||
244 | public function testSetFieldMappingAllowOnlyForArray() |
||
249 | |||
250 | public function testSetFieldMappingSetType() |
||
251 | { |
||
262 | |||
263 | public function testSetFieldMappingSetMappingType() |
||
275 | |||
276 | public function testSetFieldMappingSetFieldName() |
||
288 | |||
289 | public function testGetTargetEntity() |
||
305 | |||
306 | public function testIsIdentifierFromFieldMapping() |
||
319 | |||
320 | public function testGetFieldMapping() |
||
333 | |||
334 | public function testGetValueForEmbeddedObject() |
||
335 | { |
||
336 | $mockedEmbeddedObject = $this->getMockBuilder('stdClass') |
||
337 | ->setMethods(array('myMethod')) |
||
359 | } |
||
360 |