[Form] Add ambiguous & exception debug:form tests

This commit is contained in:
Maxime Steinhausser 2017-09-22 22:13:43 +02:00
parent 2e0cb6055f
commit 35f9c0ba2d
4 changed files with 104 additions and 33 deletions

View File

@ -118,7 +118,7 @@ EOF
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]);

View File

@ -13,11 +13,11 @@ 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\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
{
@ -39,6 +39,67 @@ class DebugCommandTest extends TestCase
$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
*/
@ -47,36 +108,10 @@ class DebugCommandTest extends TestCase
$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);

View File

@ -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
{
}

View File

@ -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
{
}