4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Tests\Extension\Core\DataMapper
;
14 use Symfony\Component\Form\FormConfigBuilder
;
15 use Symfony\Component\Form\FormConfigInterface
;
16 use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper
;
18 class PropertyPathMapperTest
extends \PHPUnit_Framework_TestCase
21 * @var PropertyPathMapper
26 * @var \PHPUnit_Framework_MockObject_MockObject
31 * @var \PHPUnit_Framework_MockObject_MockObject
33 private $propertyAccessor;
35 protected function setUp()
37 if (!class_exists('Symfony\Component\EventDispatcher\Event')) {
38 $this->markTestSkipped('The "EventDispatcher" component is not available');
41 if (!class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) {
42 $this->markTestSkipped('The "PropertyAccess" component is not available');
45 $this->dispatcher
= $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
46 $this->propertyAccessor
= $this->getMock('Symfony\Component\PropertyAccess\PropertyAccessorInterface');
47 $this->mapper
= new PropertyPathMapper($this->propertyAccessor
);
52 * @return \PHPUnit_Framework_MockObject_MockObject
54 private function getPropertyPath($path)
56 return $this->getMockBuilder('Symfony\Component\PropertyAccess\PropertyPath')
57 ->setConstructorArgs(array($path))
58 ->setMethods(array('getValue', 'setValue'))
63 * @param FormConfigInterface $config
64 * @param Boolean $synchronized
65 * @return \PHPUnit_Framework_MockObject_MockObject
67 private function getForm(FormConfigInterface
$config, $synchronized = true)
69 $form = $this->getMockBuilder('Symfony\Component\Form\Form')
70 ->setConstructorArgs(array($config))
71 ->setMethods(array('isSynchronized'))
74 $form->expects($this->any())
75 ->method('isSynchronized')
76 ->will($this->returnValue($synchronized));
82 * @return \PHPUnit_Framework_MockObject_MockObject
84 private function getDataMapper()
86 return $this->getMock('Symfony\Component\Form\DataMapperInterface');
89 public function testMapDataToFormsPassesObjectRefIfByReference()
91 $car = new \
stdClass();
92 $engine = new \
stdClass();
93 $propertyPath = $this->getPropertyPath('engine');
95 $this->propertyAccessor
->expects($this->once())
97 ->with($car, $propertyPath)
98 ->will($this->returnValue($engine));
100 $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher
);
101 $config->setByReference(true);
102 $config->setPropertyPath($propertyPath);
103 $form = $this->getForm($config);
105 $this->mapper
->mapDataToForms($car, array($form));
107 // Can't use isIdentical() above because mocks always clone their
108 // arguments which can't be disabled in PHPUnit 3.6
109 $this->assertSame($engine, $form->getData());
112 public function testMapDataToFormsPassesObjectCloneIfNotByReference()
114 $car = new \
stdClass();
115 $engine = new \
stdClass();
116 $propertyPath = $this->getPropertyPath('engine');
118 $this->propertyAccessor
->expects($this->once())
120 ->with($car, $propertyPath)
121 ->will($this->returnValue($engine));
123 $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher
);
124 $config->setByReference(false);
125 $config->setPropertyPath($propertyPath);
126 $form = $this->getForm($config);
128 $this->mapper
->mapDataToForms($car, array($form));
130 $this->assertNotSame($engine, $form->getData());
131 $this->assertEquals($engine, $form->getData());
134 public function testMapDataToFormsIgnoresEmptyPropertyPath()
136 $car = new \
stdClass();
138 $config = new FormConfigBuilder(null, '\stdClass', $this->dispatcher
);
139 $config->setByReference(true);
140 $form = $this->getForm($config);
142 $this->assertNull($form->getPropertyPath());
144 $this->mapper
->mapDataToForms($car, array($form));
146 $this->assertNull($form->getData());
149 public function testMapDataToFormsIgnoresUnmapped()
151 $car = new \
stdClass();
152 $propertyPath = $this->getPropertyPath('engine');
154 $this->propertyAccessor
->expects($this->never())
155 ->method('getValue');
157 $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher
);
158 $config->setByReference(true);
159 $config->setMapped(false);
160 $config->setPropertyPath($propertyPath);
161 $form = $this->getForm($config);
163 $this->mapper
->mapDataToForms($car, array($form));
165 $this->assertNull($form->getData());
168 public function testMapDataToFormsIgnoresEmptyData()
170 $propertyPath = $this->getPropertyPath('engine');
172 $this->propertyAccessor
->expects($this->never())
173 ->method('getValue');
175 $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher
);
176 $config->setByReference(true);
177 $config->setPropertyPath($propertyPath);
178 $form = $this->getForm($config);
180 $this->mapper
->mapDataToForms(null, array($form));
182 $this->assertNull($form->getData());
185 public function testMapFormsToDataWritesBackIfNotByReference()
187 $car = new \
stdClass();
188 $engine = new \
stdClass();
189 $propertyPath = $this->getPropertyPath('engine');
191 $this->propertyAccessor
->expects($this->once())
193 ->with($car, $propertyPath, $engine);
195 $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher
);
196 $config->setByReference(false);
197 $config->setPropertyPath($propertyPath);
198 $config->setData($engine);
199 $form = $this->getForm($config);
201 $this->mapper
->mapFormsToData(array($form), $car);
204 public function testMapFormsToDataWritesBackIfByReferenceButNoReference()
206 $car = new \
stdClass();
207 $engine = new \
stdClass();
208 $propertyPath = $this->getPropertyPath('engine');
210 $this->propertyAccessor
->expects($this->once())
212 ->with($car, $propertyPath, $engine);
214 $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher
);
215 $config->setByReference(true);
216 $config->setPropertyPath($propertyPath);
217 $config->setData($engine);
218 $form = $this->getForm($config);
220 $this->mapper
->mapFormsToData(array($form), $car);
223 public function testMapFormsToDataWritesBackIfByReferenceAndReference()
225 $car = new \
stdClass();
226 $engine = new \
stdClass();
227 $propertyPath = $this->getPropertyPath('engine');
229 // $car already contains the reference of $engine
230 $this->propertyAccessor
->expects($this->once())
232 ->with($car, $propertyPath)
233 ->will($this->returnValue($engine));
235 $this->propertyAccessor
->expects($this->never())
236 ->method('setValue');
238 $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher
);
239 $config->setByReference(true);
240 $config->setPropertyPath($propertyPath);
241 $config->setData($engine);
242 $form = $this->getForm($config);
244 $this->mapper
->mapFormsToData(array($form), $car);
247 public function testMapFormsToDataIgnoresUnmapped()
249 $car = new \
stdClass();
250 $engine = new \
stdClass();
251 $propertyPath = $this->getPropertyPath('engine');
253 $this->propertyAccessor
->expects($this->never())
254 ->method('setValue');
256 $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher
);
257 $config->setByReference(true);
258 $config->setPropertyPath($propertyPath);
259 $config->setData($engine);
260 $config->setMapped(false);
261 $form = $this->getForm($config);
263 $this->mapper
->mapFormsToData(array($form), $car);
266 public function testMapFormsToDataIgnoresEmptyData()
268 $car = new \
stdClass();
269 $propertyPath = $this->getPropertyPath('engine');
271 $this->propertyAccessor
->expects($this->never())
272 ->method('setValue');
274 $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher
);
275 $config->setByReference(true);
276 $config->setPropertyPath($propertyPath);
277 $config->setData(null);
278 $form = $this->getForm($config);
280 $this->mapper
->mapFormsToData(array($form), $car);
283 public function testMapFormsToDataIgnoresUnsynchronized()
285 $car = new \
stdClass();
286 $engine = new \
stdClass();
287 $propertyPath = $this->getPropertyPath('engine');
289 $this->propertyAccessor
->expects($this->never())
290 ->method('setValue');
292 $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher
);
293 $config->setByReference(true);
294 $config->setPropertyPath($propertyPath);
295 $config->setData($engine);
296 $form = $this->getForm($config, false);
298 $this->mapper
->mapFormsToData(array($form), $car);
301 public function testMapFormsToDataIgnoresDisabled()
303 $car = new \
stdClass();
304 $engine = new \
stdClass();
305 $propertyPath = $this->getPropertyPath('engine');
307 $this->propertyAccessor
->expects($this->never())
308 ->method('setValue');
310 $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher
);
311 $config->setByReference(true);
312 $config->setPropertyPath($propertyPath);
313 $config->setData($engine);
314 $config->setDisabled(true);
315 $form = $this->getForm($config);
317 $this->mapper
->mapFormsToData(array($form), $car);