1 | <?php |
||
16 | class FieldDescriptionTest extends \PHPUnit_Framework_TestCase |
||
17 | { |
||
18 | public function testOptions() |
||
79 | |||
80 | public function testAssociationMapping() |
||
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->getMock('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->getMock('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->getMockBuilder('Sonata\AdminBundle\Admin\Admin') |
||
161 | ->disableOriginalConstructor() |
||
162 | ->getMock(); |
||
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->getMockBuilder('Sonata\AdminBundle\Admin\Admin') |
||
176 | ->disableOriginalConstructor() |
||
177 | ->getMock(); |
||
178 | $adminMock->expects($this->once()) |
||
179 | ->method('setParentFieldDescription') |
||
180 | ->with($this->isInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface')); |
||
181 | |||
182 | $field = new FieldDescription(); |
||
183 | |||
184 | $this->assertFalse($field->hasAssociationAdmin()); |
||
185 | |||
186 | $field->setAssociationAdmin($adminMock); |
||
187 | |||
188 | $this->assertTrue($field->hasAssociationAdmin()); |
||
189 | } |
||
190 | |||
191 | public function testGetValue() |
||
192 | { |
||
193 | $mockedObject = $this->getMock('MockedTestObject', array('myMethod')); |
||
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->getMock('MockedTestObject', array('myMethod')); |
||
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() |
||
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->getMock('MockedTestObject', array('myMethod')); |
||
337 | $mockedEmbeddedObject->expects($this->once()) |
||
338 | ->method('myMethod') |
||
339 | ->will($this->returnValue('myMethodValue')); |
||
340 | |||
341 | $mockedObject = $this->getMock('MockedTestObject', array('getMyEmbeddedObject')); |
||
342 | $mockedObject->expects($this->once()) |
||
343 | ->method('getMyEmbeddedObject') |
||
344 | ->will($this->returnValue($mockedEmbeddedObject)); |
||
345 | |||
346 | $field = new FieldDescription(); |
||
355 | } |
||
356 |