]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Tests / Extension / Core / DataMapper / PropertyPathMapperTest.php
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\Form\Tests\Extension\Core\DataMapper;
13
14 use Symfony\Component\Form\FormConfigBuilder;
15 use Symfony\Component\Form\FormConfigInterface;
16 use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
17
18 class PropertyPathMapperTest extends \PHPUnit_Framework_TestCase
19 {
20 /**
21 * @var PropertyPathMapper
22 */
23 private $mapper;
24
25 /**
26 * @var \PHPUnit_Framework_MockObject_MockObject
27 */
28 private $dispatcher;
29
30 /**
31 * @var \PHPUnit_Framework_MockObject_MockObject
32 */
33 private $propertyAccessor;
34
35 protected function setUp()
36 {
37 if (!class_exists('Symfony\Component\EventDispatcher\Event')) {
38 $this->markTestSkipped('The "EventDispatcher" component is not available');
39 }
40
41 if (!class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) {
42 $this->markTestSkipped('The "PropertyAccess" component is not available');
43 }
44
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);
48 }
49
50 /**
51 * @param $path
52 * @return \PHPUnit_Framework_MockObject_MockObject
53 */
54 private function getPropertyPath($path)
55 {
56 return $this->getMockBuilder('Symfony\Component\PropertyAccess\PropertyPath')
57 ->setConstructorArgs(array($path))
58 ->setMethods(array('getValue', 'setValue'))
59 ->getMock();
60 }
61
62 /**
63 * @param FormConfigInterface $config
64 * @param Boolean $synchronized
65 * @return \PHPUnit_Framework_MockObject_MockObject
66 */
67 private function getForm(FormConfigInterface $config, $synchronized = true)
68 {
69 $form = $this->getMockBuilder('Symfony\Component\Form\Form')
70 ->setConstructorArgs(array($config))
71 ->setMethods(array('isSynchronized'))
72 ->getMock();
73
74 $form->expects($this->any())
75 ->method('isSynchronized')
76 ->will($this->returnValue($synchronized));
77
78 return $form;
79 }
80
81 /**
82 * @return \PHPUnit_Framework_MockObject_MockObject
83 */
84 private function getDataMapper()
85 {
86 return $this->getMock('Symfony\Component\Form\DataMapperInterface');
87 }
88
89 public function testMapDataToFormsPassesObjectRefIfByReference()
90 {
91 $car = new \stdClass();
92 $engine = new \stdClass();
93 $propertyPath = $this->getPropertyPath('engine');
94
95 $this->propertyAccessor->expects($this->once())
96 ->method('getValue')
97 ->with($car, $propertyPath)
98 ->will($this->returnValue($engine));
99
100 $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
101 $config->setByReference(true);
102 $config->setPropertyPath($propertyPath);
103 $form = $this->getForm($config);
104
105 $this->mapper->mapDataToForms($car, array($form));
106
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());
110 }
111
112 public function testMapDataToFormsPassesObjectCloneIfNotByReference()
113 {
114 $car = new \stdClass();
115 $engine = new \stdClass();
116 $propertyPath = $this->getPropertyPath('engine');
117
118 $this->propertyAccessor->expects($this->once())
119 ->method('getValue')
120 ->with($car, $propertyPath)
121 ->will($this->returnValue($engine));
122
123 $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
124 $config->setByReference(false);
125 $config->setPropertyPath($propertyPath);
126 $form = $this->getForm($config);
127
128 $this->mapper->mapDataToForms($car, array($form));
129
130 $this->assertNotSame($engine, $form->getData());
131 $this->assertEquals($engine, $form->getData());
132 }
133
134 public function testMapDataToFormsIgnoresEmptyPropertyPath()
135 {
136 $car = new \stdClass();
137
138 $config = new FormConfigBuilder(null, '\stdClass', $this->dispatcher);
139 $config->setByReference(true);
140 $form = $this->getForm($config);
141
142 $this->assertNull($form->getPropertyPath());
143
144 $this->mapper->mapDataToForms($car, array($form));
145
146 $this->assertNull($form->getData());
147 }
148
149 public function testMapDataToFormsIgnoresUnmapped()
150 {
151 $car = new \stdClass();
152 $propertyPath = $this->getPropertyPath('engine');
153
154 $this->propertyAccessor->expects($this->never())
155 ->method('getValue');
156
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);
162
163 $this->mapper->mapDataToForms($car, array($form));
164
165 $this->assertNull($form->getData());
166 }
167
168 public function testMapDataToFormsIgnoresEmptyData()
169 {
170 $propertyPath = $this->getPropertyPath('engine');
171
172 $this->propertyAccessor->expects($this->never())
173 ->method('getValue');
174
175 $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
176 $config->setByReference(true);
177 $config->setPropertyPath($propertyPath);
178 $form = $this->getForm($config);
179
180 $this->mapper->mapDataToForms(null, array($form));
181
182 $this->assertNull($form->getData());
183 }
184
185 public function testMapFormsToDataWritesBackIfNotByReference()
186 {
187 $car = new \stdClass();
188 $engine = new \stdClass();
189 $propertyPath = $this->getPropertyPath('engine');
190
191 $this->propertyAccessor->expects($this->once())
192 ->method('setValue')
193 ->with($car, $propertyPath, $engine);
194
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);
200
201 $this->mapper->mapFormsToData(array($form), $car);
202 }
203
204 public function testMapFormsToDataWritesBackIfByReferenceButNoReference()
205 {
206 $car = new \stdClass();
207 $engine = new \stdClass();
208 $propertyPath = $this->getPropertyPath('engine');
209
210 $this->propertyAccessor->expects($this->once())
211 ->method('setValue')
212 ->with($car, $propertyPath, $engine);
213
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);
219
220 $this->mapper->mapFormsToData(array($form), $car);
221 }
222
223 public function testMapFormsToDataWritesBackIfByReferenceAndReference()
224 {
225 $car = new \stdClass();
226 $engine = new \stdClass();
227 $propertyPath = $this->getPropertyPath('engine');
228
229 // $car already contains the reference of $engine
230 $this->propertyAccessor->expects($this->once())
231 ->method('getValue')
232 ->with($car, $propertyPath)
233 ->will($this->returnValue($engine));
234
235 $this->propertyAccessor->expects($this->never())
236 ->method('setValue');
237
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);
243
244 $this->mapper->mapFormsToData(array($form), $car);
245 }
246
247 public function testMapFormsToDataIgnoresUnmapped()
248 {
249 $car = new \stdClass();
250 $engine = new \stdClass();
251 $propertyPath = $this->getPropertyPath('engine');
252
253 $this->propertyAccessor->expects($this->never())
254 ->method('setValue');
255
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);
262
263 $this->mapper->mapFormsToData(array($form), $car);
264 }
265
266 public function testMapFormsToDataIgnoresEmptyData()
267 {
268 $car = new \stdClass();
269 $propertyPath = $this->getPropertyPath('engine');
270
271 $this->propertyAccessor->expects($this->never())
272 ->method('setValue');
273
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);
279
280 $this->mapper->mapFormsToData(array($form), $car);
281 }
282
283 public function testMapFormsToDataIgnoresUnsynchronized()
284 {
285 $car = new \stdClass();
286 $engine = new \stdClass();
287 $propertyPath = $this->getPropertyPath('engine');
288
289 $this->propertyAccessor->expects($this->never())
290 ->method('setValue');
291
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);
297
298 $this->mapper->mapFormsToData(array($form), $car);
299 }
300
301 public function testMapFormsToDataIgnoresDisabled()
302 {
303 $car = new \stdClass();
304 $engine = new \stdClass();
305 $propertyPath = $this->getPropertyPath('engine');
306
307 $this->propertyAccessor->expects($this->never())
308 ->method('setValue');
309
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);
316
317 $this->mapper->mapFormsToData(array($form), $car);
318 }
319 }