- Introduced IntStatusFixture, StringStatusFixture, and MissingLabelFixture for comprehensive testing of enum behavior.
107 lines
3.5 KiB
PHP
107 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Shared\Enums;
|
|
|
|
use Shared\Enums\BaseEnum;
|
|
|
|
covers(BaseEnum::class);
|
|
|
|
it('exposes value, name, and configured label for int-backed enum', function (): void {
|
|
// GIVEN
|
|
$enum = IntStatusFixture::IN_PROGRESS;
|
|
|
|
// WHEN
|
|
$value = $enum->value();
|
|
$name = $enum->name();
|
|
$label = $enum->label();
|
|
|
|
// THEN
|
|
expect($value)->toBe(2)
|
|
->and($name)->toBe('IN_PROGRESS')
|
|
->and($label)->toBe('In Progress');
|
|
});
|
|
|
|
it('falls back to ucfirst lowercase name when LABELS is not defined', function (): void {
|
|
// GIVEN
|
|
$enum = StringStatusFixture::DRAFT;
|
|
|
|
// WHEN
|
|
$label = $enum->label();
|
|
$labels = StringStatusFixture::toLabels();
|
|
|
|
// THEN
|
|
expect($label)->toBe('Draft')
|
|
->and($labels)->toBe([
|
|
'DRAFT' => 'Draft',
|
|
'PUBLISHED' => 'Published',
|
|
]);
|
|
});
|
|
|
|
it('returns all labels and values maps', function (): void {
|
|
// GIVEN / WHEN
|
|
$labels = IntStatusFixture::toLabels();
|
|
$values = IntStatusFixture::toValues();
|
|
|
|
// THEN
|
|
expect($labels)->toBe([
|
|
'PENDING' => 'En attente',
|
|
'IN_PROGRESS' => 'In Progress',
|
|
'DONE' => 'Terminé',
|
|
])->and($values)->toBe([
|
|
'PENDING' => 1,
|
|
'IN_PROGRESS' => 2,
|
|
'DONE' => 3,
|
|
]);
|
|
});
|
|
|
|
it('throws Error when LABELS is missing a case key', function (): void {
|
|
// GIVEN / WHEN / THEN
|
|
expect(static fn (): array => MissingLabelFixture::toLabels())
|
|
->toThrow(\Error::class, "Missing definition label Tests\Unit\Shared\Enums\MissingLabelFixture for 'SECOND'");
|
|
});
|
|
|
|
it('resolves enum via make from instance, value, normalized label, custom field, and returns null when unknown', function (): void {
|
|
// GIVEN / WHEN
|
|
$fromInstance = IntStatusFixture::make(IntStatusFixture::DONE);
|
|
$fromIntValue = IntStatusFixture::make(1);
|
|
$fromNormalizedLabel = IntStatusFixture::make('In-Progress!', 'label');
|
|
$fromNameField = IntStatusFixture::make('PENDING', 'name');
|
|
$fromUnknownValue = IntStatusFixture::make(999);
|
|
$fromUnknownLabel = IntStatusFixture::make('nonexistent', 'label');
|
|
|
|
// THEN
|
|
expect($fromInstance)->toBe(IntStatusFixture::DONE)
|
|
->and($fromIntValue)->toBe(IntStatusFixture::PENDING)
|
|
->and($fromNormalizedLabel)->toBe(IntStatusFixture::IN_PROGRESS)
|
|
->and($fromNameField)->toBe(IntStatusFixture::PENDING)
|
|
->and($fromUnknownValue)->toBeNull()
|
|
->and($fromUnknownLabel)->toBeNull()
|
|
->and(static fn (): ?StringStatusFixture => StringStatusFixture::make('draft'))->toThrow(\TypeError::class);
|
|
});
|
|
|
|
it('checks equality including variadic arguments', function (): void {
|
|
// GIVEN
|
|
$enum = IntStatusFixture::PENDING;
|
|
|
|
// WHEN / THEN
|
|
expect($enum->equals(IntStatusFixture::PENDING))->toBeTrue()
|
|
->and($enum->equals(IntStatusFixture::DONE))->toBeFalse()
|
|
->and($enum->equals(IntStatusFixture::DONE, IntStatusFixture::PENDING))->toBeTrue()
|
|
->and($enum->equals(IntStatusFixture::DONE, IntStatusFixture::IN_PROGRESS))->toBeFalse();
|
|
});
|
|
|
|
it('serializes to json using its backing value', function (): void {
|
|
// GIVEN
|
|
$intEnum = IntStatusFixture::DONE;
|
|
$stringEnum = StringStatusFixture::PUBLISHED;
|
|
|
|
// WHEN
|
|
$encodedInt = json_encode($intEnum);
|
|
$encodedString = json_encode($stringEnum);
|
|
|
|
// THEN
|
|
expect($encodedInt)->toBe('3')
|
|
->and($encodedString)->toBe('"published"');
|
|
});
|