Skip to content

Commit

Permalink
chore: add more tests for run group commadn
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 25, 2021
1 parent 1119e7f commit cce9850
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
11 changes: 5 additions & 6 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use function implode;
use function is_object;
use function is_string;
use function method_exists;
use function str_replace;
use function strlen;
use function strpos;
Expand Down Expand Up @@ -404,14 +403,13 @@ protected function createController(array $info): Controller
$handler = $info['handler']; // The controller class or object
if (is_string($handler)) {
$class = $handler;
if (!class_exists($class)) {
Helper::throwInvalidArgument('The console controller class [%s] not exists!', $class);
}
Assert::isTrue(class_exists($class), "The console controller class '$class' not exists!");

$handler = new $class($this->input, $this->output);
// create group object
$handler = new $class();
}

if (!($handler instanceof Controller)) {
if (!$handler instanceof Controller) {
Helper::throwInvalidArgument(
'The console controller class [%s] must instanceof the %s',
$handler,
Expand All @@ -422,6 +420,7 @@ protected function createController(array $info): Controller
// force set name and description
$handler::setName($group);
$handler->setApp($this);
$handler->setInputOutput($this->input, $this->output);

// set input name
if ($inputName = $info['name'] ?? '') {
Expand Down
23 changes: 18 additions & 5 deletions test/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function testAddCommandError(): void
}
}

public function testRunCommand_class(): void
public function testAddCommand_class_run(): void
{
$app = $this->newApp([
'./app',
Expand All @@ -101,7 +101,7 @@ public function testRunCommand_class(): void
self::assertSame('Inhere\ConsoleTest\TestCommand::execute', $ret);
}

public function testRunCommand_callback(): void
public function testAddCommand_callback_run(): void
{
$app = $this->newApp([
'./app',
Expand All @@ -116,14 +116,14 @@ public function testRunCommand_callback(): void
self::assertSame('hello', $ret);
}

public function testRun_Command_object(): void
public function testAddCommand_object_run(): void
{
$app = $this->newApp([
'./app',
'test'
]);

$app->addCommand('test', new TestCommand());
$app->command('test', new TestCommand());

$ret = $app->run(false);
self::assertSame('Inhere\ConsoleTest\TestCommand::execute', $ret);
Expand Down Expand Up @@ -165,7 +165,7 @@ public function testAddControllerError(): void
}
}

public function testRunController(): void
public function testAdd_Controller_class_Run(): void
{
$app = $this->newApp([
'./app',
Expand All @@ -178,6 +178,19 @@ public function testRunController(): void
self::assertSame('Inhere\ConsoleTest\TestController::demoCommand', $ret);
}

public function testAdd_Controller_object_Run(): void
{
$app = $this->newApp([
'./app',
'test:demo'
]);

$app->controller('test', new TestController);

$ret = $app->run(false);
self::assertSame('Inhere\ConsoleTest\TestController::demoCommand', $ret);
}

public function testTriggerEvent(): void
{
$app = $this->newApp([
Expand Down
4 changes: 2 additions & 2 deletions test/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class TestController extends Controller
/**
* this is an demo command in test
*
* @return mixed
* @return string
*/
public function demoCommand(): mixed
public function demoCommand(): string
{
return __METHOD__;
}
Expand Down

0 comments on commit cce9850

Please sign in to comment.