/
AbstractDescriptorTest.php
167 lines (140 loc) · 7.05 KB
/
AbstractDescriptorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests\Console\Descriptor;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Csrf\Type\FormTypeCsrfExtension;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\ResolvedFormType;
use Symfony\Component\Form\ResolvedFormTypeInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Csrf\CsrfTokenManager;
abstract class AbstractDescriptorTest extends TestCase
{
/** @dataProvider getDescribeDefaultsTestData */
public function testDescribeDefaults($object, array $options, $fixtureName)
{
$describedObject = $this->getObjectDescription($object, $options);
$expectedDescription = $this->getExpectedDescription($fixtureName);
if ('json' === $this->getFormat()) {
$this->assertEquals(json_encode(json_decode($expectedDescription), JSON_PRETTY_PRINT), json_encode(json_decode($describedObject), JSON_PRETTY_PRINT));
} else {
$this->assertEquals(trim($expectedDescription), trim(str_replace(PHP_EOL, "\n", $describedObject)));
}
}
/** @dataProvider getDescribeResolvedFormTypeTestData */
public function testDescribeResolvedFormType(ResolvedFormTypeInterface $type, array $options, $fixtureName)
{
$describedObject = $this->getObjectDescription($type, $options);
$expectedDescription = $this->getExpectedDescription($fixtureName);
if ('json' === $this->getFormat()) {
$this->assertEquals(json_encode(json_decode($expectedDescription), JSON_PRETTY_PRINT), json_encode(json_decode($describedObject), JSON_PRETTY_PRINT));
} else {
$this->assertEquals(trim($expectedDescription), trim(str_replace(PHP_EOL, "\n", $describedObject)));
}
}
/** @dataProvider getDescribeOptionTestData */
public function testDescribeOption(OptionsResolver $optionsResolver, array $options, $fixtureName)
{
$describedObject = $this->getObjectDescription($optionsResolver, $options);
$expectedDescription = $this->getExpectedDescription($fixtureName);
if ('json' === $this->getFormat()) {
$this->assertEquals(json_encode(json_decode($expectedDescription), JSON_PRETTY_PRINT), json_encode(json_decode($describedObject), JSON_PRETTY_PRINT));
} else {
$this->assertStringMatchesFormat(trim($expectedDescription), trim(str_replace(PHP_EOL, "\n", $describedObject)));
}
}
public function getDescribeDefaultsTestData()
{
$options['core_types'] = ['Symfony\Component\Form\Extension\Core\Type\FormType'];
$options['service_types'] = ['Symfony\Bridge\Doctrine\Form\Type\EntityType'];
$options['extensions'] = ['Symfony\Component\Form\Extension\Csrf\Type\FormTypeCsrfExtension'];
$options['guessers'] = ['Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser'];
$options['decorated'] = false;
$options['show_deprecated'] = false;
yield [null, $options, 'defaults_1'];
$options['core_types'] = [];
$options['service_types'] = [FooType::class];
$options['show_deprecated'] = true;
yield [null, $options, 'types_with_deprecated_options'];
}
public function getDescribeResolvedFormTypeTestData()
{
$typeExtensions = [new FormTypeCsrfExtension(new CsrfTokenManager())];
$parent = new ResolvedFormType(new FormType(), $typeExtensions);
yield [new ResolvedFormType(new ChoiceType(), [], $parent), ['decorated' => false, 'show_deprecated' => false], 'resolved_form_type_1'];
yield [new ResolvedFormType(new FormType()), ['decorated' => false, 'show_deprecated' => false], 'resolved_form_type_2'];
yield [new ResolvedFormType(new FooType(), [], $parent), ['decorated' => false, 'show_deprecated' => true], 'deprecated_options_of_type'];
}
public function getDescribeOptionTestData()
{
$parent = new ResolvedFormType(new FormType());
$options['decorated'] = false;
$options['show_deprecated'] = false;
$resolvedType = new ResolvedFormType(new ChoiceType(), [], $parent);
$options['type'] = $resolvedType->getInnerType();
$options['option'] = 'choice_translation_domain';
yield [$resolvedType->getOptionsResolver(), $options, 'default_option_with_normalizer'];
$resolvedType = new ResolvedFormType(new FooType(), [], $parent);
$options['type'] = $resolvedType->getInnerType();
$options['option'] = 'foo';
yield [$resolvedType->getOptionsResolver(), $options, 'required_option_with_allowed_values'];
$options['option'] = 'empty_data';
yield [$resolvedType->getOptionsResolver(), $options, 'overridden_option_with_default_closures'];
$resolvedType = new ResolvedFormType(new FooType(), [], $parent);
$options['type'] = $resolvedType->getInnerType();
$options['option'] = 'bar';
$options['show_deprecated'] = true;
yield [$resolvedType->getOptionsResolver(), $options, 'deprecated_option'];
}
abstract protected function getDescriptor();
abstract protected function getFormat();
private function getObjectDescription($object, array $options)
{
$output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, $options['decorated']);
$io = new SymfonyStyle(new ArrayInput([]), $output);
$this->getDescriptor()->describe($io, $object, $options);
return $output->fetch();
}
private function getExpectedDescription($name)
{
return file_get_contents($this->getFixtureFilename($name));
}
private function getFixtureFilename($name)
{
return sprintf('%s/../../Fixtures/Descriptor/%s.%s', __DIR__, $name, $this->getFormat());
}
}
class FooType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired('foo');
$resolver->setDefined('bar');
$resolver->setDeprecated('bar', 'vendor/package', '1.1');
$resolver->setDefault('empty_data', function (Options $options, $value) {
$foo = $options['foo'];
return function (FormInterface $form) use ($foo) {
return $form->getConfig()->getCompound() ? [$foo] : $foo;
};
});
$resolver->setAllowedTypes('foo', 'string');
$resolver->setAllowedValues('foo', ['bar', 'baz']);
$resolver->setNormalizer('foo', function (Options $options, $value) {
return (string) $value;
});
}
}