Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Oct 3, 2024
1 parent 7ed5efb commit 637ed3c
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 0 deletions.
85 changes: 85 additions & 0 deletions tests/BackedEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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]);
Expand Down Expand Up @@ -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()
Expand All @@ -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);
});
10 changes: 10 additions & 0 deletions tests/CasesCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
85 changes: 85 additions & 0 deletions tests/PureEnumTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Cerbero\Enum\CasesCollection;
use Cerbero\Enum\Enums;
use Cerbero\Enum\PureEnum;
use Pest\Expectation;

Expand All @@ -18,6 +19,27 @@
->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)
Expand Down Expand Up @@ -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()
Expand All @@ -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');
});

0 comments on commit 637ed3c

Please sign in to comment.