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
$ definition = new Definition ('%my-command.class% ' );
@@ -127,7 +129,7 @@ public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
127
129
{
128
130
$ container = new ContainerBuilder ();
129
131
$ container ->setResourceTracking (false );
130
- $ container ->addCompilerPass (new AddConsoleCommandPass ());
132
+ $ container ->addCompilerPass (new AddConsoleCommandPass (), PassConfig:: TYPE_BEFORE_REMOVING );
131
133
132
134
$ definition = new Definition ('Symfony\Component\Console\Tests\DependencyInjection\MyCommand ' );
133
135
$ definition ->addTag ('console.command ' );
@@ -145,7 +147,7 @@ public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
145
147
{
146
148
$ container = new ContainerBuilder ();
147
149
$ container ->setResourceTracking (false );
148
- $ container ->addCompilerPass (new AddConsoleCommandPass ());
150
+ $ container ->addCompilerPass (new AddConsoleCommandPass (), PassConfig:: TYPE_BEFORE_REMOVING );
149
151
150
152
$ definition = new Definition ('SplObjectStorage ' );
151
153
$ definition ->addTag ('console.command ' );
@@ -175,6 +177,79 @@ public function testProcessPrivateServicesWithSameCommand()
175
177
$ this ->assertTrue ($ container ->hasAlias ($ alias1 ));
176
178
$ this ->assertTrue ($ container ->hasAlias ($ alias2 ));
177
179
}
180
+
181
+ public function testProcessOnChildDefinitionWithClass ()
182
+ {
183
+ $ container = new ContainerBuilder ();
184
+ $ container ->addCompilerPass (new AddConsoleCommandPass (), PassConfig::TYPE_BEFORE_REMOVING );
185
+ $ className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand ' ;
186
+
187
+ $ parentId = 'my-parent-command ' ;
188
+ $ childId = 'my-child-command ' ;
189
+
190
+ $ parentDefinition = new Definition (/* no class */ );
191
+ $ parentDefinition ->setAbstract (true )->setPublic (false );
192
+
193
+ $ childDefinition = new ChildDefinition ($ parentId );
194
+ $ childDefinition ->addTag ('console.command ' )->setPublic (true );
195
+ $ childDefinition ->setClass ($ className );
196
+
197
+ $ container ->setDefinition ($ parentId , $ parentDefinition );
198
+ $ container ->setDefinition ($ childId , $ childDefinition );
199
+
200
+ $ container ->compile ();
201
+ $ command = $ container ->get ($ childId );
202
+
203
+ $ this ->assertInstanceOf ($ className , $ command );
204
+ }
205
+
206
+ public function testProcessOnChildDefinitionWithParentClass ()
207
+ {
208
+ $ container = new ContainerBuilder ();
209
+ $ container ->addCompilerPass (new AddConsoleCommandPass (), PassConfig::TYPE_BEFORE_REMOVING );
210
+ $ className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand ' ;
211
+
212
+ $ parentId = 'my-parent-command ' ;
213
+ $ childId = 'my-child-command ' ;
214
+
215
+ $ parentDefinition = new Definition ($ className );
216
+ $ parentDefinition ->setAbstract (true )->setPublic (false );
217
+
218
+ $ childDefinition = new ChildDefinition ($ parentId );
219
+ $ childDefinition ->addTag ('console.command ' )->setPublic (true );
220
+
221
+ $ container ->setDefinition ($ parentId , $ parentDefinition );
222
+ $ container ->setDefinition ($ childId , $ childDefinition );
223
+
224
+ $ container ->compile ();
225
+ $ command = $ container ->get ($ childId );
226
+
227
+ $ this ->assertInstanceOf ($ className , $ command );
228
+ }
229
+
230
+ /**
231
+ * @expectedException \RuntimeException
232
+ * @expectedExceptionMessage The definition for "my-child-command" has no class.
233
+ */
234
+ public function testProcessOnChildDefinitionWithoutClass ()
235
+ {
236
+ $ container = new ContainerBuilder ();
237
+ $ container ->addCompilerPass (new AddConsoleCommandPass (), PassConfig::TYPE_BEFORE_REMOVING );
238
+
239
+ $ parentId = 'my-parent-command ' ;
240
+ $ childId = 'my-child-command ' ;
241
+
242
+ $ parentDefinition = new Definition ();
243
+ $ parentDefinition ->setAbstract (true )->setPublic (false );
244
+
245
+ $ childDefinition = new ChildDefinition ($ parentId );
246
+ $ childDefinition ->addTag ('console.command ' )->setPublic (true );
247
+
248
+ $ container ->setDefinition ($ parentId , $ parentDefinition );
249
+ $ container ->setDefinition ($ childId , $ childDefinition );
250
+
251
+ $ container ->compile ();
252
+ }
178
253
}
179
254
180
255
class MyCommand extends Command
0 commit comments