ldc-mesbr-app/tests/Unit/Domain/Enums/CountryTest.php
2026-10-02 12:47:27 +02:00

81 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Shared\Enums;
use Domain\Enums\Country;
covers(Country::class);
describe('country', function (): void {
it('has the expected cases with their backing values', function (): void {
// THEN
expect(Country::FR->value())->toBe(1)
->and(Country::BE->value())->toBe(2)
->and(Country::LU->value())->toBe(3);
});
it('exposes its case name', function (): void {
// THEN
expect(Country::FR->name())->toBe('FR');
});
it('returns the defined label', function (): void {
// THEN
expect(Country::FR->label())->toBe('FR')
->and(Country::BE->label())->toBe('BE')
->and(Country::LU->label())->toBe('LU');
});
it('lists all labels keyed by case name', function (): void {
// THEN
expect(Country::toLabels())->toBe([
'FR' => 'FR',
'BE' => 'BE',
'LU' => 'LU',
]);
});
it('lists all values keyed by case name', function (): void {
// THEN
expect(Country::toValues())->toBe([
'FR' => 1,
'BE' => 2,
'LU' => 3,
]);
});
it('makes an instance from its backing value', function (): void {
// THEN
expect(Country::make(1))->toBe(Country::FR);
});
it('makes an instance from a label', function (): void {
// THEN
expect(Country::make('BE', 'label'))->toBe(Country::BE);
});
it('returns the same instance when already an enum case', function (): void {
// THEN
expect(Country::make(Country::LU))->toBe(Country::LU);
});
it('returns null for an unknown value', function (): void {
// THEN
expect(Country::make(99))->toBeNull();
});
it('implements equals correctly', function (): void {
// THEN
expect(Country::FR->equals(Country::FR))->toBeTrue()
->and(Country::FR->equals(Country::BE))->toBeFalse()
->and(Country::FR->equals(null))->toBeFalse();
});
it('serializes to json as its backing value', function (): void {
// THEN
expect(Country::FR->jsonSerialize())->toBe(1)
->and(json_encode(Country::FR))->toBe('1');
});
});