Skip to content

Commit b21e145

Browse files
add failing test
fix
1 parent 4a68a18 commit b21e145

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

features/interactive.feature

+20
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,23 @@ Feature: It is possible to interactively fill in a form from the CLI
399399
[0] => blue
400400
)
401401
"""
402+
403+
Scenario: Default value with transformers
404+
Given I run the command "form:default_value_with_data_transformers" and I provide as input "Rue du Faubourg Saint-Honoré" with parameters
405+
| Parameter | Value |
406+
| --street | pennsylvania-ave-nw |
407+
Then the command has finished successfully
408+
And the output should contain
409+
"""
410+
Street [pennsylvania-ave-nw]:
411+
"""
412+
And the output should contain
413+
"""
414+
Array
415+
(
416+
[street] => Matthias\SymfonyConsoleForm\Tests\Form\Data\Street Object
417+
(
418+
[value] => Rue du Faubourg Saint-Honoré
419+
)
420+
)
421+
"""

test/Form/Data/Street.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Form\Data;
4+
5+
class Street implements \Stringable
6+
{
7+
public string $value;
8+
9+
public function __construct(string $street)
10+
{
11+
$this->value = $street;
12+
}
13+
14+
public function __toString(): string
15+
{
16+
return $this->value;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Form;
4+
5+
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Address;
6+
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Street;
7+
use Symfony\Component\Form\AbstractType;
8+
use Symfony\Component\Form\CallbackTransformer;
9+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
10+
use Symfony\Component\Form\Extension\Core\Type\TextType;
11+
use Symfony\Component\Form\FormBuilderInterface;
12+
13+
class DefaultValueWithDataTransformersType extends AbstractType
14+
{
15+
public function buildForm(FormBuilderInterface $builder, array $options)
16+
{
17+
$builder
18+
->add('street', StreetType::class, [
19+
'label' => 'Street'
20+
]);
21+
}
22+
}

test/Form/StreetType.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Form;
4+
5+
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Address;
6+
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Street;
7+
use Symfony\Component\Form\AbstractType;
8+
use Symfony\Component\Form\DataTransformerInterface;
9+
use Symfony\Component\Form\Exception\TransformationFailedException;
10+
use Symfony\Component\Form\Extension\Core\Type\TextType;
11+
use Symfony\Component\Form\FormBuilderInterface;
12+
use Symfony\Component\OptionsResolver\OptionsResolver;
13+
14+
class StreetType extends AbstractType implements DataTransformerInterface
15+
{
16+
public function buildForm(FormBuilderInterface $builder, array $options)
17+
{
18+
$builder->addModelTransformer($this);
19+
}
20+
21+
public function getParent()
22+
{
23+
return TextType::class;
24+
}
25+
26+
public function transform(mixed $value)
27+
{
28+
return (string)$value;
29+
}
30+
31+
public function reverseTransform(mixed $value)
32+
{
33+
return new Street((string)$value);
34+
}
35+
}

test/config.yml

+8
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ services:
154154
tags:
155155
- { name: console.command }
156156

157+
default_value_with_data_transformers:
158+
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
159+
arguments:
160+
- Matthias\SymfonyConsoleForm\Tests\Form\DefaultValueWithDataTransformersType
161+
- default_value_with_data_transformers
162+
tags:
163+
- { name: console.command }
164+
157165
nested_command:
158166
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
159167
arguments:

0 commit comments

Comments
 (0)