Skip to content

Commit 04e5fdb

Browse files
committed
fix: unit test error on subcommands
1 parent a272ae3 commit 04e5fdb

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

src/Component/Router.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
use function array_merge;
2727
use function class_exists;
2828
use function explode;
29+
use function get_class;
2930
use function in_array;
3031
use function is_int;
3132
use function is_object;
3233
use function is_string;
3334
use function is_subclass_of;
34-
use function method_exists;
3535
use function preg_match;
3636
use function strpos;
3737
use function trim;
@@ -187,9 +187,12 @@ public function addCommand(string $name, string|Closure|CommandInterface $handle
187187
$name = $name::getName();
188188
}
189189

190-
Assert::isFalse(!$name || !$handler, "Command 'name' and 'handler' cannot be empty! name: $name");
191-
Assert::isFalse(isset($this->commands[$name]), "Command '$name' have been registered!");
190+
if (!$name || !$handler) {
191+
$handlerClass = is_object($handler) ? get_class($handler) : $handler;
192+
throw new InvalidArgumentException("Command 'name' and 'handler' cannot be empty! name: $name, handler: $handlerClass");
193+
}
192194

195+
Assert::isFalse(isset($this->commands[$name]), "Command '$name' have been registered!");
193196
$this->validateName($name);
194197

195198
$config['aliases'] = isset($config['aliases']) ? (array)$config['aliases'] : [];

src/Decorate/SubCommandsWareTrait.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use function class_exists;
2727
use function count;
2828
use function explode;
29+
use function get_class;
2930
use function implode;
3031
use function in_array;
3132
use function is_int;
@@ -122,7 +123,7 @@ protected function dispatchSub(string $name, array $args): mixed
122123
// create and init sub-command
123124
$subCmd = $this->createSubCommand($subInfo);
124125
$subCmd->setParent($this);
125-
$subCmd->setApp($this->getApp());
126+
$subCmd->setApp($this->app);
126127
$subCmd->setPath($this->path);
127128
$subCmd->setInputOutput($this->input, $this->output);
128129

@@ -168,9 +169,12 @@ public function addSub(string $name, string|CommandInterface $handler = null, ar
168169
$name = $handler::getName();
169170
}
170171

171-
Assert::isFalse(!$name || !$handler, "Command 'name' and 'handler' cannot be empty! name: $name, handler: $handler");
172-
Assert::isFalse(isset($this->commands[$name]), "Command '$name' have been registered!");
172+
if (!$name || !$handler) {
173+
$handlerClass = is_object($handler) ? get_class($handler) : $handler;
174+
throw new InvalidArgumentException("Command 'name' and 'handler' cannot be empty! name: $name, handler: $handlerClass");
175+
}
173176

177+
Assert::isFalse(isset($this->commands[$name]), "Command '$name' have been registered!");
174178
$this->validateName($name);
175179

176180
$config['aliases'] = isset($config['aliases']) ? (array)$config['aliases'] : [];

src/Handler/AbstractHandler.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,9 @@
4343
*/
4444
abstract class AbstractHandler implements CommandHandlerInterface
4545
{
46-
use AttachApplicationTrait;
46+
use AttachApplicationTrait, CommandHelpTrait;
4747

48-
use CommandHelpTrait;
49-
50-
use InputOutputAwareTrait;
51-
52-
use UserInteractAwareTrait;
48+
use InputOutputAwareTrait, UserInteractAwareTrait;
5349

5450
use SubCommandsWareTrait;
5551

0 commit comments

Comments
 (0)