Skip to content

Commit 2bcbd5d

Browse files
Jirka Hrazdilf3l1x
authored andcommitted
add test for a command with an alias and for a hidden command
1 parent 14c277c commit 2bcbd5d

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

tests/Cases/DI/ConsoleExtension.phpt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use Nette\DI\ServiceCreationException;
1414
use Symfony\Component\Console\Command\Command;
1515
use Tester\Assert;
1616
use Tester\FileMock;
17+
use Tests\Fixtures\FooAliasCommand;
1718
use Tests\Fixtures\FooCommand;
19+
use Tests\Fixtures\FooHiddenCommand;
1820
use Tests\Fixtures\FooRequestFactory;
1921

2022
require_once __DIR__ . '/../../bootstrap.php';
@@ -246,3 +248,49 @@ Toolkit::test(function (): void {
246248
new $class();
247249
}, ServiceCreationException::class, 'Custom http.requestFactory is used, argument console.url should be removed.');
248250
});
251+
252+
// 1 command with aliases
253+
Toolkit::test(function (): void {
254+
$loader = new ContainerLoader(Environment::getTestDir(), true);
255+
$class = $loader->load(function (Compiler $compiler): void {
256+
$compiler->addExtension('console', new ConsoleExtension(true));
257+
$compiler->loadConfig(FileMock::create('
258+
console:
259+
services:
260+
foo: Tests\Fixtures\FooAliasCommand
261+
', 'neon'));
262+
}, [getmypid(), 13]);
263+
264+
/** @var Container $container */
265+
$container = new $class();
266+
267+
$application = $container->getByType(Application::class);
268+
Assert::type(Application::class, $application);
269+
Assert::false($container->isCreated('foo'));
270+
Assert::count(1, $container->findByType(Command::class));
271+
Assert::type(FooAliasCommand::class, $container->getByType(Command::class));
272+
$application->all();
273+
});
274+
275+
// 1 hidden command
276+
Toolkit::test(function (): void {
277+
$loader = new ContainerLoader(Environment::getTestDir(), true);
278+
$class = $loader->load(function (Compiler $compiler): void {
279+
$compiler->addExtension('console', new ConsoleExtension(true));
280+
$compiler->loadConfig(FileMock::create('
281+
console:
282+
services:
283+
foo: Tests\Fixtures\FooHiddenCommand
284+
', 'neon'));
285+
}, [getmypid(), 14]);
286+
287+
/** @var Container $container */
288+
$container = new $class();
289+
290+
$application = $container->getByType(Application::class);
291+
Assert::type(Application::class, $application);
292+
Assert::false($container->isCreated('foo'));
293+
Assert::count(1, $container->findByType(Command::class));
294+
Assert::type(FooHiddenCommand::class, $container->getByType(Command::class));
295+
$application->all();
296+
});

tests/Fixtures/FooAliasCommand.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Tests\Fixtures;
4+
5+
use Symfony\Component\Console\Attribute\AsCommand;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
#[AsCommand(
11+
name: 'app:foo',
12+
description: 'Foo command',
13+
aliases: ['foo', 'bar']
14+
)]
15+
final class FooAliasCommand extends Command
16+
{
17+
18+
protected function execute(InputInterface $input, OutputInterface $output): int
19+
{
20+
return 0;
21+
}
22+
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Tests\Fixtures;
4+
5+
use Symfony\Component\Console\Attribute\AsCommand;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
#[AsCommand(
11+
name: 'app:foo',
12+
description: 'Foo command',
13+
hidden: true
14+
)]
15+
final class FooHiddenCommand extends Command
16+
{
17+
18+
protected function execute(InputInterface $input, OutputInterface $output): int
19+
{
20+
return 0;
21+
}
22+
23+
}

0 commit comments

Comments
 (0)