Skip to content

Commit da2ea23

Browse files
committed
up: add more tests for flags add
1 parent 8c84296 commit da2ea23

22 files changed

+326
-162
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555

5656
- name: Run test suite
5757
run: |
58-
phpunit -v
58+
phpunit -v --debug
5959
php example flags-demo.php
6060
php example sflags-demo.php
6161

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@ $fs->addOpt('age', 'a', 'this is a int option', FlagType::INT);
7777

7878
// - use string rule
7979
$fs->addOptByRule('name,n', 'string;this is a string option;true');
80+
8081
// -- add multi option at once.
8182
$fs->addOptsByRules([
8283
'tag,t' => 'strings;array option, allow set multi times',
8384
'f' => 'bool;this is an bool option',
8485
]);
86+
8587
// - use array rule
8688
/** @see Flags::DEFINE_ITEM for array rule */
8789
$fs->addOptByRule('name-is-very-lang', [
@@ -375,7 +377,13 @@ public const DEFINE_ITEM = [
375377
## Unit tests
376378

377379
```bash
378-
phpunit
380+
phpunit --debug
381+
```
382+
383+
test with coverage:
384+
385+
```bash
386+
phpdbg -qrr $(which phpunit) --coverage-text
379387
```
380388

381389
## License

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
}
2828
},
2929
"scripts": {
30-
"test": "php vender/bin/phpunit"
30+
"test": "php vender/bin/phpunit -v --debug"
3131
}
3232
}

phpunit.xml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false"
3-
bootstrap="test/bootstrap.php" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true"
4-
convertWarningsToExceptions="true" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
colors="true"
4+
backupGlobals="false"
5+
backupStaticAttributes="false"
6+
bootstrap="test/bootstrap.php"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
stopOnFailure="false"
11+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
512

613
<testsuites>
714
<testsuite name="Library Test Suite">
815
<directory>test/</directory>
916
</testsuite>
1017
</testsuites>
1118

12-
<filter>
13-
<whitelist>
14-
<directory suffix=".php">app</directory>
15-
</whitelist>
16-
</filter>
19+
<coverage>
20+
<include>
21+
<directory suffix=".php">src</directory>
22+
</include>
23+
</coverage>
1724
</phpunit>

src/Contract/FlagInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ public function isRequired(): bool;
6666
* @return bool
6767
*/
6868
public function isOptional(): bool;
69+
70+
/**
71+
* @return string
72+
*/
73+
public function getKind(): string;
6974
}

src/Contract/ParserInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
*/
1515
interface ParserInterface
1616
{
17+
public const KIND_OPT = 'option';
18+
public const KIND_ARG = 'argument';
19+
1720
/**
1821
* @return array
1922
* @psalm-return list<string>
@@ -36,6 +39,11 @@ public function isEmpty(): bool;
3639
*/
3740
public function isNotEmpty(): bool;
3841

42+
/**
43+
* @return bool
44+
*/
45+
public function hasShortOpts(): bool;
46+
3947
/**
4048
* Add option
4149
*

src/Exception/FlagParseException.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Toolkit\PFlag\Exception;
44

55
use Throwable;
6+
use Toolkit\PFlag\FlagsParser;
67

78
/**
89
* class FlagParseException
@@ -12,9 +13,9 @@ class FlagParseException extends FlagException
1213
/**
1314
* @var string
1415
*/
15-
public $flagType = 'option';
16+
public $flagType = FlagsParser::KIND_OPT;
1617

17-
public function __construct(string $message, int $code = 0, string $flagType = 'option')
18+
public function __construct(string $message, int $code = 0, string $flagType = FlagsParser::KIND_OPT)
1819
{
1920
$this->flagType = $flagType;
2021

src/Flag/AbstractFlag.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Toolkit\PFlag\Contract\FlagInterface;
1515
use Toolkit\PFlag\Contract\ValidatorInterface;
1616
use Toolkit\PFlag\Exception\FlagException;
17+
use Toolkit\PFlag\FlagsParser;
1718
use Toolkit\PFlag\FlagType;
1819
use Toolkit\Stdlib\Obj;
1920
use Toolkit\Stdlib\OS;
@@ -203,7 +204,8 @@ public function setValue($value): void
203204
}
204205

205206
if (false === $ok) {
206-
throw new FlagException('invalid value for flag: ' . $this->getNameMark());
207+
$kind = $this->getKind();
208+
throw new FlagException("set invalid value for flag $kind: " . $this->getNameMark());
207209
}
208210
}
209211

@@ -242,6 +244,14 @@ public function getNameMark(): string
242244
return $this->name;
243245
}
244246

247+
/**
248+
* @return string
249+
*/
250+
public function getKind(): string
251+
{
252+
return FlagsParser::KIND_OPT;
253+
}
254+
245255
/******************************************************************
246256
* getter/setter methods
247257
*****************************************************************/
@@ -285,7 +295,8 @@ public function getName(): string
285295
public function setName(string $name): void
286296
{
287297
if (!FlagHelper::isValidName($name)) {
288-
throw new FlagException('invalid flag name: ' . $name);
298+
$kind = $this->getKind();
299+
throw new FlagException("invalid flag $kind name: " . $name);
289300
}
290301

291302
$this->name = $name;

src/Flag/Argument.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Toolkit\PFlag\Flag;
1111

12+
use Toolkit\PFlag\FlagsParser;
1213
use Toolkit\Stdlib\Str;
1314
use function sprintf;
1415

@@ -37,6 +38,14 @@ public function setName(string $name): void
3738
}
3839
}
3940

41+
/**
42+
* @return string
43+
*/
44+
public function getKind(): string
45+
{
46+
return FlagsParser::KIND_ARG;
47+
}
48+
4049
/**
4150
* @return string
4251
*/

src/Flags.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ public function bindingArguments(): self
420420
*/
421421
public function buildHelp(bool $withColor = true): string
422422
{
423-
return $this->doBuildHelp($this->arguments, $this->options, $withColor, $this->countAlias() > 0);
423+
return $this->doBuildHelp($this->arguments, $this->options, $withColor, $this->hasShortOpts());
424424
}
425425

426426
/**

0 commit comments

Comments
 (0)