8000 [Form] Add ambiguous & exception debug:form tests · symfony/symfony@35f9c0b · GitHub
[go: up one dir, main page]

Skip to content

Commit 35f9c0b

Browse files
committed
[Form] Add ambiguous & exception debug:form tests
1 parent 2e0cb60 commit 35f9c0b

File tree

4 files changed

+104
-33
lines changed

4 files changed

+104
-33
lines changed

src/Symfony/Component/Form/Command/DebugCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private function getFqcnTypeClass(InputInterface $input, SymfonyStyle $io, $shor
118118
return $classes[0];
119119
}
120120
if (!$input->isInteractive()) {
121-
throw new InvalidArgumentException(sprintf("The type \"%s\" is ambiguous.\nDid you mean one of these?\n %s", $shortClassName, implode("\n ", $classes)));
121+
throw new InvalidArgumentException(sprintf("The type \"%s\" is ambiguous.\n\nDid you mean one of these?\n %s", $shortClassName, implode("\n ", $classes)));
122122
}
123123

124124
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]);

src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Application;
16+
use Symfony\Component\Console\Exception\InvalidArgumentException;
1617
use Symfony\Component\Console\Tester\CommandTester;
1718
use Symfony\Component\Form\Command\DebugCommand;
18-
use Symfony\Component\Form\Extension\Core\Type\FormType;
19-
use Symfony\Component\Form\FormRegistryInterface;
20-
use Symfony\Component\Form\ResolvedFormTypeInterface;
19+
use Symfony\Component\Form\FormRegistry;
20+
use Symfony\Component\Form\ResolvedFormTypeFactory;
2121

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

42+
/**
43+
* @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException
44+
* @expectedExceptionMessage Could not find type "NonExistentType"
45+
*/
46+
public function testDebugSingleFormTypeNotFound()
47+
{
48+
$tester = $this->createCommandTester();
49+
$tester->execute(array('class' => 'NonExistentType'), array('decorated' => false, 'interactive' => false));
50+
}
51+
52+
public function testDebugAmbiguousFormType()
53+
{
54+
$expectedMessage = <<<TXT
55+
The type "AmbiguousType" is ambiguous.
56+
57+
Did you mean one of these?
58+
Symfony\Component\Form\Tests\Fixtures\Debug\A\AmbiguousType
59+
Symfony\Component\Form\Tests\Fixtures\Debug\B\AmbiguousType
60+
TXT;
61+
62+
if (method_exists($this, 'expectException')) {
63+
$this->expectException(InvalidArgumentException::class);
64+
$this->expectExceptionMessage($expectedMessage);
65+
} else {
66+
$this->setExpectedException(InvalidArgumentException::class, $expectedMessage);
67+
}
68+
69+
$tester = $this->createCommandTester(array(
70+
'Symfony\Component\Form\Tests\Fixtures\Debug\A',
71+
'Symfony\Component\Form\Tests\Fixtures\Debug\B',
72+
));
73+
74+
$tester->execute(array('class' => 'AmbiguousType'), array('decorated' => false, 'interactive' => false));
75+
}
76+
77+
public function testDebugAmbiguousFormTypeInteractive()
78+
{
79+
$tester = $this->createCommandTester(array(
80+
'Symfony\Component\Form\Tests\Fixtures\Debug\A',
81+
'Symfony\Component\Form\Tests\Fixtures\Debug\B',
82+
));
83+
84+
$tester->setInputs(array(0));
85+
$tester->execute(array('class' => 'AmbiguousType'), array('decorated' => false, 'interactive' => true));
86+
87+
$this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
88+
$output = $tester->getDisplay(true);
89+
$this->assertStringMatchesFormat(<<<TXT
90+
91+
The type "AmbiguousType" is ambiguous.
92+
93+
Select one of the following form types to display its information: [%A\A\AmbiguousType]:
94+
[0] %A\A\AmbiguousType
95+
[1] %A\B\AmbiguousType
96+
%A
97+
%A\A\AmbiguousType (Block prefix: "ambiguous")
98+
%A
99+
TXT
100+
, $output);
101+
}
102+
42103
/**
43104
* @expectedException \InvalidArgumentException
44105
*/
@@ -47,36 +108,10 @@ public function testDebugInvalidFormType()
47108
$this->createCommandTester()->execute(array('class' => 'test'));
48109
}
49110

50-
/**
51-
* @return CommandTester
52-
*/
53-
private function createCommandTester()
111+
private function createCommandTester(array $namespaces = null)
54112
{
55-
$resolvedFormType = $this->getMockBuilder(ResolvedFormTypeInterface::class)->getMock();
56-
$resolvedFormType
57-
->expects($this->any())
58-
->method('getParent')
59-
->willReturn(null)
60-
;
61-
$resolvedFormType
62-
->expects($this->any())
63-
->method('getInnerType')
64-
->willReturn(new FormType())
65-
;
66-
$resolvedFormType
67-
->expects($this->any())
68-
->method('getTypeExtensions')
69-
->willReturn(array())
70-
;
71-
72-
$formRegistry = $this->getMockBuilder(FormRegistryInterface::class)->getMock();
73-
$formRegistry
74-
->expects($this->any())
75-
->method('getType')
76-
->will($this->returnValue($resolvedFormType))
77-
;
78-
79-
$command = new DebugCommand($formRegistry);
113+
$formRegistry = new FormRegistry(array(), new ResolvedFormTypeFactory());
114+
$command = null === $namespaces ? new DebugCommand($formRegistry) : new DebugCommand($formRegistry, $namespaces);
80115
$application = new Application();
81116
$application->add($command);
82117

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Fixtures\Debug\A;
13+
14+
use Symfony\Component\Form\AbstractType;
15+
16+
class AmbiguousType extends AbstractType
17+
{
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Fixtures\Debug\B;
13+
14+
use Symfony\Component\Form\AbstractType;
15+
16+
class AmbiguousType extends AbstractType
17+
{
18+
}

0 commit comments

Comments
 (0)
0