From e01edd128f0f99eb1271848d950b78871f633ce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 20 Feb 2022 21:50:53 +0100 Subject: [PATCH] add options() method, resolve #2 --- src/Options.php | 16 ++++++++++++++++ tests/Pest.php | 11 +++++++++++ .../{EnumTest.php => InvokableCasesTest.php} | 9 --------- tests/Pest/OptionsTest.php | 7 +++++++ 4 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 src/Options.php rename tests/Pest/{EnumTest.php => InvokableCasesTest.php} (80%) create mode 100644 tests/Pest/OptionsTest.php diff --git a/src/Options.php b/src/Options.php new file mode 100644 index 0000000..785daf6 --- /dev/null +++ b/src/Options.php @@ -0,0 +1,16 @@ + case value]. */ + public static function options(): array + { + return array_reduce(static::cases(), function ($options, $case) { + $options[$case->name] = $case->value; + + return $options; + }, []); + } +} diff --git a/tests/Pest.php b/tests/Pest.php index 68220b0..5a22b99 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -11,6 +11,9 @@ | */ +use ArchTech\Enums\InvokableCases; +use ArchTech\Enums\Options; + uses(ArchTech\Enums\Tests\TestCase::class)->in('Pest'); /* @@ -43,3 +46,11 @@ function something() { // .. } + +enum Status: int +{ + use InvokableCases, Options; + + case PENDING = 0; + case DONE = 1; +} diff --git a/tests/Pest/EnumTest.php b/tests/Pest/InvokableCasesTest.php similarity index 80% rename from tests/Pest/EnumTest.php rename to tests/Pest/InvokableCasesTest.php index 27b03d5..ba23579 100644 --- a/tests/Pest/EnumTest.php +++ b/tests/Pest/InvokableCasesTest.php @@ -1,7 +1,6 @@ toBe(0); @@ -18,11 +17,3 @@ it('can be invoked as an instance', function () { it('throws an error when a nonexistent case is being used', function () { Status::INVALID(); })->expectException(UndefinedCaseError::class); - -enum Status: int -{ - use InvokableCases; - - case PENDING = 0; - case DONE = 1; -} diff --git a/tests/Pest/OptionsTest.php b/tests/Pest/OptionsTest.php new file mode 100644 index 0000000..1498efd --- /dev/null +++ b/tests/Pest/OptionsTest.php @@ -0,0 +1,7 @@ +expect(Status::options())->toBe([ + 'PENDING' => 0, + 'DONE' => 1, + ]);