From 35f9c0ba2d26cb21eca8f75f135a35052ca22ef3 Mon Sep 17 00:00:00 2001 From: Maxime Steinhausser Date: Fri, 22 Sep 2017 22:13:43 +0200 Subject: [PATCH] [Form] Add ambiguous & exception debug:form tests --- .../Component/Form/Command/DebugCommand.php | 2 +- .../Form/Tests/Command/DebugCommandTest.php | 99 +++++++++++++------ .../Tests/Fixtures/Debug/A/AmbiguousType.php | 18 ++++ .../Tests/Fixtures/Debug/B/AmbiguousType.php | 18 ++++ 4 files changed, 104 insertions(+), 33 deletions(-) create mode 100644 src/Symfony/Component/Form/Tests/Fixtures/Debug/A/AmbiguousType.php create mode 100644 src/Symfony/Component/Form/Tests/Fixtures/Debug/B/AmbiguousType.php diff --git a/src/Symfony/Component/Form/Command/DebugCommand.php b/src/Symfony/Component/Form/Command/DebugCommand.php index d9b3eee838244..d8e1f5a0789b0 100644 --- a/src/Symfony/Component/Form/Command/DebugCommand.php +++ b/src/Symfony/Component/Form/Command/DebugCommand.php @@ -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]); diff --git a/src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php b/src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php index 610f6197d62cb..d24af0a4b1bc5 100644 --- a/src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php +++ b/src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php @@ -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 { @@ -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 = <<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(<<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); diff --git a/src/Symfony/Component/Form/Tests/Fixtures/Debug/A/AmbiguousType.php b/src/Symfony/Component/Form/Tests/Fixtures/Debug/A/AmbiguousType.php new file mode 100644 index 0000000000000..e6e183902882c --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Fixtures/Debug/A/AmbiguousType.php @@ -0,0 +1,18 @@ + + * + * 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 +{ +} diff --git a/src/Symfony/Component/Form/Tests/Fixtures/Debug/B/AmbiguousType.php b/src/Symfony/Component/Form/Tests/Fixtures/Debug/B/AmbiguousType.php new file mode 100644 index 0000000000000..c670e8e925bdf --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Fixtures/Debug/B/AmbiguousType.php @@ -0,0 +1,18 @@ + + * + * 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 +{ +}