1 | <?php |
||
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) { |
||
|
|||
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() |
||
115 | |||
116 | public function testSetNameSetFieldNameToo() |
||
123 | |||
124 | public function testSetNameDoesNotSetFieldNameWhenSetBefore() |
||
132 | |||
133 | public function testGetParent() |
||
141 | |||
142 | public function testGetHelp() |
||
149 | |||
150 | public function testGetAdmin() |
||
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() |
||
232 | |||
233 | /** |
||
234 | * @expectedException \RuntimeException |
||
235 | */ |
||
236 | public function testSetAssociationMappingAllowOnlyForArray() |
||
241 | |||
242 | /** |
||
243 | * @expectedException \RuntimeException |
||
244 | */ |
||
245 | public function testSetFieldMappingAllowOnlyForArray() |
||
250 | |||
251 | public function testSetFieldMappingSetType() |
||
252 | { |
||
253 | $fieldMapping = array( |
||
254 | 'type' => 'integer', |
||
255 | 'fieldName' => 'position', |
||
256 | ); |
||
263 | |||
264 | public function testSetFieldMappingSetMappingType() |
||
276 | |||
277 | public function testSetFieldMappingSetFieldName() |
||
289 | |||
290 | public function testGetTargetEntity() |
||
306 | |||
307 | public function testIsIdentifierFromFieldMapping() |
||
320 | |||
321 | public function testGetFieldMapping() |
||
334 | |||
335 | public function testGetValueForEmbeddedObject() |
||
360 | } |
||
361 |