-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
DebugCommandTest.php
226 lines (186 loc) · 7.68 KB
/
DebugCommandTest.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?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\Command;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Command\DebugCommand;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormRegistry;
use Symfony\Component\Form\ResolvedFormTypeFactory;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class DebugCommandTest extends TestCase
{
public function testDebugDefaults()
{
$tester = $this->createCommandTester();
$ret = $tester->execute([], ['decorated' => false]);
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertContains('Built-in form types', $tester->getDisplay());
}
public function testDebugDeprecatedDefaults()
{
$tester = $this->createCommandTester(['Symfony\Component\Form\Tests\Console\Descriptor'], [TextType::class, FooType::class]);
$ret = $tester->execute(['--show-deprecated' => true], ['decorated' => false]);
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertSame(<<<TXT
Built-in form types (Symfony\Component\Form\Extension\Core\Type)
----------------------------------------------------------------
BirthdayType, DateTimeType, DateType, IntegerType, TimezoneType
Service form types
------------------
* Symfony\Component\Form\Tests\Command\FooType
TXT
, $tester->getDisplay(true));
}
public function testDebugSingleFormType()
{
$tester = $this->createCommandTester();
$ret = $tester->execute(['class' => 'FormType'], ['decorated' => false]);
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertContains('Symfony\Component\Form\Extension\Core\Type\FormType (Block prefix: "form")', $tester->getDisplay());
}
public function testDebugDateTimeType()
{
$tester = $this->createCommandTester();
$tester->execute(['class' => 'DateTime'], ['decorated' => false, 'interactive' => false]);
$this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
$this->assertContains('Symfony\Component\Form\Extension\Core\Type\DateTimeType (Block prefix: "datetime")', $tester->getDisplay());
}
public function testDebugFormTypeOption()
{
$tester = $this->createCommandTester();
$ret = $tester->execute(['class' => 'FormType', 'option' => 'method'], ['decorated' => false]);
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertContains('Symfony\Component\Form\Extension\Core\Type\FormType (method)', $tester->getDisplay());
}
/**
* @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException
* @expectedExceptionMessage Could not find type "NonExistentType"
*/
public function testDebugSingleFormTypeNotFound()
{
$tester = $this->createCommandTester();
$tester->execute(['class' => 'NonExistentType'], ['decorated' => false, 'interactive' => false]);
}
public function testDebugAmbiguousFormType()
{
$expectedMessage = <<<TXT
The type "AmbiguousType" is ambiguous.
Did you mean one of these?
Symfony\Component\Form\Tests\Fixtures\Debug\A\AmbiguousType
Symfony\Component\Form\Tests\Fixtures\Debug\B\AmbiguousType
TXT;
if (method_exists($this, 'expectException')) {
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($expectedMessage);
} else {
$this->setExpectedException(InvalidArgumentException::class, $expectedMessage);
}
$tester = $this->createCommandTester([
'Symfony\Component\Form\Tests\Fixtures\Debug\A',
'Symfony\Component\Form\Tests\Fixtures\Debug\B',
]);
$tester->execute(['class' => 'AmbiguousType'], ['decorated' => false, 'interactive' => false]);
}
public function testDebugAmbiguousFormTypeInteractive()
{
$tester = $this->createCommandTester([
'Symfony\Component\Form\Tests\Fixtures\Debug\A',
'Symfony\Component\Form\Tests\Fixtures\Debug\B',
]);
$tester->setInputs([0]);
$tester->execute(['class' => 'AmbiguousType'], ['decorated' => false, 'interactive' => true]);
$this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
$output = $tester->getDisplay(true);
$this->assertStringMatchesFormat(<<<TXT
The type "AmbiguousType" is ambiguous.
Select one of the following form types to display its information: [%A\A\AmbiguousType]:
[0] %A\A\AmbiguousType
[1] %A\B\AmbiguousType
%A
%A\A\AmbiguousType (Block prefix: "ambiguous")
%A
TXT
, $output);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testDebugInvalidFormType()
{
$this->createCommandTester()->execute(['class' => 'test']);
}
public function testDebugCustomFormTypeOption()
{
$tester = $this->createCommandTester([], [FooType::class]);
$ret = $tester->execute(['class' => FooType::class, 'option' => 'foo'], ['decorated' => false]);
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertStringMatchesFormat(<<<'TXT'
Symfony\Component\Form\Tests\Command\FooType (foo)
==================================================
---------------- -----------%s
Required true %s
---------------- -----------%s
Default - %s
---------------- -----------%s
Allowed types [ %s
"string"%s
] %s
---------------- -----------%s
Allowed values [ %s
"bar", %s
"baz" %s
] %s
---------------- -----------%s
Normalizers [ %s
Closure(%s
class:%s
this: %s
file: %s
line: %s
} %s
] %s
---------------- -----------%s
TXT
, $tester->getDisplay(true));
}
private function createCommandTester(array $namespaces = ['Symfony\Component\Form\Extension\Core\Type'], array $types = [])
{
$formRegistry = new FormRegistry([], new ResolvedFormTypeFactory());
$command = new DebugCommand($formRegistry, $namespaces, $types);
$application = new Application();
$application->add($command);
return new CommandTester($application->find('debug:form'));
}
}
class FooType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired('foo');
$resolver->setDefined('bar');
$resolver->setDeprecated('bar');
$resolver->setDefault('empty_data', function (Options $options) {
$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;
});
}
}