diff --git a/tests/BackedEnumTest.php b/tests/BackedEnumTest.php index 8c89c1f..bee3690 100644 --- a/tests/BackedEnumTest.php +++ b/tests/BackedEnumTest.php @@ -2,6 +2,7 @@ use Cerbero\Enum\CasesCollection; use Cerbero\Enum\BackedEnum; +use Cerbero\Enum\Enums; use Pest\Expectation; it('determines whether the enum is pure') @@ -16,6 +17,27 @@ ->expect(BackedEnum::names()) ->toBe(['one', 'two', 'three']); +it('retrieves the first case', fn() => expect(BackedEnum::first())->toBe(BackedEnum::one)); + +it('retrieves the first case with a closure') + ->expect(BackedEnum::first(fn(BackedEnum $case) => !$case->isOdd())) + ->toBe(BackedEnum::two); + +it('retrieves the result of mapping over all the cases', function() { + $cases = $keys = []; + + $mapped = BackedEnum::map(function(BackedEnum $case, int $key) use (&$cases, &$keys) { + $cases[] = $case; + $keys[] = $key; + + return $case->color(); + }); + + expect($cases)->toBe([BackedEnum::one, BackedEnum::two, BackedEnum::three]) + ->and($keys)->toBe([0, 1, 2]) + ->and($mapped)->toBe(['red', 'green', 'blue']); +}); + it('retrieves all the values of the backed cases') ->expect(BackedEnum::values()) ->toBe([1, 2, 3]); @@ -343,6 +365,65 @@ ->all() ->toBe([BackedEnum::two]); +it('handles the call to an inaccessible enum method') + ->expect(BackedEnum::one()) + ->toBe(1); + +it('fails handling the call to an invalid enum method', fn() => BackedEnum::four()) + ->throws(ValueError::class, '"four" is not a valid name for enum "Cerbero\Enum\BackedEnum"'); + +it('runs custom logic when calling an inaccessible enum method', function() { + Enums::onStaticCall(function(string $enum, string $name, array $arguments) { + expect($enum)->toBe(BackedEnum::class) + ->and($name)->toBe('unknownStaticMethod') + ->and($arguments)->toBe([1, 2, 3]); + + return 'ciao'; + }); + + expect(BackedEnum::unknownStaticMethod(1, 2, 3))->toBe('ciao'); + + (fn() => static::$onStaticCall = null)->bindTo(null, Enums::class)(); +}); + +it('handles the call to an inaccessible case method', fn() => BackedEnum::one->unknownMethod()) + ->throws(Error::class, 'Call to undefined method Cerbero\Enum\BackedEnum::one->unknownMethod()'); + +it('runs custom logic when calling an inaccessible case method', function() { + Enums::onCall(function(object $case, string $name, array $arguments) { + expect($case)->toBeInstanceOf(BackedEnum::class) + ->and($name)->toBe('unknownMethod') + ->and($arguments)->toBe([1, 2, 3]); + + return 'ciao'; + }); + + expect(BackedEnum::one->unknownMethod(1, 2, 3))->toBe('ciao'); + + (fn() => self::$onCall = null)->bindTo(null, Enums::class)(); +}); + +it('handles the invocation of a case') + ->expect((BackedEnum::one)()) + ->toBe(1); + +it('runs custom logic when invocating a case', function() { + Enums::onInvoke(function(object $case, mixed ...$arguments) { + expect($case)->toBeInstanceOf(BackedEnum::class) + ->and($arguments)->toBe([1, 2, 3]); + + return 'ciao'; + }); + + expect((BackedEnum::one)(1, 2, 3))->toBe('ciao'); + + (fn() => self::$onInvoke = null)->bindTo(null, Enums::class)(); +}); + +it('retrieves the keys of an enum', function() { + expect(BackedEnum::keys())->toBe(['name', 'value', 'color', 'shape', 'isOdd']); +}); + it('retrieves the key of a case') ->expect(fn(string $key, mixed $value) => BackedEnum::one->resolveKey($key) === $value) ->toBeTrue() @@ -359,3 +440,7 @@ it('throws a value error when attempting to retrieve an invalid key', fn() => BackedEnum::one->resolveKey('invalid')) ->throws(ValueError::class, '"invalid" is not a valid key for enum "Cerbero\Enum\BackedEnum"'); + +it('retrieves the value of a backed case or the name of a pure case', function() { + expect(BackedEnum::one->value())->toBe(1); +}); diff --git a/tests/CasesCollectionTest.php b/tests/CasesCollectionTest.php index 87d080c..54e7bd7 100644 --- a/tests/CasesCollectionTest.php +++ b/tests/CasesCollectionTest.php @@ -15,6 +15,11 @@ ->count() ->toBe(3); +it('retrieves all the cases as a plain array recursively') + ->expect((new CasesCollection(PureEnum::cases()))->groupBy('isOdd')) + ->toArray() + ->toBe([1 => [PureEnum::one, PureEnum::three], 0 => [PureEnum::two]]); + it('retrieves the first case') ->expect(new CasesCollection(PureEnum::cases())) ->first() @@ -30,6 +35,11 @@ ->first() ->toBeNull(); +it('retrieves the result of mapping over the cases') + ->expect(new CasesCollection(PureEnum::cases())) + ->map(fn(PureEnum $case) => $case->color()) + ->toBe(['red', 'green', 'blue']); + it('retrieves the cases keyed by name') ->expect((new CasesCollection(PureEnum::cases()))->keyByName()) ->toBeInstanceOf(CasesCollection::class) diff --git a/tests/PureEnumTest.php b/tests/PureEnumTest.php index 3c30995..ffdfecc 100644 --- a/tests/PureEnumTest.php +++ b/tests/PureEnumTest.php @@ -1,6 +1,7 @@ all() ->toBe([PureEnum::one, PureEnum::two, PureEnum::three]); +it('retrieves the first case', fn() => expect(PureEnum::first())->toBe(PureEnum::one)); + +it('retrieves the first case with a closure') + ->expect(PureEnum::first(fn(PureEnum $case) => !$case->isOdd())) + ->toBe(PureEnum::two); + +it('retrieves the result of mapping over all the cases', function() { + $cases = $keys = []; + + $mapped = PureEnum::map(function(PureEnum $case, int $key) use (&$cases, &$keys) { + $cases[] = $case; + $keys[] = $key; + + return $case->color(); + }); + + expect($cases)->toBe([PureEnum::one, PureEnum::two, PureEnum::three]) + ->and($keys)->toBe([0, 1, 2]) + ->and($mapped)->toBe(['red', 'green', 'blue']); +}); + it('retrieves all cases keyed by name', function () { expect(PureEnum::keyByName()) ->toBeInstanceOf(CasesCollection::class) @@ -359,6 +381,65 @@ ->all() ->toBe([PureEnum::two]); +it('handles the call to an inaccessible enum method') + ->expect(PureEnum::one()) + ->toBe('one'); + +it('fails handling the call to an invalid enum method', fn() => PureEnum::four()) + ->throws(ValueError::class, '"four" is not a valid name for enum "Cerbero\Enum\PureEnum"'); + +it('runs custom logic when calling an inaccessible enum method', function() { + Enums::onStaticCall(function(string $enum, string $name, array $arguments) { + expect($enum)->toBe(PureEnum::class) + ->and($name)->toBe('unknownStaticMethod') + ->and($arguments)->toBe([1, 2, 3]); + + return 'ciao'; + }); + + expect(PureEnum::unknownStaticMethod(1, 2, 3))->toBe('ciao'); + + (fn() => self::$onStaticCall = null)->bindTo(null, Enums::class)(); +}); + +it('handles the call to an inaccessible case method', fn() => PureEnum::one->unknownMethod()) + ->throws(Error::class, 'Call to undefined method Cerbero\Enum\PureEnum::one->unknownMethod()'); + +it('runs custom logic when calling an inaccessible case method', function() { + Enums::onCall(function(object $case, string $name, array $arguments) { + expect($case)->toBeInstanceOf(PureEnum::class) + ->and($name)->toBe('unknownMethod') + ->and($arguments)->toBe([1, 2, 3]); + + return 'ciao'; + }); + + expect(PureEnum::one->unknownMethod(1, 2, 3))->toBe('ciao'); + + (fn() => self::$onCall = null)->bindTo(null, Enums::class)(); +}); + +it('handles the invocation of a case') + ->expect((PureEnum::one)()) + ->toBe('one'); + +it('runs custom logic when invocating a case', function() { + Enums::onInvoke(function(object $case, mixed ...$arguments) { + expect($case)->toBeInstanceOf(PureEnum::class) + ->and($arguments)->toBe([1, 2, 3]); + + return 'ciao'; + }); + + expect((PureEnum::one)(1, 2, 3))->toBe('ciao'); + + (fn() => self::$onInvoke = null)->bindTo(null, Enums::class)(); +}); + +it('retrieves the keys of an enum', function() { + expect(PureEnum::keys())->toBe(['name', 'color', 'shape', 'isOdd']); +}); + it('retrieves the key of a case') ->expect(fn(string $key, mixed $value) => PureEnum::one->resolveKey($key) === $value) ->toBeTrue() @@ -374,3 +455,7 @@ it('throws a value error when attempting to retrieve an invalid key', fn() => PureEnum::one->resolveKey('invalid')) ->throws(ValueError::class, '"invalid" is not a valid key for enum "Cerbero\Enum\PureEnum"'); + +it('retrieves the value of a backed case or the name of a pure case', function() { + expect(PureEnum::one->value())->toBe('one'); +});