Skip to content

Commit 60fb195

Browse files
committed
add tests to AbstractCommand
1 parent 64668b7 commit 60fb195

13 files changed

Lines changed: 1246 additions & 3 deletions

system/CLI/BaseCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ abstract public function run(array $params);
116116
*/
117117
protected function call(string $command, array $params = [])
118118
{
119-
return $this->commands->run($command, $params);
119+
return $this->commands->runLegacy($command, $params);
120120
}
121121

122122
/**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Commands\Legacy;
15+
16+
use CodeIgniter\CLI\BaseCommand;
17+
18+
/**
19+
* Test fixture only. Exercises that a legacy `BaseCommand` can invoke a modern
20+
* `AbstractCommand` via {@see \CodeIgniter\CLI\Commands::runCommand()}. Not a
21+
* pattern to follow in application code — migrate legacy commands to
22+
* `AbstractCommand` instead.
23+
*/
24+
final class HelpLegacyCommand extends BaseCommand
25+
{
26+
protected $group = 'Fixtures';
27+
protected $name = 'help:legacy';
28+
protected $description = 'Legacy command to call the help command.';
29+
30+
public function run(array $params): int
31+
{
32+
return $this->commands->runCommand('help', [], []);
33+
}
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Commands\Legacy;
15+
16+
use CodeIgniter\CLI\BaseCommand;
17+
18+
/**
19+
* @internal
20+
*/
21+
final class NullReturningCommand extends BaseCommand
22+
{
23+
protected $group = 'Fixtures';
24+
protected $name = 'null:return';
25+
protected $description = 'A command that returns null.';
26+
27+
public function run(array $params)
28+
{
29+
return null;
30+
}
31+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Commands\Modern;
15+
16+
use CodeIgniter\CLI\AbstractCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
18+
use CodeIgniter\CLI\CLI;
19+
use CodeIgniter\CLI\Input\Argument;
20+
use CodeIgniter\CLI\Input\Option;
21+
use CodeIgniter\CodeIgniter;
22+
use CodeIgniter\Exceptions\RuntimeException;
23+
24+
#[Command(name: 'app:about', description: 'Displays basic application information.', group: 'Fixtures')]
25+
final class AppAboutCommand extends AbstractCommand
26+
{
27+
protected function configure(): void
28+
{
29+
$this
30+
->addArgument(new Argument(name: 'required', description: 'Unused required argument.', required: true))
31+
->addArgument(new Argument(name: 'optional', description: 'Unused optional argument.', default: 'val'))
32+
->addArgument(new Argument(name: 'array', description: 'Unused array argument.', isArray: true, default: ['a', 'b']))
33+
->addOption(new Option(name: 'foo', shortcut: 'f', description: 'Option that requires a value.', requiresValue: true, default: 'qux'))
34+
->addOption(new Option(name: 'bar', shortcut: 'a', description: 'Option that optionally accepts a value.', acceptsValue: true))
35+
->addOption(new Option(name: 'baz', shortcut: 'b', description: 'Option that allows multiple values.', requiresValue: true, isArray: true, default: ['a']))
36+
->addOption(new Option(name: 'quux', description: 'Negatable option.', negatable: true, default: false))
37+
->addUsage('app:about required-value');
38+
}
39+
40+
protected function execute(array $arguments, array $options): int
41+
{
42+
CLI::write(sprintf('CodeIgniter Version: %s', CLI::color(CodeIgniter::CI_VERSION, 'red')));
43+
44+
return EXIT_SUCCESS;
45+
}
46+
47+
public function bomb(): int
48+
{
49+
try {
50+
CLI::color('test', 'white', 'Background');
51+
52+
return EXIT_SUCCESS;
53+
} catch (RuntimeException $e) {
54+
$this->renderThrowable($e);
55+
56+
return EXIT_ERROR;
57+
}
58+
}
59+
60+
public function helpMe(): int
61+
{
62+
return $this->call('help');
63+
}
64+
65+
/**
66+
* @param array<string, list<string|null>|string|null>|null $options
67+
*/
68+
public function callHasUnboundOption(string $name, ?array $options = null): bool
69+
{
70+
return $this->hasUnboundOption($name, $options);
71+
}
72+
73+
/**
74+
* @param array<string, list<string|null>|string|null>|null $options
75+
* @param list<string|null>|string|null $default
76+
*
77+
* @return list<string|null>|string|null
78+
*/
79+
public function callGetUnboundOption(string $name, ?array $options = null, array|string|null $default = null): array|string|null
80+
{
81+
return $this->getUnboundOption($name, $options, $default);
82+
}
83+
84+
/**
85+
* @return list<string>
86+
*/
87+
public function callGetUnboundArguments(): array
88+
{
89+
return $this->getUnboundArguments();
90+
}
91+
92+
public function callGetUnboundArgument(int $index): string
93+
{
94+
return $this->getUnboundArgument($index);
95+
}
96+
97+
/**
98+
* @return array<string, list<string|null>|string|null>
99+
*/
100+
public function callGetUnboundOptions(): array
101+
{
102+
return $this->getUnboundOptions();
103+
}
104+
105+
/**
106+
* @return array<string, list<string>|string>
107+
*/
108+
public function callGetValidatedArguments(): array
109+
{
110+
return $this->getValidatedArguments();
111+
}
112+
113+
/**
114+
* @return list<string>|string
115+
*/
116+
public function callGetValidatedArgument(string $name): array|string
117+
{
118+
return $this->getValidatedArgument($name);
119+
}
120+
121+
/**
122+
* @return array<string, bool|list<string>|string|null>
123+
*/
124+
public function callGetValidatedOptions(): array
125+
{
126+
return $this->getValidatedOptions();
127+
}
128+
129+
/**
130+
* @return bool|list<string>|string|null
131+
*/
132+
public function callGetValidatedOption(string $name): array|bool|string|null
133+
{
134+
return $this->getValidatedOption($name);
135+
}
136+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Commands\Modern;
15+
16+
use CodeIgniter\CLI\AbstractCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
18+
use CodeIgniter\CLI\Input\Argument;
19+
use CodeIgniter\CLI\Input\Option;
20+
21+
#[Command(name: 'test:interact', description: 'Fixture that mutates arguments and options in interact().', group: 'Fixtures')]
22+
final class InteractFixtureCommand extends AbstractCommand
23+
{
24+
/**
25+
* @var array<string, list<string>|string>
26+
*/
27+
public array $executedArguments = [];
28+
29+
/**
30+
* @var array<string, bool|list<string>|string|null>
31+
*/
32+
public array $executedOptions = [];
33+
34+
protected function configure(): void
35+
{
36+
$this
37+
->addArgument(new Argument(name: 'name', default: 'anonymous'))
38+
->addOption(new Option(name: 'force'));
39+
}
40+
41+
protected function interact(array &$arguments, array &$options): void
42+
{
43+
// Supply a positional argument the caller omitted.
44+
if ($arguments === []) {
45+
$arguments[] = 'from-interact';
46+
}
47+
48+
// Simulate the `--force` flag being passed so execute() sees it bound to `true`.
49+
$options['force'] = null;
50+
}
51+
52+
protected function execute(array $arguments, array $options): int
53+
{
54+
$this->executedArguments = $arguments;
55+
$this->executedOptions = $options;
56+
57+
return EXIT_SUCCESS;
58+
}
59+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Commands\Modern;
15+
16+
use CodeIgniter\CLI\AbstractCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
18+
19+
#[Command(name: 'test:fixture', group: 'Fixtures', description: 'A command used as a fixture for testing purposes.')]
20+
final class TestFixtureCommand extends AbstractCommand
21+
{
22+
protected function execute(array $arguments, array $options): int
23+
{
24+
return EXIT_SUCCESS;
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\InvalidCommands;
15+
16+
use CodeIgniter\CLI\AbstractCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
18+
19+
#[Command('')]
20+
final class EmptyCommandName extends AbstractCommand
21+
{
22+
protected function execute(array $arguments, array $options): int
23+
{
24+
return EXIT_SUCCESS;
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\InvalidCommands;
15+
16+
use CodeIgniter\CLI\AbstractCommand;
17+
18+
final class NoAttributeCommand extends AbstractCommand
19+
{
20+
protected function execute(array $arguments, array $options): int
21+
{
22+
return EXIT_SUCCESS;
23+
}
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace App\Commands;
15+
16+
use CodeIgniter\CLI\AbstractCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
18+
use CodeIgniter\CLI\CLI;
19+
use CodeIgniter\CLI\Input\Argument;
20+
21+
#[Command(name: 'app:about', description: 'This is testing to override `app:about` command.', group: 'App')]
22+
final class AppAboutCommand extends AbstractCommand
23+
{
24+
protected function configure(): void
25+
{
26+
$this->addArgument(new Argument(name: 'unused', description: 'This argument is not used.', required: true));
27+
}
28+
29+
protected function execute(array $arguments, array $options): int
30+
{
31+
CLI::write('This is ' . self::class);
32+
33+
return EXIT_SUCCESS;
34+
}
35+
}

0 commit comments

Comments
 (0)