8000 [Form] Added tests for the case when "property_path" is null or false… · symfony/symfony@5e87dd8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5e87dd8

Browse files
committed
[Form] Added tests for the case when "property_path" is null or false. Instead of setting "property_path" to false, you should set "mapped" to false instead.
1 parent 2301b15 commit 5e87dd8

File tree

7 files changed

+174
-11
lines changed

7 files changed

+174
-11
lines changed

UPGRADE-2.1.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,28 @@
458458
}
459459
}
460460
461+
* Setting the option "property_path" to `false` was deprecated and will be unsupported
462+
as of Symfony 2.3.
463+
464+
You should use the new option "mapped" instead in order to set that you don't want
465+
a field to be mapped to its parent's data.
466+
467+
Before:
468+
469+
```
470+
$builder->add('termsAccepted', 'checkbox', array(
471+
'property_path' => false,
472+
));
473+
```
474+
475+
After:
476+
477+
```
478+
$builder->add('termsAccepted', 'checkbox', array(
479+
'mapped' => false,
480+
));
481+
```
482+
461483
### Validator
462484
463485
* The methods `setMessage()`, `getMessageTemplate()` and

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ CHANGELOG
7070
* deprecated method `guessMinLength` in favor of `guessPattern`
7171
* labels don't display field attributes anymore. Label attributes can be
7272
passed in the "label_attr" option/variable
73+
* added option "mapped" which should be used instead of setting "property_path" to false

