Skip to content

Commit 65c727c

Browse files
committed
chore: add some tests for CliApp
1 parent 2987de9 commit 65c727c

File tree

3 files changed

+86
-9
lines changed

3 files changed

+86
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ $arrArg = $fs->getArg('arrArg'); // array{"arr0", "arr1"}
226226

227227
-----------
228228

229-
## Create simple cmd or app
229+
## Build simple cli app
230230

231231
In the pflag, built in `CliApp` and `CliCmd` for quick create and run an simple console application.
232232

src/CliApp.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,21 @@
99
use Toolkit\Cli\Cli;
1010
use Toolkit\Cli\Color;
1111
use Toolkit\Stdlib\Arr;
12-
use Toolkit\Stdlib\Str;
1312
use function array_merge;
1413
use function array_shift;
1514
use function basename;
1615
use function class_exists;
1716
use function function_exists;
1817
use function getcwd;
19-
use function implode;
2018
use function is_array;
2119
use function is_callable;
2220
use function is_object;
2321
use function is_string;
2422
use function ksort;
2523
use function method_exists;
26-
use function rtrim;
2724
use function str_pad;
2825
use function strlen;
29-
use function strpos;
3026
use function ucfirst;
31-
use function vdump;
3227

3328
/**
3429
* class CliApp
@@ -37,8 +32,10 @@
3732
*/
3833
class CliApp
3934
{
35+
// use AutoConfigTrait;
36+
4037
/** @var self|null */
41-
public static $global;
38+
private static $global;
4239

4340
private const COMMAND_CONFIG = [
4441
'desc' => '',
@@ -396,7 +393,7 @@ public function addCommand(string $command, $handler, array $config = []): void
396393
}
397394

398395
/**
399-
* @param array $commands
396+
* @param array<string, array{desc: string, handler:callable, options:array, arguments:array}> $commands
400397
*
401398
* @throws InvalidArgumentException
402399
*/
@@ -450,6 +447,16 @@ public function displayCommands(string $err = ''): void
450447
Cli::println($help);
451448
}
452449

450+
/**
451+
* @param string $command
452+
*
453+
* @return bool
454+
*/
455+
public function hasCommand(string $command): bool
456+
{
457+
return isset($this->commands[$command]);
458+
}
459+
453460
/****************************************************************************
454461
* getter/setter methods
455462
****************************************************************************/
@@ -462,12 +469,20 @@ public function getScriptFile(): string
462469
return $this->scriptFile;
463470
}
464471

472+
/**
473+
* @return string
474+
*/
475+
public function getBinName(): string
476+
{
477+
return $this->scriptName;
478+
}
479+
465480
/**
466481
* @return string
467482
*/
468483
public function getScriptName(): string
469484
{
470-
return basename($this->scriptFile);
485+
return $this->scriptName;
471486
}
472487

473488
/**

test/CliAppTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\PFlagTest;
4+
5+
use Toolkit\PFlag\CliApp;
6+
use Toolkit\Stdlib\Obj\DataObject;
7+
8+
/**
9+
* class CliAppTest
10+
*
11+
* @author inhere
12+
*/
13+
class CliAppTest extends BaseFlagsTestCase
14+
{
15+
private function initApp(CliApp $app): DataObject
16+
{
17+
$buf = DataObject::new();
18+
$app->add('test1', fn() => $buf->set('key', 'in test1'));
19+
$app->addCommands([
20+
'test2' => [
21+
'desc' => 'desc for test2 command',
22+
'handler' => fn() => $buf->set('key', 'in test2'),
23+
'options' => [
24+
'opt1' => 'string;a string opt1 for command test2',
25+
'opt2' => 'int;a int opt2 for command test2',
26+
],
27+
],
28+
]);
29+
30+
return $buf;
31+
}
32+
33+
public function testCliApp_basic(): void
34+
{
35+
$app = CliApp::global();
36+
$app->setScriptFile('/path/myapp');
37+
38+
$this->assertEquals('/path/myapp', $app->getScriptFile());
39+
$this->assertEquals('myapp', $app->getBinName());
40+
$this->assertEquals('myapp', $app->getScriptName());
41+
$this->assertFalse($app->hasCommand('test1'));
42+
43+
$buf = $this->initApp($app);
44+
45+
$this->assertTrue($app->hasCommand('test1'));
46+
$this->assertTrue($app->hasCommand('test2'));
47+
48+
$app->runByArgs(['test1']);
49+
$this->assertEquals('in test1', $buf->get('key'));
50+
}
51+
52+
public function testCliApp_showHelp(): void
53+
{
54+
$app = new CliApp();
55+
$this->initApp($app);
56+
57+
$this->assertTrue($app->hasCommand('test1'));
58+
$this->assertTrue($app->hasCommand('test2'));
59+
60+
$app->runByArgs(['-h']);
61+
}
62+
}

0 commit comments

Comments
 (0)