13
13
14
14
use PHPUnit \Framework \TestCase ;
15
15
use Symfony \Component \Console \Application ;
16
+ use Symfony \Component \Console \Exception \InvalidArgumentException ;
16
17
use Symfony \Component \Console \Tester \CommandTester ;
17
18
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 ;
21
21
22
22
class DebugCommandTest extends TestCase
23
23
{
@@ -39,6 +39,67 @@ public function testDebugSingleFormType()
39
39
$ this ->assertContains ('Symfony\Component\Form\Extension\Core\Type\FormType (Block prefix: "form") ' , $ tester ->getDisplay ());
40
40
}
41
41
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
+
42
103
/**
43
104
* @expectedException \InvalidArgumentException
44
105
*/
@@ -47,36 +108,10 @@ public function testDebugInvalidFormType()
47
108
$ this ->createCommandTester ()->execute (array ('class ' => 'test ' ));
48
109
}
49
110
50
- /**
51
- * @return CommandTester
52
- */
53
- private function createCommandTester ()
111
+ private function createCommandTester (array $ namespaces = null )
54
112
{
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 );
80
115
$ application = new Application ();
81
116
$ application ->add ($ command );
82
117
0 commit comments