11
11
12
12
namespace Symfony \Bundle \FrameworkBundle \Tests \Console ;
13
13
14
- use Symfony \Bundle \FrameworkBundle \Tests \TestCase ;
15
14
use Symfony \Bundle \FrameworkBundle \Console \Application ;
15
+ use Symfony \Bundle \FrameworkBundle \Tests \TestCase ;
16
+ use Symfony \Component \Console \Command \Command ;
16
17
use Symfony \Component \Console \Input \ArrayInput ;
17
18
use Symfony \Component \Console \Output \NullOutput ;
18
19
use Symfony \Component \Console \Tester \ApplicationTester ;
@@ -23,7 +24,7 @@ public function testBundleInterfaceImplementation()
23
24
{
24
25
$ bundle = $ this ->getMock ('Symfony\Component\HttpKernel\Bundle\BundleInterface ' );
25
26
26
- $ kernel = $ this ->getKernel (array ($ bundle ));
27
+ $ kernel = $ this ->getKernel (array ($ bundle ), true );
27
28
28
29
$ application = new Application ($ kernel );
29
30
$ application ->doRun (new ArrayInput (array ('list ' )), new NullOutput ());
@@ -34,10 +35,73 @@ public function testBundleCommandsAreRegistered()
34
35
$ bundle = $ this ->getMock ('Symfony\Component\HttpKernel\Bundle\Bundle ' );
35
36
$ bundle ->expects ($ this ->once ())->method ('registerCommands ' );
36
37
37
- $ kernel = $ this ->getKernel (array ($ bundle ));
38
+ $ kernel = $ this ->getKernel (array ($ bundle ), true );
38
39
39
40
$ application = new Application ($ kernel );
40
41
$ application ->doRun (new ArrayInput (array ('list ' )), new NullOutput ());
42
+
43
+ // Calling twice: registration should only be done once.
44
+ $ application ->doRun (new ArrayInput (array ('list ' )), new NullOutput ());
45
+ }
46
+
47
+ public function testBundleCommandsAreRetrievable ()
48
+ {
49
+ $ bundle = $ this ->getMock ('Symfony\Component\HttpKernel\Bundle\Bundle ' );
50
+ $ bundle ->expects ($ this ->once ())->method ('registerCommands ' );
51
+
52
+ $ kernel = $ this ->getKernel (array ($ bundle ));
53
+
54
+ $ application = new Application ($ kernel );
55
+ $ application ->all ();
56
+
57
+ // Calling twice: registration should only be done once.
58
+ $ application ->all ();
59
+ }
60
+
61
+ public function testBundleSingleCommandIsRetrievable ()
62
+ {
63
+ $ bundle = $ this ->getMock ('Symfony\Component\HttpKernel\Bundle\Bundle ' );
64
+ $ bundle ->expects ($ this ->once ())->method ('registerCommands ' );
65
+
66
+ $ kernel = $ this ->getKernel (array ($ bundle ));
67
+
68
+ $ application = new Application ($ kernel );
69
+
70
+ $ command = new Command ('example ' );
71
+ $ application ->add ($ command );
72
+
73
+ $ this ->assertSame ($ command , $ application ->get ('example ' ));
74
+ }
75
+
76
+ public function testBundleCommandCanBeFound ()
77
+ {
78
+ $ bundle = $ this ->getMock ('Symfony\Component\HttpKernel\Bundle\Bundle ' );
79
+ $ bundle ->expects ($ this ->once ())->method ('registerCommands ' );
80
+
81
+ $ kernel = $ this ->getKernel (array ($ bundle ));
82
+
83
+ $ application = new Application ($ kernel );
84
+
85
+ $ command = new Command ('example ' );
86
+ $ application ->add ($ command );
87
+
88
+ $ this ->assertSame ($ command , $ application ->find ('example ' ));
89
+ }
90
+
91
+ public function testBundleCommandCanBeFoundByAlias ()
92
+ {
93
+ $ bundle = $ this ->getMock ('Symfony\Component\HttpKernel\Bundle\Bundle ' );
94
+ $ bundle ->expects ($ this ->once ())->method ('registerCommands ' );
95
+
96
+ $ kernel = $ this ->getKernel (array ($ bundle ));
97
+
98
+ $ application = new Application ($ kernel );
99
+
100
+ $ command = new Command ('example ' );
101
+ $ command ->setAliases (array ('alias ' ));
102
+ $ application ->add ($ command );
103
+
104
+ $ this ->assertSame ($ command , $ application ->find ('alias ' ));
41
105
}
42
106
43
107
public function testBundleCommandsHaveRightContainer ()
@@ -46,7 +110,7 @@ public function testBundleCommandsHaveRightContainer()
46
110
$ command ->setCode (function () {});
47
111
$ command ->expects ($ this ->exactly (2 ))->method ('setContainer ' );
48
112
49
- $ application = new Application ($ this ->getKernel (array ()));
113
+ $ application = new Application ($ this ->getKernel (array (), true ));
50
114
$ application ->setAutoExit (false );
51
115
$ application ->setCatchExceptions (false );
52
116
$ application ->add ($ command );
@@ -59,21 +123,23 @@ public function testBundleCommandsHaveRightContainer()
59
123
$ tester ->run (array ('command ' => 'foo ' ));
60
124
}
61
125
62
- private function getKernel (array $ bundles )
126
+ private function getKernel (array $ bundles, $ useDispatcher = false )
63
127
{
64
- $ dispatcher = $ this ->getMock ('Symfony\Component\EventDispatcher\EventDispatcherInterface ' );
65
- $ dispatcher
66
- ->expects ($ this ->atLeastOnce ())
67
- ->method ('dispatch ' )
68
- ;
69
-
70
128
$ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
71
- $ container
72
- ->expects ($ this ->atLeastOnce ())
73
- ->method ('get ' )
74
- ->with ($ this ->equalTo ('event_dispatcher ' ))
75
- ->will ($ this ->returnValue ($ dispatcher ))
76
- ;
129
+
130
+ if ($ useDispatcher ) {
131
+ $ dispatcher = $ this ->getMock ('Symfony\Component\EventDispatcher\EventDispatcherInterface ' );
132
+ $ dispatcher
133
+ ->expects ($ this ->atLeastOnce ())
134
+ ->method ('dispatch ' )
135
+ ;
136
+ $ container
137
+ ->expects ($ this ->atLeastOnce ())
138
+ ->method ('get ' )
139
+ ->with ($ this ->equalTo ('event_dispatcher ' ))
140
+ ->will ($ this ->returnValue ($ dispatcher ));
141
+ }
142
+
77
143
$ container
78
144
->expects ($ this ->once ())
79
145
->method ('hasParameter ' )
0 commit comments