src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ public function mapDataToForm($data, FormInterface $form)
4444
{
4545
if (!empty($data)) {
4646
$propertyPath = $form->getPropertyPath();
47+
$config = $form->getConfig();
4748

48-
if (null !== $propertyPath) {
49+
if (null !== $propertyPath && $config->getMapped()) {
4950
$propertyData = $propertyPath->getValue($data);
5051

5152
if (is_object($propertyData) && !$form->getAttribute('by_reference')) {
@@ -76,8 +77,11 @@ public function mapFormsToData(array $forms, &$data)
7677
public function mapFormToData(FormInterface $form, &$data)
7778
{
7879
$propertyPath = $form->getPropertyPath();
80+
$config = $form->getConfig();
7981

80-
if (null !== $propertyPath && $form->isSynchronized() && !$form->isDisabled()) {
82+
// Write-back is disabled if the form is not synchronized (transformation failed)
83+
// and if the form is disabled (modification not allowed)
84+
if (null !== $propertyPath && $config->getMapped() && $form->isSynchronized() && !$form->isDisabled()) {
8185
// If the data is identical to the value in $data, we are
8286
// dealing with a reference
8387
$isReference = $form->getData() === $propertyPath->getValue($data);

src/Symfony/Component/Form/Extension/Core/Type/FormType.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ public function buildForm(FormBuilder $builder, array $options)
4444
->setDisabled($options['disabled'])
4545
->setErrorBubbling($options['error_bubbling'])
4646
->setEmptyData($options['empty_data'])
47-
->setPropertyPath($options['property_path'])
47+
// BC compatibility, when "property_path" could be false
48+
->setPropertyPath(is_string($options['property_path']) ? $options['property_path'] : null)
49+
->setMapped($options['mapped'])
4850
->setAttribute('read_only', $options['read_only'])
4951
->setAttribute('by_reference', $options['by_reference'])
5052
->setAttribute('error_mapping', $options['error_mapping'])
@@ -189,6 +191,11 @@ public function getDefaultOptions()
189191
return !$options['single_control'];
190192
};
191193

194+
// BC clause: former property_path=false now equals mapped=false
195+
$mapped = function (Options $options) {
196+
return false !== $options['property_path'];
197+
};
198+
192199
return array(
193200
'data' => null,
194201
'data_class' => $dataClass,
@@ -200,6 +207,7 @@ public function getDefaultOptions()
200207
'max_length' => null,
201208
'pattern' => null,
202209
'property_path' => null,
210+
'mapped' => $mapped,
203211
'by_reference' => true,
204212
'error_bubbling' => $errorBubbling,
205213
'error_mapping' => array(),

src/Symfony/Component/Form/Form.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,15 @@ public function getName()
168168
*/
169169
public function getPropertyPath()
170170
{
171-
if (!$this->hasParent() || null !== $this->config->getPropertyPath()) {
171+
if (null !== $this->config->getPropertyPath()) {
172172
return $this->config->getPropertyPath();
173173
}
174174

175175
if (null === $this->getName() || '' === $this->getName()) {
176176
return null;
177177
}
178178

179-
if (null === $this->getParent()->getConfig()->getDataClass()) {
179+
if ($this->hasParent() && null === $this->getParent()->getConfig()->getDataClass()) {
180180
return new PropertyPath('[' . $this->getName() . ']');
181181
}
182182

src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@ private function getPropertyPath($path)
6464
->getMock();
6565
}
6666

67-
private function getForm(PropertyPath $propertyPath = null, $byReference, $synchronized = true)
67+
private function getForm(PropertyPath $propertyPath = null, $byReference, $synchronized = true, $mapped = true, $disabled = false)
6868
{
69+
$config = $this->getMock('Symfony\Component\Form\FormConfigInterface');
70+
71+
$config->expects($this->any())
72+
->method('getMapped')
73+
->will($this->returnValue($mapped));
74+
6975
$form = $this->getMockBuilder(__CLASS__ . '_Form')
7076
// PHPUnit's getMockForAbstractClass does not behave like in the docs..
7177
// If the array is empty, all methods are mocked. If it is not
@@ -76,6 +82,10 @@ private function getForm(PropertyPath $propertyPath = null, $byReference, $synch
7682

7783
$form->setAttribute('by_reference', $byReference);
7884

85+
$form->expects($this->any())
86+
->method('getConfig')
87+
->will($this->returnValue($config));
88+
7989
$form->expects($this->any())
8090
->method('getPropertyPath')
8191
->will($this->returnValue($propertyPath));
@@ -84,6 +94,10 @@ private function getForm(PropertyPath $propertyPath = null, $byReference, $synch
8494
->method('isSynchronized')
8595
->will($this->returnValue($synchronized));
8696

97+
$form->expects($this->any())
98+
->method('isDisabled')
99+
->will($this->returnValue($disabled));
100+
87101
return $form;
88102
}
89103

@@ -132,21 +146,34 @@ public function testMapDataToFormIgnoresEmptyPropertyPath()
132146

133147
$form = $this->getForm(null, true);
134148

135-
$form->expects($this->never())
136-
->method('setData');
149+
$this->mapper->mapDataToForm($car, $form);
150+
151+
$this->assertNull($form->getData());
152+
}
153+
154+
public function testMapDataToFormIgnoresUnmapped()
155+
{
156+
$car = new \stdClass();
157+
$propertyPath = $this->getPropertyPath('engine');
158+
159+
$propertyPath->expects($this->never())
160+
->method('getValue');
161+
162+
$form = $this->getForm($propertyPath, true, true, false);
137163

138164
$this->mapper->mapDataToForm($car, $form);
165+
166+
$this->assertNull($form->getData());
139167
}
140168

141169
public function testMapDataToFormIgnoresEmptyData()
142170
{
143171
$propertyPath = $this->getPropertyPath('engine');
144172
$form = $this->getForm($propertyPath, true);
145173

146-
$form->expects($this->never())
147-
->method('setData');
148-
149174
$this->mapper->mapDataToForm(null, $form);
175+
176+
$this->assertNull($form->getData());
150177
}
151178

152179
public function testMapFormToDataWritesBackIfNotByReference()
@@ -201,4 +228,63 @@ public function testMapFormToDataWritesBackIfByReferenceAndReference()
201228

202229
$this->mapper->mapFormToData($form, $car);
203230
}
231+
232+
public function testMapFormToDataIgnoresUnmapped()
233+
{
234+
$car = new \stdClass();
235+
$engine = new \stdClass();
236+
$propertyPath = $this->getPropertyPath('engine');
237+
238+
$propertyPath->expects($this->never())
239+
->method('setValue');
240+
241+
$form = $this->getForm($propertyPath, true, true, false);
242+
$form->setData($engine);
243+
244+
$this->mapper->mapFormToData($form, $car);
245+
}
246+
247+
public function testMapFormToDataIgnoresEmptyData()
248+
{
249+
$car = new \stdClass();
250+
$propertyPath = $this->getPropertyPath('engine');
251+
252+
$propertyPath->expects($this->never())
253+
->method('setValue');
254+
255+
$form = $this->getForm($propertyPath, true);
256+
$form->setData(null);
257+
258+
$this->mapper->mapFormToData($form, $car);
259+
}
260+
261+
public function testMapFormToDataIgnoresUnsynchronized()
262+
{
263+
$car = new \stdClass();
264+
$engine = new \stdClass();
265+
$propertyPath = $this->getPropertyPath('engine');
266+
267+
$propertyPath->expects($this->never())
268+
->method('setValue');
269+
270+
$form = $this->getForm($propertyPath, true, false);
271+
$form->setData($engine);
272+
273+
$this->mapper->mapFormToData($form, $car);
274+
}
275+
276+
public function testMapFormToDataIgnoresDisabled()
277+
{
278+
$car = new \stdClass();
279+
$engine = new \stdClass();
280+
$propertyPath = $this->getPropertyPath('engine');
281+
282+
$propertyPath->expects($this->never())
283+
->method('setValue');
284+
285+
$form = $this->getForm($propertyPath, true, true, true, true);
286+
$form->setData($engine);
287+
288+
$this->mapper->mapFormToData($form, $car);
289+
}
204290
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,4 +562,46 @@ public function testOverrideErrorBubbling()
562562

563563
$this->assertTrue($form->getErrorBubbling());
564564
}
565+
566+
public function testPropertyPath()
567+
{
568+
$form = $this->factory->create('form', null, array(
569+
'property_path' => 'foo',
570+
));
571+
572+
$this->assertEquals(new PropertyPath('foo'), $form->getPropertyPath());
573+
$this->assertTrue($form->getConfig()->getMapped());
574+
}
575+
576+
public function testPropertyPathNullImpliesDefault()
577+
{
578+
$form = $this->factory->createNamed('form', 'name', null, array(
579+
'property_path' => null,
580+
));
581+
582+
$this->assertEquals(new PropertyPath('name'), $form->getPropertyPath());
583+
$this->assertTrue($form->getConfig()->getMapped());
584+
}
585+
586+
// BC
587+
public function testPropertyPathFalseImpliesDefaultNotMapped()
588+
{
589+
$form = $this->factory->createNamed('form', 'name', null, array(
590+
'property_path' => false,
591+
));
592+
593+
$this->assertEquals(new PropertyPath('name'), $form->getPropertyPath());
594+
$this->assertFalse($form->getConfig()->getMapped());
595+
}
596+
597+
public function testNotMapped()
598+
{
599+
$form = $this->factory->create('form', null, array(
600+
'property_path' => 'foo',
601+
'mapped' => false,
602+
));
603+
604+
$this->assertEquals(new PropertyPath('foo'), $form->getPropertyPath());
605+
$this->assertFalse($form->getConfig()->getMapped());
606+
}
565607
}

0 commit comments

Comments
 (0)
0