Skip to content

Commit 246cbd4

Browse files
authored
test: refactor tests on BaseCommand and Commands (#10103)
1 parent 1af911c commit 246cbd4

File tree

12 files changed

+369
-389
lines changed

12 files changed

+369
-389
lines changed

system/CLI/Commands.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ public function run(string $command, array $params)
6464
return EXIT_ERROR;
6565
}
6666

67-
// The file would have already been loaded during the
68-
// createCommandList function...
6967
$className = $this->commands[$command]['class'];
7068
$class = new $className($this->logger, $this);
7169

@@ -104,14 +102,10 @@ public function discoverCommands()
104102
$locator = service('locator');
105103
$files = $locator->listFiles('Commands/');
106104

107-
// If no matching command files were found, bail
108-
// This should never happen in unit testing.
109105
if ($files === []) {
110-
return; // @codeCoverageIgnore
106+
return;
111107
}
112108

113-
// Loop over each file checking to see if a command with that
114-
// alias exists in the class.
115109
foreach ($files as $file) {
116110
/** @var class-string<BaseCommand>|false */
117111
$className = $locator->findQualifiedNameFromPath($file);
@@ -159,21 +153,20 @@ public function verifyCommand(string $command, array $commands): bool
159153
return true;
160154
}
161155

162-
$message = lang('CLI.commandNotFound', [$command]);
156+
$message = lang('CLI.commandNotFound', [$command]);
157+
163158
$alternatives = $this->getCommandAlternatives($command, $commands);
164159

165160
if ($alternatives !== []) {
166-
if (count($alternatives) === 1) {
167-
$message .= "\n\n" . lang('CLI.altCommandSingular') . "\n ";
168-
} else {
169-
$message .= "\n\n" . lang('CLI.altCommandPlural') . "\n ";
170-
}
171-
172-
$message .= implode("\n ", $alternatives);
161+
$message = sprintf(
162+
"%s\n\n%s\n %s",
163+
$message,
164+
count($alternatives) === 1 ? lang('CLI.altCommandSingular') : lang('CLI.altCommandPlural'),
165+
implode("\n ", $alternatives),
166+
);
173167
}
174168

175169
CLI::error($message);
176-
CLI::newLine();
177170

178171
return false;
179172
}

tests/_support/Commands/AppInfo.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,40 @@
1818
use CodeIgniter\CodeIgniter;
1919
use CodeIgniter\Exceptions\RuntimeException;
2020

21-
class AppInfo extends BaseCommand
21+
/**
22+
* @internal
23+
*/
24+
final class AppInfo extends BaseCommand
2225
{
2326
protected $group = 'demo';
2427
protected $name = 'app:info';
2528
protected $arguments = ['draft' => 'unused'];
2629
protected $description = 'Displays basic application information.';
2730

28-
public function run(array $params): void
31+
public function run(array $params): int
2932
{
30-
CLI::write('CI Version: ' . CLI::color(CodeIgniter::CI_VERSION, 'red'));
33+
CLI::write(sprintf('CodeIgniter Version: %s', CodeIgniter::CI_VERSION));
34+
35+
return 0;
3136
}
3237

33-
public function bomb(): void
38+
public function bomb(): int
3439
{
3540
try {
3641
CLI::color('test', 'white', 'Background');
37-
} catch (RuntimeException $oops) {
38-
$this->showError($oops);
42+
} catch (RuntimeException $e) {
43+
$this->showError($e);
44+
45+
return 1;
3946
}
47+
48+
return 0;
4049
}
4150

42-
public function helpme(): void
51+
public function helpMe(): int
4352
{
4453
$this->call('help');
54+
55+
return 0;
4556
}
4657
}

tests/_support/Commands/ParamsReveal.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

tests/_support/_command/ListCommands.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,30 @@
1313

1414
namespace App\Commands;
1515

16+
use CodeIgniter\CLI\BaseCommand;
1617
use CodeIgniter\CLI\CLI;
17-
use CodeIgniter\Commands\ListCommands as BaseListCommands;
1818

