Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Oct 1, 2024
1 parent 387bd90 commit cc54f8d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 46 deletions.
103 changes: 58 additions & 45 deletions tests/CasesCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
use Cerbero\Enum\BackedEnum;
use Cerbero\Enum\CasesCollection;
use Cerbero\Enum\PureEnum;
use Pest\Expectation;

it('retrieves all the cases')
->expect(new CasesCollection(PureEnum::cases()))
->cases()
->all()
->toBe([PureEnum::one, PureEnum::two, PureEnum::three]);

it('retrieves the count of all the cases')
Expand All @@ -29,29 +30,32 @@
->first()
->toBeNull();

it('returns a default value if no case is present')
->expect(new CasesCollection([]))
->first(default: PureEnum::one)
->toBe(PureEnum::one);

it('retrieves the cases keyed by name')
->expect(new CasesCollection(PureEnum::cases()))
->keyByName()
->expect((new CasesCollection(PureEnum::cases()))->keyByName())
->toBeInstanceOf(CasesCollection::class)
->all()
->toBe(['one' => PureEnum::one, 'two' => PureEnum::two, 'three' => PureEnum::three]);

it('retrieves the cases keyed by a custom key')
->expect(new CasesCollection(PureEnum::cases()))
->keyBy('color')
->expect((new CasesCollection(PureEnum::cases()))->keyBy('color'))
->toBeInstanceOf(CasesCollection::class)
->all()
->toBe(['red' => PureEnum::one, 'green' => PureEnum::two, 'blue' => PureEnum::three]);

it('retrieves the cases keyed by a custom closure')
->expect(new CasesCollection(PureEnum::cases()))
->keyBy(fn(PureEnum $case) => $case->shape())
->toBe(['triangle' => PureEnum::one, 'square' => PureEnum::two, 'circle' => PureEnum::three]);

it('retrieves the cases keyed by value')
->expect(new CasesCollection(BackedEnum::cases()))
->keyByValue()
->toBeInstanceOf(CasesCollection::class)
->sequence(
fn(Expectation $case, Expectation $key) => $key->toBe('triangle')->and($case)->toBe(PureEnum::one),
fn(Expectation $case, Expectation $key) => $key->toBe('square')->and($case)->toBe(PureEnum::two),
fn(Expectation $case, Expectation $key) => $key->toBe('circle')->and($case)->toBe(PureEnum::three),
);

it('retrieves the cases keyed by value xxxxxxx')
->expect((new CasesCollection(BackedEnum::cases()))->keyByValue())
->toBeInstanceOf(CasesCollection::class)
->all()
->toBe([1 => BackedEnum::one, 2 => BackedEnum::two, 3 => BackedEnum::three]);

it('retrieves an empty array when trying to key cases by value belonging to a pure enum')
Expand All @@ -62,12 +66,21 @@
it('retrieves the cases grouped by a custom key')
->expect(new CasesCollection(PureEnum::cases()))
->groupBy('color')
->toBe(['red' => [PureEnum::one], 'green' => [PureEnum::two], 'blue' => [PureEnum::three]]);
->toBeInstanceOf(CasesCollection::class)
->sequence(
fn(Expectation $cases, Expectation $key) => $key->toBe('red')->and($cases)->toBeInstanceOf(CasesCollection::class)->all()->toBe([PureEnum::one]),
fn(Expectation $cases, Expectation $key) => $key->toBe('green')->and($cases)->toBeInstanceOf(CasesCollection::class)->all()->toBe([PureEnum::two]),
fn(Expectation $cases, Expectation $key) => $key->toBe('blue')->and($cases)->toBeInstanceOf(CasesCollection::class)->all()->toBe([PureEnum::three]),
);

it('retrieves the cases grouped by a custom closure')
->expect(new CasesCollection(PureEnum::cases()))
->groupBy(fn(PureEnum $case) => $case->isOdd())
->toBe([1 => [PureEnum::one, PureEnum::three], 0 => [PureEnum::two]]);
->toBeInstanceOf(CasesCollection::class)
->sequence(
fn(Expectation $cases) => $cases->toBeInstanceOf(CasesCollection::class)->all()->toBe([PureEnum::one, PureEnum::three]),
fn(Expectation $cases) => $cases->toBeInstanceOf(CasesCollection::class)->all()->toBe([PureEnum::two]),
);

it('retrieves all the names of the cases')
->expect(new CasesCollection(PureEnum::cases()))
Expand All @@ -84,14 +97,14 @@
->values()
->toBeEmpty();

it('retrieves a list of names by default when plucking a pure enum')
it('retrieves a list of names')
->expect(new CasesCollection(PureEnum::cases()))
->pluck()
->pluck('name')
->toBe(['one', 'two', 'three']);

it('retrieves a list of names by default when plucking a backed enum')
it('retrieves a list of values')
->expect(new CasesCollection(BackedEnum::cases()))
->pluck()
->pluck('value')
->toBe([1, 2, 3]);

it('retrieves a list of custom values when plucking with an argument')
Expand All @@ -117,99 +130,99 @@
it('retrieves a collection with filtered cases', function () {
expect((new CasesCollection(PureEnum::cases()))->filter(fn(PureEnum $case) => $case->isOdd()))
->toBeInstanceOf(CasesCollection::class)
->cases()
->toBe([PureEnum::one, PureEnum::three]);
->all()
->toBe([0 => PureEnum::one, 2 => PureEnum::three]);
});

it('retrieves a collection with cases filtered by a key', function () {
expect((new CasesCollection(PureEnum::cases()))->filter('isOdd'))
->toBeInstanceOf(CasesCollection::class)
->cases()
->toBe([PureEnum::one, PureEnum::three]);
->all()
->toBe([0 => PureEnum::one, 2 => PureEnum::three]);
});

