|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +use Contributte\Console\Application; |
| 4 | +use Contributte\Console\DI\ConsoleExtension; |
| 5 | +use Contributte\Tester\Toolkit; |
| 6 | +use Contributte\Tester\Utils\ContainerBuilder; |
| 7 | +use Contributte\Tester\Utils\Neonkit; |
| 8 | +use Nette\DI\Compiler; |
| 9 | +use Symfony\Component\Console\Command\Command; |
| 10 | +use Symfony\Component\Console\Exception\CommandNotFoundException; |
| 11 | +use Tester\Assert; |
| 12 | + |
| 13 | +require_once __DIR__ . '/../../bootstrap.php'; |
| 14 | + |
| 15 | +// console.command |
| 16 | +Toolkit::test(function (): void { |
| 17 | + $container = ContainerBuilder::of() |
| 18 | + ->withCompiler(function (Compiler $compiler): void { |
| 19 | + $compiler->addExtension('console', new ConsoleExtension(true)); |
| 20 | + $compiler->addConfig(Neonkit::load(<<<'NEON' |
| 21 | + console: |
| 22 | + services: |
| 23 | + foo: |
| 24 | + class: Tests\Fixtures\FooCommand |
| 25 | + tags: [console.command: app:foo] |
| 26 | + bar: |
| 27 | + class: Tests\Fixtures\BarCommand |
| 28 | + tags: [console.command: {name: app:bar}] |
| 29 | + NEON |
| 30 | + )); |
| 31 | + })->build(); |
| 32 | + |
| 33 | + $application = $container->getByType(Application::class); |
| 34 | + assert($application instanceof Application); |
| 35 | + |
| 36 | + Assert::count(2, $container->findByType(Command::class)); |
| 37 | + Assert::same(['help', 'list', '_complete', 'completion', 'app:foo', 'app:bar'], array_keys($application->all())); |
| 38 | +}); |
| 39 | + |
| 40 | +// try to set command other name |
| 41 | +Toolkit::test(function (): void { |
| 42 | + $container = ContainerBuilder::of() |
| 43 | + ->withCompiler(function (Compiler $compiler): void { |
| 44 | + $compiler->addExtension('console', new ConsoleExtension(true)); |
| 45 | + $compiler->addConfig(Neonkit::load(<<<'NEON' |
| 46 | + console: |
| 47 | + services: |
| 48 | + foo: |
| 49 | + class: Tests\Fixtures\FooCommand |
| 50 | + tags: [console.command: fake] |
| 51 | + NEON |
| 52 | + )); |
| 53 | + })->build(); |
| 54 | + |
| 55 | + $application = $container->getByType(Application::class); |
| 56 | + assert($application instanceof Application); |
| 57 | + |
| 58 | + Assert::exception( |
| 59 | + fn () => $application->all(), |
| 60 | + CommandNotFoundException::class, |
| 61 | + 'The "fake" command cannot be found because it is registered under multiple names. Make sure you don\'t set a different name via constructor or "setName()".' |
| 62 | + ); |
| 63 | +}); |
0 commit comments