Skip to content

Commit 3127312

Browse files
Bernhard Schussekfabpot
authored andcommitted
[Form] Added option 'value_transformer' and 'normalization_transformer' to Field class
1 parent ece9691 commit 3127312

File tree

3 files changed

+79
-65
lines changed

3 files changed

+79
-65
lines changed

src/Symfony/Component/Form/Field.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public function __construct($key, array $options = array())
6969
$this->addOption('required', true);
7070
$this->addOption('disabled', false);
7171
$this->addOption('property_path', (string)$key);
72+
$this->addOption('value_transformer');
73+
$this->addOption('normalization_transformer');
7274

7375
$this->key = (string)$key;
7476

@@ -78,6 +80,14 @@ public function __construct($key, array $options = array())
7880

7981
parent::__construct($options);
8082

83+
if ($this->getOption('value_transformer')) {
84+
$this->setValueTransformer($this->getOption('value_transformer'));
85+
}
86+
87+
if ($this->getOption('normalization_transformer')) {
88+
$this->setNormalizationTransformer($this->getOption('normalization_transformer'));
89+
}
90+
8191
$this->normalizedData = $this->normalize($this->data);
8292
$this->transformedData = $this->transform($this->normalizedData);
8393
$this->required = $this->getOption('required');

tests/Symfony/Tests/Component/Form/FieldTest.php

Lines changed: 69 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,28 @@ public function testGetIdIncludesParent()
133133
$this->assertEquals('news_article_title', $this->field->getId());
134134
}
135135

136-
public function testLocaleIsPassedToLocalizableValueTransformer_setLocaleCalledBefore()
137-
{
138-
$transformer = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
139-
$transformer->expects($this->once())
140-
->method('setLocale')
141-
->with($this->equalTo('de_DE'));
142-
143-
$this->field->setLocale('de_DE');
144-
$this->field->setValueTransformer($transformer);
145-
}
136+
// public function testLocaleIsPassedToLocalizableValueTransformer_setLocaleCalledBefore()
137+
// {
138+
// $transformer = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
139+
// $transformer->expects($this->once())
140+
// ->method('setLocale')
141+
// ->with($this->equalTo('de_DE'));
142+
//
143+
// $this->field->setLocale('de_DE');
144+
// $this->field->setValueTransformer($transformer);
145+
// }
146146

147147
public function testLocaleIsPassedToValueTransformer_setLocaleCalledAfter()
148148
{
149149
$transformer = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
150150
$transformer->expects($this->exactly(2))
151151
->method('setLocale'); // we can't test the params cause they differ :(
152152

153-
$this->field->setValueTransformer($transformer);
154-
$this->field->setLocale('de_DE');
153+
$field = new TestField('title', array(
154+
'value_transformer' => $transformer,
155+
));
156+
157+
$field->setLocale('de_DE');
155158
}
156159

157160
public function testIsRequiredReturnsOwnValueIfNoParent()
@@ -231,21 +234,24 @@ public function testValuesAreTransformedCorrectlyIfNull_noValueTransformer()
231234

