From 46090f8114ab9ca7fe2af4fbc04e9507aba76f1b Mon Sep 17 00:00:00 2001 From: lip8up Date: Fri, 18 Feb 2022 19:26:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20asList=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + README.md | 2 +- runtest.sh | 9 --------- src/Enum.php | 34 ++++++++++++++++++++++++++++++++++ tests/EnumTest.php | 13 +++++++++++++ tests/runtest.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 10 deletions(-) delete mode 100755 runtest.sh create mode 100755 tests/runtest.sh diff --git a/.gitignore b/.gitignore index 193af85..cee83d9 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/README.md b/README.md index b74b1ca..6059c39 100644 --- a/README.md +++ b/README.md @@ -208,7 +208,7 @@ json_encode(Some::One()); // '{"key":"One","value":1,"label":"\u4e00"}' ## 单元测试 -运行 `./runtest.sh` 执行单元测试,已 **100%** 测试通过,请放心使用。 +运行 `tests/runtest.sh` 执行单元测试,已 **100%** 测试通过,请放心使用。 ## 结语 diff --git a/runtest.sh b/runtest.sh deleted file mode 100755 index b9d7d56..0000000 --- a/runtest.sh +++ /dev/null @@ -1,9 +0,0 @@ -baseDir=`dirname $(readlink -f $0)` - -runDir=`pwd` - -if [ $baseDir != $runDir ]; then cd $baseDir; fi - -./vendor/bin/phpunit --testdox --color ${1:-tests} - -if [ $baseDir != $runDir ]; then cd - > /dev/null; fi diff --git a/src/Enum.php b/src/Enum.php index da8da23..8a7b969 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -142,6 +142,40 @@ public static function allConstants(): array return self::$allConstants[$class]; } + /** + * 作为列表返回,以便前端使用,类 js 格式:`[ { key: key1, value: value1, label: label1 }, ... ]`。 + * + * @return array + * + * @example # + * + * ```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' => '三' ], + * ] + * ``` + * + */ + 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 列表。 * diff --git a/tests/EnumTest.php b/tests/EnumTest.php index f3c4a25..1c4a6fd 100644 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -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']); diff --git a/tests/runtest.sh b/tests/runtest.sh new file mode 100755 index 0000000..2d24c0c --- /dev/null +++ b/tests/runtest.sh @@ -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