|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flow\ETL\Tests\Unit\Function; |
| 6 | + |
| 7 | +use Flow\ETL\Exception\InvalidArgumentException; |
| 8 | +use Flow\ETL\Function\ExecutionMode; |
| 9 | +use Flow\ETL\Tests\Fixtures\Enum\BackedIntEnum; |
| 10 | +use Flow\ETL\Tests\Fixtures\Enum\BackedStringEnum; |
| 11 | +use Flow\ETL\Tests\Fixtures\Enum\BasicEnum; |
| 12 | +use Flow\ETL\Tests\FlowTestCase; |
| 13 | +use PHPUnit\Framework\Attributes\TestWith; |
| 14 | +use UnitEnum; |
| 15 | + |
| 16 | +use function Flow\ETL\DSL\config; |
| 17 | +use function Flow\ETL\DSL\enum_entry; |
| 18 | +use function Flow\ETL\DSL\enum_name; |
| 19 | +use function Flow\ETL\DSL\flow_context; |
| 20 | +use function Flow\ETL\DSL\ref; |
| 21 | +use function Flow\ETL\DSL\row; |
| 22 | +use function Flow\Types\DSL\type_equals; |
| 23 | +use function Flow\Types\DSL\type_string; |
| 24 | + |
| 25 | +final class EnumNameTest extends FlowTestCase |
| 26 | +{ |
| 27 | + public function test_enum_name_accepts_literal_enum(): void |
| 28 | + { |
| 29 | + static::assertSame('one', enum_name(BackedStringEnum::one)->eval(row(), flow_context())?->value); |
| 30 | + } |
| 31 | + |
| 32 | + public function test_enum_name_carries_string_type(): void |
| 33 | + { |
| 34 | + $result = enum_name(ref('e'))->eval(row(enum_entry('e', BackedIntEnum::one)), flow_context()); |
| 35 | + |
| 36 | + static::assertNotNull($result); |
| 37 | + static::assertTrue(type_equals(type_string(), $result->type)); |
| 38 | + } |
| 39 | + |
| 40 | + public function test_enum_name_from_scalar_function_chain(): void |
| 41 | + { |
| 42 | + static::assertSame( |
| 43 | + 'one', |
| 44 | + ref('e')->enumName()->eval(row(enum_entry('e', BackedIntEnum::one)), flow_context())?->value, |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + #[TestWith([BackedStringEnum::one])] |
| 49 | + #[TestWith([BackedIntEnum::one])] |
| 50 | + #[TestWith([BasicEnum::one])] |
| 51 | + public function test_enum_name_returns_case_name(UnitEnum $enum): void |
| 52 | + { |
| 53 | + static::assertSame('one', enum_name(ref('e'))->eval(row(enum_entry('e', $enum)), flow_context())?->value); |
| 54 | + } |
| 55 | + |
| 56 | + #[TestWith([null])] |
| 57 | + #[TestWith(['foo'])] |
| 58 | + #[TestWith([42])] |
| 59 | + public function test_enum_name_returns_null_in_permissive_mode(mixed $input): void |
| 60 | + { |
| 61 | + static::assertNull(enum_name($input)->eval(row(), flow_context())); |
| 62 | + } |
| 63 | + |
| 64 | + #[TestWith([null])] |
| 65 | + #[TestWith(['foo'])] |
| 66 | + #[TestWith([42])] |
| 67 | + public function test_enum_name_throws_in_strict_mode(mixed $input): void |
| 68 | + { |
| 69 | + $this->expectException(InvalidArgumentException::class); |
| 70 | + $this->expectExceptionMessage('EnumName function requires a UnitEnum value'); |
| 71 | + |
| 72 | + $context = flow_context(config()); |
| 73 | + $context->functions()->setMode(ExecutionMode::STRICT); |
| 74 | + |
| 75 | + enum_name($input)->eval(row(), $context); |
| 76 | + } |
| 77 | +} |
0 commit comments