Skip to content

Commit

Permalink
添加 asList 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
lip8up committed Feb 18, 2022
1 parent 173d0f1 commit 46090f8
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
tests/.runtestlast
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ json_encode(Some::One()); // '{"key":"One","value":1,"label":"\u4e00"}'

## 单元测试

运行 `./runtest.sh` 执行单元测试,已 **100%** 测试通过,请放心使用。
运行 `tests/runtest.sh` 执行单元测试,已 **100%** 测试通过,请放心使用。

## 结语

Expand Down
9 changes: 0 additions & 9 deletions runtest.sh

This file was deleted.

34 changes: 34 additions & 0 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,40 @@ public static function allConstants(): array
return self::$allConstants[$class];
}

/**
* 作为列表返回,以便前端使用,类 js 格式:`[ { key: key1, value: value1, label: label1 }, ... ]`。
*
* @return array
*
* @example #
* <code>
* ```php
* // 对于下面的 Some
* class Some extends Enum
* {
* private const One = [1, '一'];
* private const Two = [2, '二'];
* private const Three = [3, '三'];
* }
* // 调用 Some::asList(),结果为:
* [
* [ 'key' => 'One', 'value' => 1, 'label' => '一' ],
* [ 'key' => 'Two', 'value' => 2, 'label' => '二' ],
* [ 'key' => 'Three', 'value' => 3, 'label' => '三' ],
* ]
* ```
* </code>
*/
public static function asList()
{
$all = self::allConstants();
$list = [];
foreach ($all as $key => [$value, $label]) {
array_push($list, ['key' => $key, 'value' => $value, 'label' => $label]);
}
return $list;
}

/**
* 获取全部 key 列表。
*
Expand Down
13 changes: 13 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ public function testAllConstants()
]);
}

public function testAsList()
{
$this->assertEquals(Some::asList(), [
[ 'key' => 'One', 'value' => 1, 'label' => '' ],
[ 'key' => 'Two', 'value' => 2, 'label' => '' ],
[ 'key' => 'Three', 'value' => 3, 'label' => '' ],
]);
$this->assertEquals(Other::asList(), [
[ 'key' => 'Haha', 'value' => 'hh', 'label' => 'hh' ],
[ 'key' => 'Bibi', 'value' => 'bb', 'label' => 'bb' ],
]);
}

public function testAllKeys()
{
$this->assertEquals(Some::allKeys(), ['One', 'Two', 'Three']);
Expand Down
42 changes: 42 additions & 0 deletions tests/runtest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
runDir=`pwd`
testsDir=`dirname $(readlink -f $0)`
projDir=`dirname $testsDir`

if test $runDir == $projDir; then
tests="tests"
elif test $runDir == $testsDir; then
tests="."
else
echo "must run in project dir or tests dir"
exit 1
fi

env="$projDir/.env"
envBack="$projDir/.env.back"
envTests="$projDir/.env.tests"

if test -e "$env" && test -e "$envTests"; then
cp -f "$env" "$envBack"
cp -f "$envTests" "$env"
fi

last="$tests/.runtestlast"

if test $# == 0; then
if test -e "$last"; then
file=`head -n 1 "$last"`
else
file="$tests"
fi
elif test "$1" == "all"; then
file="$tests"
else
file="$1"
echo "$1" > "$last"
fi

$tests/../vendor/bin/phpunit --testdox --color "$file"

if test -e "$env" && test -e "$envTests"; then
mv "$envBack" "$env"
fi

0 comments on commit 46090f8

Please sign in to comment.