Skip to content

Commit 3727530

Browse files
committed
Tests: improvements
1 parent c7234eb commit 3727530

5 files changed

Lines changed: 108 additions & 9 deletions

File tree

src/CommandLoader/ContainerCommandLoader.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ public function __construct(Container $container, array $commandMap)
2929
* Loads a command.
3030
*
3131
* @param string $name
32-
* @return Command
3332
* @throws CommandNotFoundException
3433
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
35-
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
3634
*/
37-
public function get($name)
35+
public function get($name): Command
3836
{
3937
if (!$this->has($name)) {
4038
throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name));

tests/cases/Unit/CommandLoader/CommandLoader.phpt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@ use Contributte\Console\CommandLoader\ContainerCommandLoader;
88
use Contributte\Console\Exception\Runtime\CommandNotFoundException;
99
use Nette\DI\Container;
1010
use Tester\Assert;
11-
use Tester\Environment;
1211

1312
require_once __DIR__ . '/../../../bootstrap.php';
1413

15-
if (!interface_exists('Symfony\Component\Console\CommandLoader\CommandLoaderInterface', true)) {
16-
Environment::skip('CommandLoaderInterface is available from symfony/console 3.4');
17-
}
18-
1914
// Empty loader
2015
test(function (): void {
2116
$container = new Container();

tests/cases/Unit/DI/ConsoleExtension.lazy.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
*/
66

77
use Contributte\Console\Application;
8+
use Contributte\Console\CommandLoader\ContainerCommandLoader;
89
use Contributte\Console\DI\ConsoleExtension;
910
use Nette\DI\Compiler;
1011
use Nette\DI\Container;
1112
use Nette\DI\ContainerLoader;
1213
use Symfony\Component\Console\Command\Command;
14+
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
1315
use Tester\Assert;
1416
use Tester\FileMock;
1517
use Tests\Fixtures\FooCommand;
@@ -33,7 +35,12 @@ test(function (): void {
3335
$container = new $class();
3436

3537
Assert::type(Application::class, $container->getByType(Application::class));
38+
3639
Assert::false($container->isCreated('foo'));
3740
Assert::count(1, $container->findByType(Command::class));
3841
Assert::type(FooCommand::class, $container->getByType(Command::class));
42+
43+
$loader = $container->getByType(CommandLoaderInterface::class);
44+
Assert::type(ContainerCommandLoader::class, $loader);
45+
Assert::same(['app:foo'], $loader->getNames());
3946
});

tests/cases/Unit/DI/ConsoleExtension.phpt

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use Nette\Bridges\HttpDI\HttpExtension;
1111
use Nette\DI\Compiler;
1212
use Nette\DI\Container;
1313
use Nette\DI\ContainerLoader;
14+
use Nette\DI\MissingServiceException;
15+
use Nette\DI\ServiceCreationException;
1416
use Symfony\Component\Console\Command\Command;
1517
use Tester\Assert;
1618
use Tester\FileMock;
@@ -71,9 +73,96 @@ test(function (): void {
7173
Assert::equal('https://contributte.org/', (string) $container->getService('http.request')->getUrl());
7274
});
7375

74-
// No CLI mode
76+
// No mode provided
7577
test(function (): void {
7678
Assert::exception(function (): void {
7779
new ConsoleExtension();
7880
}, InvalidArgumentException::class, 'Provide CLI mode, e.q. Contributte\Console\DI\ConsoleExtension(%consoleMode%).');
7981
});
82+
83+
// Non-CLI mode
84+
test(function (): void {
85+
$loader = new ContainerLoader(TEMP_DIR, true);
86+
$class = $loader->load(function (Compiler $compiler): void {
87+
$compiler->addExtension('console', new ConsoleExtension(false));
88+
}, [getmypid(), 4]);
89+
90+
/** @var Container $container */
91+
$container = new $class();
92+
93+
Assert::exception(static function () use ($container): void {
94+
$container->getByType(Application::class);
95+
}, MissingServiceException::class);
96+
});
97+
98+
// Config
99+
test(function (): void {
100+
$loader = new ContainerLoader(TEMP_DIR, true);
101+
$class = $loader->load(function (Compiler $compiler): void {
102+
$compiler->addExtension('console', new ConsoleExtension(true));
103+
$compiler->loadConfig(FileMock::create('
104+
console:
105+
name: Hello world
106+
version: 1.0.0
107+
catchExceptions: false
108+
autoExit: false
109+
', 'neon'));
110+
}, [getmypid(), 5]);
111+
112+
/** @var Container $container */
113+
$container = new $class();
114+
115+
$application = $container->getByType(Application::class);
116+
Assert::type(Application::class, $application);
117+
Assert::same('Hello world', $application->getName());
118+
Assert::same('1.0.0', $application->getVersion());
119+
Assert::false($application->areExceptionsCaught());
120+
Assert::false($application->isAutoExitEnabled());
121+
});
122+
123+
// Lazy commands
124+
test(function (): void {
125+
$loader = new ContainerLoader(TEMP_DIR, true);
126+
$class = $loader->load(function (Compiler $compiler): void {
127+
$compiler->addExtension('console', new ConsoleExtension(true));
128+
$compiler->loadConfig(FileMock::create('
129+
console:
130+
lazy: true
131+
services:
132+
defaultName: Tests\Fixtures\FooCommand
133+
tagNameString:
134+
factory: Tests\Fixtures\FooCommand
135+
tags: [console.command: bar]
136+
tagNameArray:
137+
factory: Tests\Fixtures\FooCommand
138+
tags: [console.command: [name: baz]]
139+
', 'neon'));
140+
}, [getmypid(), 6]);
141+
142+
/** @var Container $container */
143+
$container = new $class();
144+
145+
$application = $container->getByType(Application::class);
146+
Assert::type(Application::class, $application);
147+
Assert::false($container->isCreated('defaultName'));
148+
Assert::count(3, $container->findByType(Command::class));
149+
Assert::true($application->has('app:foo'));
150+
Assert::true($application->has('bar'));
151+
Assert::true($application->has('baz'));
152+
});
153+
154+
// Invalid command
155+
test(function (): void {
156+
Assert::exception(function (): void {
157+
$loader = new ContainerLoader(TEMP_DIR, true);
158+
$loader->load(function (Compiler $compiler): void {
159+
$compiler->addExtension('console', new ConsoleExtension(true));
160+
$compiler->loadConfig(FileMock::create('
161+
console:
162+
lazy: true
163+
services:
164+
noName: Tests\Fixtures\NoNameCommand
165+
', 'neon'));
166+
}, [getmypid(), 7]);
167+
}, ServiceCreationException::class, 'Command "Tests\Fixtures\NoNameCommand" missing tag "console.command[name]" or variable "$defaultName".');
168+
});

tests/fixtures/NoNameCommand.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Tests\Fixtures;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
7+
final class NoNameCommand extends Command
8+
{
9+
10+
}

0 commit comments

Comments
 (0)