8000 [Form] Add ambiguous & exception debug:form tests by ogizanagi · Pull Request #24294 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Add ambiguous & exception debug:form tests #24294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private function getFqcnTypeClass(InputInterface $input, SymfonyStyle $io, $shor
return $classes[0];
}
if (!$input->isInteractive()) {
throw new InvalidArgumentException(sprintf("The type \"%s\" is ambiguous.\nDid you mean one of these?\n %s", $shortClassName, implode("\n ", $classes)));
throw new InvalidArgumentException(sprintf("The type \"%s\" is ambiguous.\n\nDid you mean one of these?\n %s", $shortClassName, implode("\n ", $classes)));
}

return $io->choice(sprintf("The type \"%s\" is ambiguous.\n\n Select one of the following form types to display its information:", $shortClassName), $classes, $classes[0]);
Expand Down
99 changes: 67 additions & 32 deletions src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

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\Command\DebugCommand;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormRegistryInterface;
use Symfony\Component\Form\ResolvedFormTypeInterface;
use Symfony\Component\Form\FormRegistry;
use Symfony\Component\Form\ResolvedFormTypeFactory;

class DebugCommandTest extends TestCase
{
Expand All @@ -39,6 +39,67 @@ public function testDebugSingleFormType()
$this->assertContains('Symfony\Component\Form\Extension\Core\Type\FormType (Block prefix: "form")', $tester->getDisplay());
}

/**
* @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException
* @expectedExceptionMessage Could not find type "NonExistentType"
*/
public function testDebugSingleFormTypeNotFound()
{
$tester = $this->createCommandTester();
$tester->execute(array('class' => 'NonExistentType'), array('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(array(
'Symfony\Component\Form\Tests\Fixtures\Debug\A',
'Symfony\Component\Form\Tests\Fixtures\Debug\B',
));

$tester->execute(array('class' => 'AmbiguousType'), array('decorated' => false, 'interactive' => false));
}

public function testDebugAmbiguousFormTypeInteractive()
{
$tester = $this->createCommandTester(array(
'Symfony\Component\Form\Tests\Fixtures\Debug\A',
'Symfony\Component\Form\Tests\Fixtures\Debug\B',
));

$tester->setInputs(array(0));
$tester->execute(array('class' => 'AmbiguousType'), array('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
*/
Expand All @@ -47,36 +108,10 @@ public function testDebugInvalidFormType()
$this->createCommandTester()->execute(array('class' => 'test'));
}

/**
* @return CommandTester
*/
private function createCommandTester()
private function createCommandTester(array $namespaces = null)
{
$resolvedFormType = $this->getMockBuilder(ResolvedFormTypeInterface::class)->getMock();
$resolvedFormType
->expects($this->any())
->method('getParent')
->willReturn(null)
;
$resolvedFormType
->expects($this->any())
->method('getInnerType')
->willReturn(new FormType())
;
$resolvedFormType
->expects($this->any())
->method('getTypeExtensions')
->willReturn(array())
;

$formRegistry = $this->getMockBuilder(FormRegistryInterface::class)->getMock();
$formRegistry
->expects($this->any())
->method('getType')
->will($this->returnValue($resolvedFormType))
;

$command = new DebugCommand($formRegistry);
$formRegistry = new FormRegistry(array(), new ResolvedFormTypeFactory());
$command = null === $namespaces ? new DebugCommand($formRegistry) : new DebugCommand($formRegistry, $namespaces);
$application = new Application();
$application->add($command);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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\Fixtures\Debug\A;

use Symfony\Component\Form\AbstractType;

class AmbiguousType extends AbstractType
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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\Fixtures\Debug\B;

use Symfony\Component\Form\AbstractType;

class AmbiguousType extends AbstractType
{
}
0