12
12
namespace Symfony \Component \Console \Tests \DependencyInjection ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \Console \Command \Command ;
15
16
use Symfony \Component \Console \CommandLoader \ContainerCommandLoader ;
16
17
use Symfony \Component \Console \DependencyInjection \AddConsoleCommandPass ;
17
- use Symfony \Component \Console \Command \Command ;
18
18
use Symfony \Component \DependencyInjection \Argument \ServiceClosureArgument ;
19
+ use Symfony \Component \DependencyInjection \ChildDefinition ;
20
+ use Symfony \Component \DependencyInjection \Compiler \PassConfig ;
19
21
use Symfony \Component \DependencyInjection \ContainerBuilder ;
20
22
use Symfony \Component \DependencyInjection \Definition ;
21
23
use Symfony \Component \DependencyInjection \TypedReference ;
@@ -28,7 +30,7 @@ class AddConsoleCommandPassTest extends TestCase
28
30
public function testProcess ($ public )
29
31
{
30
32
$ container = new ContainerBuilder ();
31
- $ container ->addCompilerPass (new AddConsoleCommandPass ());
33
+ $ container ->addCompilerPass (new AddConsoleCommandPass (), PassConfig:: TYPE_BEFORE_REMOVING );
32
34
$ container ->setParameter ('my-command.class ' , 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand ' );
33
35
34
36
$ id = 'my-command ' ;
@@ -124,7 +126,7 @@ public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
124
126
{
125
127
$ container = new ContainerBuilder ();
126
128
$ container ->setResourceTracking (false );
127
- $ container ->addCompilerPass (new AddConsoleCommandPass ());
129
+ $ container ->addCompilerPass (new AddConsoleCommandPass (), PassConfig:: TYPE_BEFORE_REMOVING );
128
130
129
131
$ definition = new Definition ('Symfony\Component\Console\Tests\DependencyInjection\MyCommand ' );
130
132
$ definition ->addTag ('console.command ' );
@@ -142,7 +144,7 @@ public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
142
144
{
143
145
$ container = new ContainerBuilder ();
144
146
$ container ->setResourceTracking (false );
145
- $ container ->addCompilerPass (new AddConsoleCommandPass ());
147
+ $ container ->addCompilerPass (new AddConsoleCommandPass (), PassConfig:: TYPE_BEFORE_REMOVING );
146
148
147
149
$ definition = new Definition ('SplObjectStorage ' );
148
150
$ definition ->addTag ('console.command ' );
@@ -171,6 +173,79 @@ public function testProcessPrivateServicesWithSameCommand()
171
173
$ this ->assertTrue ($ container ->hasAlias ($ aliasPrefix .'my-command1 ' ));
172
174
$ this ->assertTrue ($ container ->hasAlias ($ aliasPrefix .'my-command2 ' ));
173
175
}
176
+
177
+ public function testProcessOnChildDefinitionWithClass ()
178
+ {
179
+ $ container = new ContainerBuilder ();
180
+ $ container ->addCompilerPass (new AddConsoleCommandPass (), PassConfig::TYPE_BEFORE_REMOVING );
181
+ $ className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand ' ;
182
+
183
+ $ parentId = 'my-parent-command ' ;
184
+ $ childId = 'my-child-command ' ;
185
+
186
+ $ parentDefinition = new Definition (/* no class */ );
187
+ $ parentDefinition ->setAbstract (true )->setPublic (false );
188
+
189
+ $ childDefinition = new ChildDefinition ($ parentId );
190
+ $ childDefinition ->addTag ('console.command ' )->setPublic (true );
191
+ $ childDefinition ->setClass ($ className );
192
+
193
+ $ container ->setDefinition ($ parentId , $ parentDefinition );
194
+ $ container ->setDefinition ($ childId , $ childDefinition );
195
+
196
+ $ container ->compile ();
197
+ $ command = $ container ->get ($ childId );
198
+
199
+ $ this ->assertInstanceOf ($ className , $ command );
200
+ }
201
+
202
+ public function testProcessOnChildDefinitionWithParentClass ()
203
+ {
204
+ $ container = new ContainerBuilder ();
205
+ $ container ->addCompilerPass (new AddConsoleCommandPass (), PassConfig::TYPE_BEFORE_REMOVING );
206
+ $ className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand ' ;
207
+
208
+ $ parentId = 'my-parent-command ' ;
209
+ $ childId = 'my-child-command ' ;
210
+
211
+ $ parentDefinition = new Definition ($ className );
212
+ $ parentDefinition ->setAbstract (true )->setPublic (false );
213
+
214
+ $ childDefinition = new ChildDefinition ($ parentId );
215
+ $ childDefinition ->addTag ('console.command ' )->setPublic (true );
216
+
217
+ $ container ->setDefinition ($ parentId , $ parentDefinition );
218
+ $ container ->setDefinition ($ childId , $ childDefinition );
219
+
220
+ $ container ->compile ();
221
+ $ command = $ container ->get ($ childId );
222
+
223
+ $ this ->assertInstanceOf ($ className , $ command );
224
+ }
225
+
226
+ /**
227
+ * @expectedException \RuntimeException
228
+ * @expectedExceptionMessage The definition for "my-child-command" has no class.
229
+ */
230
+ public function testProcessOnChildDefinitionWithoutClass ()
231
+ {
232
+ $ container = new ContainerBuilder ();
233
+ $ container ->addCompilerPass (new AddConsoleCommandPass (), PassConfig::TYPE_BEFORE_REMOVING );
234
+
235
+ $ parentId = 'my-parent-command ' ;
236
+ $ childId = 'my-child-command ' ;
237
+
238
+ $ parentDefinition = new Definition ();
239
+ $ parentDefinition ->setAbstract (true )->setPublic (false );
240
+
241
+ $ childDefinition = new ChildDefinition ($ parentId );
242
+ $ childDefinition ->addTag ('console.command ' )->setPublic (true );
243
+
244
+ $ container ->setDefinition ($ parentId , $ parentDefinition );
245
+ $ container ->setDefinition ($ childId , $ childDefinition );
246
+
247
+ $ container ->compile ();
248
+ }
174
249
}
175
250
176
251
class MyCommand extends Command
0 commit comments