Skip to content

Commit 163afd2

Browse files
committed
add phpunit config file. Enhance 'help' command
1 parent 32f3f95 commit 163afd2

File tree

9 files changed

+136
-19
lines changed

9 files changed

+136
-19
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,12 @@ $answer = Interact::limitedAsk('please input you age?', null, function($age)
690690
});
691691
```
692692

693+
## Unit testing
694+
695+
```bash
696+
phpunit
697+
```
698+
693699
## License
694700

695701
MIT

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "inhere/console",
33
"type": "library",
4-
"description": "a php console application library.",
5-
"keywords": ["library","console","console-color","command", "console-application"],
4+
"description": "a php console library, provide console argument parse,color style,user interactive, information show.",
5+
"keywords": ["library","console","console-color", "command", "command-line", "cli", "console-application"],
66
"homepage": "https://github.com/inhere/php-console",
77
"license": "MIT",
88
"authors": [
@@ -20,6 +20,11 @@
2020
"Inhere\\Console\\" : "src/"
2121
}
2222
},
23+
"autoload-dev": {
24+
"psr-4": {
25+
"Inhere\\Console\\Tests\\" : "tests/"
26+
}
27+
},
2328
"suggest": {
2429
"inhere/simple-print-tool": "Very lightweight data printing tools"
2530
}

examples/s-autoload.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@
1111

1212
spl_autoload_register(function($class)
1313
{
14+
$file = null;
15+
1416
if (0 === strpos($class,'Inhere\Console\Examples\\')) {
1517
$path = str_replace('\\', '/', substr($class, strlen('Inhere\Console\examples\\')));
1618
$file =__DIR__ . "/{$path}.php";
1719

18-
if (is_file($file)) {
19-
include $file;
20-
}
21-
20+
} elseif (0 === strpos($class,'Inhere\Console\Tests\\')) {
21+
$path = str_replace('\\', '/', substr($class, strlen('Inhere\Console\Tests\\')));
22+
$file = dirname(__DIR__) . "/tests/{$path}.php";
2223
} elseif (0 === strpos($class,'Inhere\Console\\')) {
2324
$path = str_replace('\\', '/', substr($class, strlen('Inhere\Console\\')));
2425
$file = dirname(__DIR__) . "/src/{$path}.php";
26+
}
2527

26-
if (is_file($file)) {
27-
include $file;
28-
}
28+
if ($file && is_file($file)) {
29+
include $file;
2930
}
3031
});

phpunit.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
bootstrap="./tests/boot.php"
6+
colors="false"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Php Console Test Suite">
15+
<directory>./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<filter>
20+
<whitelist>
21+
<directory suffix=".php">./examples</directory>
22+
</whitelist>
23+
</filter>
24+
</phpunit>

src/Base/AbstractApplication.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ protected function filterSpecialCommand($command)
287287

288288
switch ($command) {
289289
case 'help':
290-
$this->showHelpInfo();
290+
$this->showHelpInfo(true, $this->input->getFirstArg());
291291
break;
292292
case 'list':
293293
$this->showCommandList();
@@ -316,16 +316,26 @@ protected function validateName(string $name, $isGroup = false)
316316
}
317317
}
318318

319-
/**********************************************************
319+
/***************************************************************************
320320
* some information for the application
321-
**********************************************************/
321+
***************************************************************************/
322322

323323
/**
324324
* show the application help information
325325
* @param bool $quit
326+
* @param string $command
326327
*/
327-
public function showHelpInfo($quit = true)
328+
public function showHelpInfo($quit = true, string $command = null)
328329
{
330+
// display help for a special command
331+
if ($command) {
332+
$this->input->setCommand($command);
333+
$this->input->setSOpt('h', true);
334+
$this->input->clearArgs();
335+
$this->dispatch($command);
336+
$this->stop();
337+
}
338+
329339
$script = $this->input->getScript();
330340
$sep = $this->delimiter;
331341

@@ -334,6 +344,7 @@ public function showHelpInfo($quit = true)
334344
'example' => [
335345
"$script test (run a independent command)",
336346
"$script home{$sep}index (run a command of the group)",
347+
"$script help {command} (see a command help information)",
337348
"$script home{$sep}index -h (see a command help of the group)",
338349
]
339350
], $quit);

src/IO/Input.php

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ public function getSameArg(array $names, $default = null)
259259
return $this->sameArg($names, $default);
260260
}
261261

262+
/**
263+
* @param array $names
264+
* @param mixed $default
265+
* @return mixed
266+
*/
262267
public function sameArg(array $names, $default = null)
263268
{
264269
foreach ($names as $name) {
@@ -270,9 +275,17 @@ public function sameArg(array $names, $default = null)
270275
return $default;
271276
}
272277

273-
/////////////////////////////////////////////////////////////////////////////////////////
274-
/// long/short options (eg: -d --help)
275-
/////////////////////////////////////////////////////////////////////////////////////////
278+
/**
279+
* clear args
280+
*/
281+
public function clearArgs()
282+
{
283+
$this->args = [];
284+
}
285+
286+
/***************************************************************************
287+
* long/short options (eg: -d --help)
288+
***************************************************************************/
276289

277290
/**
278291
* get (long/short)opt value
@@ -372,7 +385,15 @@ public function sameOpt(array $names, $default = null)
372385
return $default;
373386
}
374387

375-
/////////////////// short-opts /////////////////////
388+
/**
389+
* clear (l/s)opts
390+
*/
391+
public function clearOpts()
392+
{
393+
$this->sOpts = $this->lOpts = [];
394+
}
395+
396+
/************************** short-opts **********************/
376397

377398
/**
378399
* get short-opt value
@@ -421,6 +442,15 @@ public function getShortOpts(): array
421442
return $this->sOpts;
422443
}
423444

445+
/**
446+
* @param string $name
447+
* @param $value
448+
*/
449+
public function setSOpt(string $name, $value)
450+
{
451+
$this->sOpts[$name] = $value;
452+
}
453+
424454
/**
425455
* @return array
426456
*/
@@ -438,7 +468,15 @@ public function setSOpts(array $sOpts, $replace = false)
438468
$this->sOpts = $replace ? $sOpts : array_merge($this->sOpts, $sOpts);
439469
}
440470

441-
/////////////////// long-opts /////////////////////
471+
/**
472+
* clear s-opts
473+
*/
474+
public function clearSOpts()
475+
{
476+
$this->sOpts = [];
477+
}
478+
479+
/************************** long-opts **********************/
442480

443481
/**
444482
* get long-opt value
@@ -495,6 +533,15 @@ public function getLOpts(): array
495533
return $this->lOpts;
496534
}
497535

536+
/**
537+
* @param string $name
538+
* @param $value
539+
*/
540+
public function setLOpt(string $name, $value)
541+
{
542+
$this->lOpts[$name] = $value;
543+
}
544+
498545
/**
499546
* @param array $lOpts
500547
* @param bool $replace
@@ -512,6 +559,14 @@ public function getOpts(): array
512559
return array_merge($this->sOpts, $this->lOpts);
513560
}
514561

562+
/**
563+
* clear l-opts
564+
*/
565+
public function clearLOpts()
566+
{
567+
$this->lOpts = [];
568+
}
569+
515570
/////////////////////////////////////////////////////////////////////////////////////////
516571
/// getter/setter
517572
/////////////////////////////////////////////////////////////////////////////////////////

tests/ApplicationTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Inhere\Console\IO\InputInterface;
4+
use Inhere\Console\IO\OutputInterface;
35
use PHPUnit\Framework\TestCase;
46
use Inhere\Console\Application;
57

@@ -8,5 +10,18 @@
810
*/
911
class ApplicationTest extends TestCase
1012
{
13+
public function testApp()
14+
{
15+
$app = new Application([
16+
'name' => 'Tests',
17+
'debug' => 1,
18+
'version' => '1.0.0',
19+
]);
1120

21+
$this->assertArrayHasKey('name', $app->getMeta());
22+
$this->assertEquals('Tests', $app->getMeta('name'));
23+
24+
$this->assertInstanceOf(InputInterface::class, $app->getInput());
25+
$this->assertInstanceOf(OutputInterface::class, $app->getOutput());
26+
}
1227
}
File renamed without changes.

tests/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/bash
22

33
# phpunit6.phar --colors --coverage-html ./coverage/
4-
phpunit6.phar --colors --bootstrap tests/bootstap.php tests
4+
phpunit6.phar --colors --bootstrap tests/boot.php tests

0 commit comments

Comments
 (0)