Skip to content

Commit fedcc2a

Browse files
committed
add more unit test
1 parent 59114a3 commit fedcc2a

19 files changed

+240
-177
lines changed

src/Application.php

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Application extends AbstractApplication
2323

2424
/**
2525
* Register a app group command(by controller)
26-
* @param string $name The controller name
26+
* @param string $name The controller name
2727
* @param string|Controller $class The controller class
2828
* @param null|array|string $option
2929
* @return static
@@ -34,7 +34,7 @@ public function controller(string $name, $class = null, $option = null)
3434
/** @var Controller $class */
3535
if (!$class && \class_exists($name)) {
3636
$class = $name;
37-
$name = $class::getName();
37+
$name = $class::getName();
3838
}
3939

4040
if (!$name || !$class) {
@@ -101,9 +101,9 @@ public function controllers(array $controllers)
101101

102102
/**
103103
* Register a app independent console command
104-
* @param string|Command $name
104+
* @param string|Command $name
105105
* @param string|\Closure|Command $handler
106-
* @param null|array|string $option
106+
* @param null|array|string $option
107107
* @return $this|mixed
108108
* @throws \InvalidArgumentException
109109
*/
@@ -112,26 +112,26 @@ public function command(string $name, $handler = null, $option = null)
112112
if (!$handler && \class_exists($name)) {
113113
/** @var Command $name */
114114
$handler = $name;
115-
$name = $name::getName();
115+
$name = $name::getName();
116116
}
117117

118118
if (!$name || !$handler) {
119-
throw new \InvalidArgumentException("Command 'name' and 'handler' not allowed to is empty! name: $name");
119+
Helper::throwInvalidArgument("Command 'name' and 'handler' not allowed to is empty! name: $name");
120120
}
121121

122122
$this->validateName($name);
123123

124124
if (isset($this->commands[$name])) {
125-
throw new \InvalidArgumentException("Command '$name' have been registered!");
125+
Helper::throwInvalidArgument("Command '$name' have been registered!");
126126
}
127127

128128
if (\is_string($handler)) {
129129
if (!\class_exists($handler)) {
130-
throw new \InvalidArgumentException("The console command class [$handler] not exists!");
130+
Helper::throwInvalidArgument("The console command class [$handler] not exists!");
131131
}
132132

133133
if (!is_subclass_of($handler, Command::class)) {
134-
throw new \InvalidArgumentException('The console command class must is subclass of the: ' . Command::class);
134+
Helper::throwInvalidArgument('The console command class must is subclass of the: ' . Command::class);
135135
}
136136

137137
// not enable
@@ -142,28 +142,26 @@ public function command(string $name, $handler = null, $option = null)
142142

143143
// allow define aliases in Command class by Command::aliases()
144144
if ($aliases = $handler::aliases()) {
145-
$option['aliases'] = isset($option['aliases']) ? array_merge($option['aliases'], $aliases) : $aliases;
145+
$option['aliases'] = isset($option['aliases']) ? \array_merge($option['aliases'], $aliases) : $aliases;
146146
}
147147
} elseif (!\is_object($handler) || !\method_exists($handler, '__invoke')) {
148-
throw new \InvalidArgumentException(sprintf(
148+
Helper::throwInvalidArgument(
149149
'The console command handler must is an subclass of %s OR a Closure OR a object have method __invoke()',
150150
Command::class
151-
));
151+
);
152152
}
153153

154154
// is an class name string
155155
$this->commands[$name] = $handler;
156156

157-
if (!$option) {
158-
return $this;
159-
}
160-
161157
// have option information
162-
if (\is_string($option)) {
163-
$this->setCommandMetaValue($name, 'description', $option);
164-
} elseif (\is_array($option)) {
165-
$this->addCommandAliases($name, $option['aliases'] ?? null);
166-
$this->setCommandMeta($name, $option);
158+
if ($option) {
159+
if (\is_string($option)) {
160+
$this->setCommandMetaValue($name, 'description', $option);
161+
} elseif (\is_array($option)) {
162+
$this->addCommandAliases($name, $option['aliases'] ?? null);
163+
$this->setCommandMeta($name, $option);
164+
}
167165
}
168166

169167
return $this;
@@ -179,25 +177,19 @@ public function commands(array $commands)
179177
}
180178

181179
/**
182-
* addCommand
183-
* @param string $name
184-
* @param mixed $handler
185-
* @param null|string|array $option
186-
* @return $this
187-
* @throws \InvalidArgumentException
180+
* add command
181+
* @inheritdoc
182+
* @see command()
188183
*/
189184
public function addCommand(string $name, $handler = null, $option = null): self
190185
{
191186
return $this->command($name, $handler, $option);
192187
}
193188

194189
/**
195-
* addGroup
196-
* @param string $name
197-
* @param string|null $controller
198-
* @param null|string|array $option
199-
* @return static
200-
* @throws \InvalidArgumentException
190+
* add group/controller
191+
* @inheritdoc
192+
* @see controller()
201193
*/
202194
public function addGroup(string $name, $controller = null, $option = null)
203195
{
@@ -213,7 +205,7 @@ public function addGroup(string $name, $controller = null, $option = null)
213205
*/
214206
public function registerCommands(string $namespace, string $basePath): self
215207
{
216-
$length = \strlen($basePath) + 1;
208+
$length = \strlen($basePath) + 1;
217209
$iterator = Helper::directoryIterator($basePath, $this->getFileFilter());
218210

219211
foreach ($iterator as $file) {
@@ -233,7 +225,7 @@ public function registerCommands(string $namespace, string $basePath): self
233225
*/
234226
public function registerGroups(string $namespace, string $basePath): self
235227
{
236-
$length = \strlen($basePath) + 1;
228+
$length = \strlen($basePath) + 1;
237229
$iterator = Helper::directoryIterator($basePath, $this->getFileFilter());
238230

239231
foreach ($iterator as $file) {
@@ -287,7 +279,7 @@ protected function dispatch(string $name)
287279
return $this->runCommand($realName, true);
288280
}
289281

290-
// maybe is a controller name
282+
// maybe is a controller/group name
291283
$action = '';
292284

293285
// like 'home:index'
@@ -321,15 +313,15 @@ protected function dispatch(string $name)
321313
/**
322314
* run a command
323315
* @param string $name Command name
324-
* @param bool $believable The `$name` is believable
316+
* @param bool $believable The `$name` is believable
325317
* @return mixed
326318
* @throws \InvalidArgumentException
327319
*/
328320
public function runCommand(string $name, bool $believable = false)
329321
{
330322
// if $believable = true, will skip check.
331323
if (!$believable && $this->isCommand($name)) {
332-
throw new \InvalidArgumentException("The console independent-command [$name] not exists!");
324+
Helper::throwInvalidArgument("The console independent-command [$name] not exists!");
333325
}
334326

335327
/** @var \Closure|string $handler Command class */
@@ -345,14 +337,14 @@ public function runCommand(string $name, bool $believable = false)
345337
$status = $handler($this->input, $this->output);
346338
} else {
347339
if (!\class_exists($handler)) {
348-
throw new \InvalidArgumentException("The console command class [$handler] not exists!");
340+
Helper::throwInvalidArgument("The console command class [$handler] not exists!");
349341
}
350342

351343
/** @var Command $object */
352344
$object = new $handler($this->input, $this->output);
353345

354346
if (!($object instanceof Command)) {
355-
throw new \InvalidArgumentException("The console command class [$handler] must instanceof the " . Command::class);
347+
Helper::throwInvalidArgument("The console command class [$handler] must instanceof the " . Command::class);
356348
}
357349

358350
$object::setName($name);
@@ -366,8 +358,8 @@ public function runCommand(string $name, bool $believable = false)
366358
/**
367359
* @param string $name group name
368360
* @param string $action Command method, no suffix
369-
* @param bool $believable The `$name` is believable
370-
* @param bool $standAlone
361+
* @param bool $believable The `$name` is believable
362+
* @param bool $standAlone
371363
* @return mixed
372364
* @throws \InvalidArgumentException
373365
* @throws \ReflectionException
@@ -394,7 +386,8 @@ public function runAction(string $name, string $action, bool $believable = false
394386
}
395387

396388
if (!($object instanceof Controller)) {
397-
Helper::throwInvalidArgument('The console controller class [%s] must instanceof the %s', $object, Controller::class);
389+
Helper::throwInvalidArgument('The console controller class [%s] must instanceof the %s', $object,
390+
Controller::class);
398391
}
399392

400393
$object::setName($name);

src/Base/AbstractApplication.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ public function __construct(array $meta = [], Input $input = null, Output $outpu
114114
protected function init()
115115
{
116116
$this->meta['_stats'] = [
117-
'startTime' => microtime(1),
117+
'startTime' => \microtime(1),
118118
'endTime' => 0,
119-
'startMemory' => memory_get_usage(),
119+
'startMemory' => \memory_get_usage(),
120120
'endMemory' => 0,
121121
];
122122

@@ -211,7 +211,7 @@ public function stop(int $code = 0)
211211
$this->output->aList($this->meta['_stats'], $title);
212212
}
213213

214-
exit((int)$code);
214+
exit($code);
215215
}
216216

217217
/**
@@ -370,7 +370,7 @@ protected function filterSpecialCommand(string $command)
370370
* @param bool $isGroup
371371
* @throws \InvalidArgumentException
372372
*/
373-
protected function validateName(string $name, $isGroup = false)
373+
protected function validateName(string $name, bool $isGroup = false)
374374
{
375375
$pattern = $isGroup ? '/^[a-z][\w-]+$/' : '/^[a-z][\w-]*:?([a-z][\w-]+)?$/';
376376

@@ -513,7 +513,7 @@ public function showCommandList($quit = true)
513513
\ksort($internalCommands);
514514

515515
// built in options
516-
$internalOptions = FormatUtil::alignmentOptions(self::$internalOptions);
516+
$internalOptions = FormatUtil::alignOptions(self::$internalOptions);
517517

518518
$this->output->mList([
519519
'Usage:' => "$script <info>{command}</info> [--opt -v -h ...] [arg0 arg1 arg2=value2 ...]",
@@ -806,7 +806,7 @@ public function isProfile(): bool
806806
* @param null|string $name
807807
* @return array
808808
*/
809-
public function getCommandAliases($name = null): array
809+
public function getCommandAliases(string $name = null): array
810810
{
811811
if (!$name) {
812812
return $this->commandAliases;

src/Base/AbstractCommand.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ protected function beforeExecute(): bool
226226
}
227227

228228
/**
229-
* do execute
229+
* do execute command
230230
* @param Input $input
231231
* @param Output $output
232232
* @return int|mixed
@@ -253,14 +253,13 @@ protected function showHelp(): bool
253253
// 创建了 InputDefinition , 则使用它的信息(此时不会再解析和使用命令的注释)
254254
$help = $definition->getSynopsis();
255255
$help['usage:'] = \sprintf('%s %s %s', $this->input->getScript(), $this->input->getCommand(), $help['usage:']);
256-
$help['global options:'] = FormatUtil::alignmentOptions(Application::getInternalOptions());
256+
$help['global options:'] = FormatUtil::alignOptions(Application::getInternalOptions());
257257

258258
if (empty($help[0]) && $this->isAlone()) {
259259
$help[0] = self::getDescription();
260260
}
261261

262262
$this->output->mList($help, ['sepChar' => ' ']);
263-
264263
return true;
265264
}
266265

@@ -300,6 +299,7 @@ public function validateInput(): bool
300299
}
301300

302301
$in = $this->input;
302+
$out = $this->output;
303303
$givenArgs = $errArgs = [];
304304

305305
foreach ($in->getArgs() as $key => $value) {
@@ -311,19 +311,17 @@ public function validateInput(): bool
311311
}
312312

313313
if (\count($errArgs) > 0) {
314-
$this->output->liteError(\sprintf('Unknown arguments (error: "%s").', \implode(', ', $errArgs)));
315-
314+
$out->liteError(\sprintf('Unknown arguments (error: "%s").', \implode(', ', $errArgs)));
316315
return false;
317316
}
318317

319318
$defArgs = $def->getArguments();
320319
$missingArgs = \array_filter(\array_keys($defArgs), function ($name, $key) use ($def, $givenArgs) {
321320
return !\array_key_exists($key, $givenArgs) && $def->argumentIsRequired($name);
322-
}, ARRAY_FILTER_USE_BOTH);
321+
}, \ARRAY_FILTER_USE_BOTH);
323322

324323
if (\count($missingArgs) > 0) {
325-
$this->output->liteError(\sprintf('Not enough arguments (missing: "%s").', \implode(', ', $missingArgs)));
326-
324+
$out->liteError(\sprintf('Not enough arguments (missing: "%s").', \implode(', ', $missingArgs)));
327325
return false;
328326
}
329327

@@ -347,7 +345,7 @@ public function validateInput(): bool
347345
$names = \array_keys($unknown);
348346
$first = \array_shift($names);
349347

350-
throw new \InvalidArgumentException(sprintf(
348+
throw new \InvalidArgumentException(\sprintf(
351349
'Input option is not exists (unknown: "%s").',
352350
(isset($first[1]) ? '--' : '-') . $first
353351
));
@@ -364,8 +362,8 @@ public function validateInput(): bool
364362
}
365363

366364
if (\count($missingOpts) > 0) {
367-
$this->output->liteError(
368-
\sprintf('Not enough options parameters (missing: "%s").', implode(', ', $missingOpts))
365+
$out->liteError(
366+
\sprintf('Not enough options parameters (missing: "%s").', \implode(', ', $missingOpts))
369367
);
370368

371369
return false;
@@ -515,7 +513,7 @@ protected function showHelpByMethodAnnotations(string $method, string $action =
515513
unset($help['Description:']);
516514
}
517515

518-
$help['Global Options:'] = FormatUtil::alignmentOptions(\array_merge(Application::getInternalOptions(),static::$commandOptions));
516+
$help['Global Options:'] = FormatUtil::alignOptions(\array_merge(Application::getInternalOptions(),static::$commandOptions));
519517

520518
$this->output->mList($help, [
521519
'sepChar' => ' ',

src/Command.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
* @package Inhere\Console
1717
*
1818
* ```php
19-
* In sub class:
20-
*
21-
* protected function execute($input, $output)
22-
* {
23-
* // some logic ...
24-
* }
25-
*
19+
* class MyCommand extends Command
20+
* {
21+
* protected function execute($input, $output)
22+
* {
23+
* // some logic ...
24+
* }
25+
* }
2626
* ```
2727
*/
2828
abstract class Command extends AbstractCommand implements CommandInterface

0 commit comments

Comments
 (0)