232235
public function testBoundValuesAreTransformedCorrectly()
233236
{
237+
$valueTransformer = $this->createMockTransformer();
238+
$normTransformer = $this->createMockTransformer();
239+
234240
$field = $this->getMock(
235241
'Symfony\Tests\Component\Form\Fixtures\TestField',
236242
array('processData'), // only mock processData()
237-
array('title')
243+
array('title', array(
244+
'value_transformer' => $valueTransformer,
245+
'normalization_transformer' => $normTransformer,
246+
))
238247
);
239248

240249
// 1a. The value is converted to a string and passed to the value transformer
241-
$valueTransformer = $this->createMockTransformer();
242250
$valueTransformer->expects($this->once())
243251
->method('reverseTransform')
244252
->with($this->identicalTo('0'))
245253
->will($this->returnValue('reverse[0]'));
246254

247-
$field->setValueTransformer($valueTransformer);
248-
249255
// 2. The output of the reverse transformation is passed to processData()
250256
// The processed data is accessible through getNormalizedData()
251257
$field->expects($this->once())
@@ -255,14 +261,11 @@ public function testBoundValuesAreTransformedCorrectly()
255261

256262
// 3. The processed data is denormalized and then accessible through
257263
// getData()
258-
$normTransformer = $this->createMockTransformer();
259264
$normTransformer->expects($this->once())
260265
->method('reverseTransform')
261266
->with($this->identicalTo('processed[reverse[0]]'))
262267
->will($this->returnValue('denorm[processed[reverse[0]]]'));
263268

264-
$field->setNormalizationTransformer($normTransformer);
265-
266269
// 4. The processed data is transformed again and then accessible
267270
// through getDisplayedData()
268271
$valueTransformer->expects($this->once())
@@ -279,21 +282,22 @@ public function testBoundValuesAreTransformedCorrectly()
279282

280283
public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsValue()
281284
{
285+
$transformer = $this->createMockTransformer();
286+
282287
$field = $this->getMock(
283288
'Symfony\Tests\Component\Form\Fixtures\TestField',
284289
array('processData'), // only mock processData()
285-
array('title')
290+
array('title', array(
291+
'value_transformer' => $transformer,
292+
))
286293
);
287294

288295
// 1. Empty values are converted to NULL by convention
289-
$transformer = $this->createMockTransformer();
290296
$transformer->expects($this->once())
291297
->method('reverseTransform')
292298
->with($this->identicalTo(''))
293299
->will($this->returnValue(null));
294300

295-
$field->setValueTransformer($transformer);
296-
297301
// 2. NULL is passed to processData()
298302
$field->expects($this->once())
299303
->method('processData')
@@ -314,26 +318,29 @@ public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturns
314318

315319
public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull()
316320
{
317-
// 1. Empty values are converted to NULL by convention
318321
$transformer = $this->createMockTransformer();
322+
323+
$field = new TestField('title', array(
324+
'value_transformer' => $transformer,
325+
));
326+
327+
// 1. Empty values are converted to NULL by convention
319328
$transformer->expects($this->once())
320329
->method('reverseTransform')
321330
->with($this->identicalTo(''))
322331
->will($this->returnValue(null));
323332

324-
$this->field->setValueTransformer($transformer);
325-
326333
// 2. The processed data is NULL and therefore transformed to an empty
327334
// string by convention
328335
$transformer->expects($this->once())
329336
->method('transform')
330337
->with($this->identicalTo(null))
331338
->will($this->returnValue(''));
332339

333-
$this->field->bind('');
340+
$field->bind('');
334341

335-
$this->assertSame(null, $this->field->getData());
336-
$this->assertEquals('', $this->field->getDisplayedData());
342+
$this->assertSame(null, $field->getData());
343+
$this->assertEquals('', $field->getDisplayedData());
337344
}
338345

339346
public function testBoundValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull_noValueTransformer()
@@ -348,25 +355,30 @@ public function testValuesAreTransformedCorrectly()
348355
{
349356
// The value is first passed to the normalization transformer...
350357
$normTransformer = $this->createMockTransformer();
351-
$normTransformer->expects($this->once())
358+
$normTransformer->expects($this->exactly(2))
352359
->method('transform')
353-
->with($this->identicalTo(0))
360+
// Impossible to test with PHPUnit because called twice
361+
// ->with($this->identicalTo(0))
354362
->will($this->returnValue('norm[0]'));
355363

356364
// ...and then to the value transformer
357365
$valueTransformer = $this->createMockTransformer();
358-
$valueTransformer->expects($this->once())
366+
$valueTransformer->expects($this->exactly(2))
359367
->method('transform')
360-
->with($this->identicalTo('norm[0]'))
368+
// Impossible to test with PHPUnit because called twice
369+
// ->with($this->identicalTo('norm[0]'))
361370
->will($this->returnValue('transform[norm[0]]'));
362371

363-
$this->field->setNormalizationTransformer($normTransformer);
364-
$this->field->setValueTransformer($valueTransformer);
365-
$this->field->setData(0);
372+
$field = new TestField('title', array(
373+
'value_transformer' => $valueTransformer,
374+
'normalization_transformer' => $normTransformer,
375+
));
376+
377+
$field->setData(0);
366378

367-
$this->assertEquals(0, $this->field->getData());
368-
$this->assertEquals('norm[0]', $this->field->getNormalizedData());
369-
$this->assertEquals('transform[norm[0]]', $this->field->getDisplayedData());
379+
$this->assertEquals(0, $field->getData());
380+
$this->assertEquals('norm[0]', $field->getNormalizedData());
381+
$this->assertEquals('transform[norm[0]]', $field->getDisplayedData());
370382
}
371383

372384
public function testBoundValuesAreTrimmedBeforeTransforming()
@@ -378,16 +390,20 @@ public function testBoundValuesAreTrimmedBeforeTransforming()
378390
->with($this->identicalTo('a'))
379391
->will($this->returnValue('reverse[a]'));
380392

381-
$transformer->expects($this->once())
393+
$transformer->expects($this->exactly(2))
382394
->method('transform')
383-
->with($this->identicalTo('reverse[a]'))
395+
// Impossible to test with PHPUnit because called twice
396+
// ->with($this->identicalTo('reverse[a]'))
384397
->will($this->returnValue('a'));
385398

386-
$this->field->setValueTransformer($transformer);
387-
$this->field->bind(' a ');
399+
$field = new TestField('title', array(
400+
'value_transformer' => $transformer,
401+
));
388402

389-
$this->assertEquals('a', $this->field->getDisplayedData());
390-
$this->assertEquals('reverse[a]', $this->field->getData());
403+
$field->bind(' a ');
404+
405+
$this->assertEquals('a', $field->getDisplayedData());
406+
$this->assertEquals('reverse[a]', $field->getData());
391407
}
392408

393409
public function testBoundValuesAreNotTrimmedBeforeTransformingIfDisabled()
@@ -399,13 +415,17 @@ public function testBoundValuesAreNotTrimmedBeforeTransformingIfDisabled()
399415
->with($this->identicalTo(' a '))
400416
->will($this->returnValue('reverse[ a ]'));
401417

402-
$transformer->expects($this->once())
418+
$transformer->expects($this->exactly(2))
403419
->method('transform')
404-
->with($this->identicalTo('reverse[ a ]'))
420+
// Impossible to test with PHPUnit because called twice
421+
// ->with($this->identicalTo('reverse[ a ]'))
405422
->will($this->returnValue(' a '));
406423

407-
$field = new TestField('title', array('trim' => false));
408-
$field->setValueTransformer($transformer);
424+
$field = new TestField('title', array(
425+
'trim' => false,
426+
'value_transformer' => $transformer,
427+
));
428+
409429
$field->bind(' a ');
410430

411431
$this->assertEquals(' a ', $field->getDisplayedData());

tests/Symfony/Tests/Component/Form/Fixtures/TestField.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,6 @@ public function render(array $attributes = array())
1111
{
1212
}
1313

14-
/**
15-
* Expose method for testing purposes
16-
*/
17-
public function setNormalizationTransformer(ValueTransformerInterface $normalizationTransformer)
18-
{
19-
parent::setNormalizationTransformer($normalizationTransformer);
20-
}
21-
22-
/**
23-
* Expose method for testing purposes
24-
*/
25-
public function setValueTransformer(ValueTransformerInterface $valueTransformer)
26-
{
27-
parent::setValueTransformer($valueTransformer);
28-
}
29-
3014
/**
3115
* Expose method for testing purposes
3216
*/

0 commit comments

Comments
 (0)