19-
class ListCommands extends BaseListCommands
19+
/**
20+
* @internal
21+
*/
22+
final class ListCommands extends BaseCommand
2023
{
2124
/**
22-
* The group the command is lumped under
23-
* when listing commands.
24-
*
2525
* @var string
2626
*/
2727
protected $group = 'App';
2828

2929
/**
30-
* The Command's name
31-
*
3230
* @var string
3331
*/
3432
protected $name = 'list';
3533

3634
/**
37-
* the Command's short description
38-
*
3935
* @var string
4036
*/
4137
protected $description = 'This is testing to override `list` command.';
4238

4339
/**
44-
* the Command's usage
45-
*
4640
* @var string
4741
*/
4842
protected $usage = 'list';
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
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 CodeIgniter\CLI;
15+
16+
use CodeIgniter\CLI\Exceptions\CLIException;
17+
use CodeIgniter\CodeIgniter;
18+
use CodeIgniter\Log\Logger;
19+
use CodeIgniter\Test\CIUnitTestCase;
20+
use CodeIgniter\Test\StreamFilterTrait;
21+
use PHPUnit\Framework\Attributes\After;
22+
use PHPUnit\Framework\Attributes\Before;
23+
use PHPUnit\Framework\Attributes\CoversClass;
24+
use PHPUnit\Framework\Attributes\Group;
25+
use Tests\Support\Commands\AppInfo;
26+
27+
/**
28+
* @internal
29+
*/
30+
#[CoversClass(BaseCommand::class)]
31+
#[CoversClass(CLIException::class)]
32+
#[Group('Others')]
33+
final class BaseCommandTest extends CIUnitTestCase
34+
{
35+
use StreamFilterTrait;
36+
37+
#[After]
38+
#[Before]
39+
protected function resetCli(): void
40+
{
41+
CLI::reset();
42+
}
43+
44+
public function testRunCommand(): void
45+
{
46+
$command = new AppInfo(single_service('logger'), single_service('commands'));
47+
48+
$this->assertSame(0, $command->run([]));
49+
$this->assertSame(
50+
sprintf("\nCodeIgniter Version: %s\n", CodeIgniter::CI_VERSION),
51+
preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()),
52+
);
53+
}
54+
55+
public function testCallingOtherCommands(): void
56+
{
57+
$command = new AppInfo(single_service('logger'), single_service('commands'));
58+
59+
$this->assertSame(0, $command->helpMe());
60+
$this->assertStringContainsString('Displays basic usage information.', $this->getStreamFilterBuffer());
61+
}
62+
63+
public function testShowError(): void
64+
{
65+
$command = new AppInfo(single_service('logger'), single_service('commands'));
66+
67+
$this->assertSame(1, $command->bomb());
68+
$this->assertStringContainsString('[CodeIgniter\CLI\Exceptions\CLIException]', $this->getStreamFilterBuffer());
69+
$this->assertStringContainsString('Invalid "background" color: "Background".', $this->getStreamFilterBuffer());
70+
}
71+
72+
public function testShowHelp(): void
73+
{
74+
$command = new AppInfo(single_service('logger'), single_service('commands'));
75+
$command->showHelp();
76+
77+
$this->assertSame(
78+
<<<'EOT'
79+
80+
Usage:
81+
app:info [arguments]
82+
83+
Description:
84+
Displays basic application information.
85+
86+
Arguments:
87+
draft unused
88+
89+
EOT,
90+
preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()),
91+
);
92+
}
93+
94+
public function testMagicGetAndIsset(): void
95+
{
96+
$command = new AppInfo(single_service('logger'), single_service('commands'));
97+
98+
$this->assertInstanceOf(Logger::class, $command->logger);
99+
$this->assertInstanceOf(Commands::class, $command->commands);
100+
$this->assertSame('demo', $command->group);
101+
$this->assertSame('app:info', $command->name);
102+
$this->assertNull($command->foo); // @phpstan-ignore property.notFound
103+
}
104+
}

0 commit comments

Comments
 (0)