Skip to content

Commit

Permalink
feature #30666 [Form][Console] Use dumper (ro0NL)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.3-dev branch.

Discussion
----------

[Form][Console] Use dumper

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #...   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Continuation of #28898 for `debug:form`

Commits
-------

a94228e [Form][Console] Use dumper
  • Loading branch information
fabpot committed Mar 25, 2019
2 parents cb1ab07 + a94228e commit 31cd26b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
25 changes: 2 additions & 23 deletions src/Symfony/Component/Form/Console/Descriptor/TextDescriptor.php
Expand Up @@ -11,12 +11,10 @@

namespace Symfony\Component\Form\Console\Descriptor;

use Symfony\Component\Console\Helper\Dumper;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Form\ResolvedFormTypeInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\VarDumper\Caster\Caster;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;

/**
* @author Yonel Ceruto <yonelceruto@gmail.com>
Expand Down Expand Up @@ -97,7 +95,7 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
{
$definition = $this->getOptionDefinition($optionsResolver, $options['option']);

$dump = $this->getDumpFunction();
$dump = new Dumper($this->output);
$map = [];
if ($definition['deprecated']) {
$map = [
Expand Down Expand Up @@ -180,23 +178,4 @@ private function normalizeAndSortOptionsColumns(array $options)

return $options;
}

private function getDumpFunction()
{
$cloner = new VarCloner();
$cloner->addCasters(['Closure' => function ($c, $a) {
$prefix = Caster::PREFIX_VIRTUAL;

return [
$prefix.'file' => $a[$prefix.'file'],
$prefix.'line' => $a[$prefix.'line'],
];
}]);
$dumper = new CliDumper(null, null, CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR);
$dumper->setColors($this->output->isDecorated());

return function ($value) use ($dumper, $cloner) {
return rtrim($dumper->dump($cloner->cloneVar($value)->withRefHandles(false), true));
};
}
}
37 changes: 37 additions & 0 deletions src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php
Expand Up @@ -153,6 +153,43 @@ 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 %w
---------------- ---------------------------------------------------------------------------%s
Default - %w
---------------- ---------------------------------------------------------------------------%s
Allowed types [ %w
"string" %w
] %w
---------------- ---------------------------------------------------------------------------%s
Allowed values [ %w
"bar", %w
"baz" %w
] %w
---------------- ---------------------------------------------------------------------------%s
Normalizer Closure(Options $options, $value) { %w
class: "Symfony\Component\Form\Tests\Command\FooType" %w
this: Symfony\Component\Form\Tests\Command\FooType { …} %w
file: "%s"%w
line: "%d to %d"%w
} %w
---------------- ---------------------------------------------------------------------------%s
TXT
, $tester->getDisplay(true));
}

private function createCommandTester(array $namespaces = ['Symfony\Component\Form\Extension\Core\Type'], array $types = [])
{
$formRegistry = new FormRegistry([], new ResolvedFormTypeFactory());
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/Form/composer.json
Expand Up @@ -29,15 +29,16 @@
"symfony/validator": "~3.4|~4.0",
"symfony/dependency-injection": "~3.4|~4.0",
"symfony/config": "~3.4|~4.0",
"symfony/console": "~3.4|~4.0",
"symfony/console": "^4.3",
"symfony/http-foundation": "~3.4|~4.0",
"symfony/http-kernel": "~4.3",
"symfony/security-csrf": "~3.4|~4.0",
"symfony/translation": "~4.2",
"symfony/var-dumper": "~3.4|~4.0"
"symfony/var-dumper": "^4.3"
},
"conflict": {
"phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0",
"symfony/console": "<4.3",
"symfony/dependency-injection": "<3.4",
"symfony/doctrine-bridge": "<3.4",
"symfony/framework-bundle": "<3.4",
Expand Down

0 comments on commit 31cd26b

Please sign in to comment.