it('retrieves a collection of cases with the given names', function () {
expect((new CasesCollection(PureEnum::cases()))->only('one', 'three'))
->toBeInstanceOf(CasesCollection::class)
->cases()
->toBe([PureEnum::one, PureEnum::three]);
->all()
->toBe([0 => PureEnum::one, 2 => PureEnum::three]);
});

it('retrieves a collection of cases excluding the given names', function () {
expect((new CasesCollection(PureEnum::cases()))->except('one', 'three'))
->toBeInstanceOf(CasesCollection::class)
->cases()
->toBe([PureEnum::two]);
->all()
->toBe([1 => PureEnum::two]);
});

it('retrieves a collection of cases with the given values', function () {
expect((new CasesCollection(BackedEnum::cases()))->onlyValues(1, 3))
->toBeInstanceOf(CasesCollection::class)
->cases()
->toBe([BackedEnum::one, BackedEnum::three]);
->all()
->toBe([0 => BackedEnum::one, 2 => BackedEnum::three]);
});

it('retrieves a collection of cases excluding the given values', function () {
expect((new CasesCollection(BackedEnum::cases()))->exceptValues(1, 3))
->toBeInstanceOf(CasesCollection::class)
->cases()
->toBe([BackedEnum::two]);
->all()
->toBe([1 => BackedEnum::two]);
});

it('retrieves an empty collection of cases when when including values of pure enums', function () {
expect((new CasesCollection(PureEnum::cases()))->onlyValues(1, 3))
->toBeInstanceOf(CasesCollection::class)
->cases()
->all()
->toBeEmpty();
});

it('retrieves an empty collection of cases when when excluding values of pure enums', function () {
expect((new CasesCollection(PureEnum::cases()))->exceptValues(1, 3))
->toBeInstanceOf(CasesCollection::class)
->cases()
->all()
->toBeEmpty();
});

it('retrieves a collection of cases sorted by name ascending', function () {
expect((new CasesCollection(PureEnum::cases()))->sort())
->toBeInstanceOf(CasesCollection::class)
->cases()
->toBe([PureEnum::one, PureEnum::three, PureEnum::two]);
->all()
->toBe([0 => PureEnum::one, 2 => PureEnum::three, 1 => PureEnum::two]);
});

it('retrieves a collection of cases sorted by name decending', function () {
expect((new CasesCollection(PureEnum::cases()))->sortDesc())
->toBeInstanceOf(CasesCollection::class)
->cases()
->toBe([PureEnum::two, PureEnum::three, PureEnum::one]);
->all()
->toBe([1 => PureEnum::two, 2 => PureEnum::three, 0 => PureEnum::one]);
});

it('retrieves a collection of cases sorted by a key ascending', function () {
expect((new CasesCollection(PureEnum::cases()))->sortBy('color'))
->toBeInstanceOf(CasesCollection::class)
->cases()
->toBe([PureEnum::three, PureEnum::two, PureEnum::one]);
->all()
->toBe([2 => PureEnum::three, 1 => PureEnum::two, 0 => PureEnum::one]);
});

it('retrieves a collection of cases sorted by a key decending', function () {
expect((new CasesCollection(PureEnum::cases()))->sortByDesc('color'))
->toBeInstanceOf(CasesCollection::class)
->cases()
->all()
->toBe([PureEnum::one, PureEnum::two, PureEnum::three]);
});

it('retrieves a collection of cases sorted by value ascending', function () {
expect((new CasesCollection(BackedEnum::cases()))->sortByValue())
->toBeInstanceOf(CasesCollection::class)
->cases()
->all()
->toBe([BackedEnum::one, BackedEnum::two, BackedEnum::three]);
});

it('retrieves a collection of cases sorted by value decending', function () {
expect((new CasesCollection(BackedEnum::cases()))->sortByDescValue())
->toBeInstanceOf(CasesCollection::class)
->cases()
->toBe([BackedEnum::three, BackedEnum::two, BackedEnum::one]);
->all()
->toBe([2 => BackedEnum::three, 1 => BackedEnum::two, 0 => BackedEnum::one]);
});

it('retrieves the iterator', function () {
Expand Down
11 changes: 10 additions & 1 deletion tests/PureEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

it('retrieves all cases keyed by name', function () {
expect(PureEnum::keyByName())
->toBeInstanceOf(CasesCollection::class)
->all()
->toBe(['one' => PureEnum::one, 'two' => PureEnum::two, 'three' => PureEnum::three]);
});

Expand All @@ -30,12 +32,19 @@

it('retrieves all cases keyed by a custom key', function () {
expect(PureEnum::keyBy('color'))
->toBeInstanceOf(CasesCollection::class)
->all()
->toBe(['red' => PureEnum::one, 'green' => PureEnum::two, 'blue' => PureEnum::three]);
});

it('retrieves all cases keyed by the result of a closure', function () {
expect(PureEnum::keyBy(fn(PureEnum $case) => $case->shape()))
->toBe(['triangle' => PureEnum::one, 'square' => PureEnum::two, 'circle' => PureEnum::three]);
->toBeInstanceOf(CasesCollection::class)
->sequence(
fn(Expectation $case, Expectation $key) => $key->toBe('triangle')->and($case)->toBe(PureEnum::one),
fn(Expectation $case, Expectation $key) => $key->toBe('square')->and($case)->toBe(PureEnum::two),
fn(Expectation $case, Expectation $key) => $key->toBe('circle')->and($case)->toBe(PureEnum::three),
);
});

it('retrieves all cases grouped by a custom key', function () {
Expand Down

0 comments on commit cc54f8d

Please sign in to comment.