|
7 | 7 | use InvalidArgumentException; |
8 | 8 | use PHPUnit\Framework\TestCase; |
9 | 9 |
|
| 10 | +enum TestStringBackedEnum: string |
| 11 | +{ |
| 12 | + case Foo = 'foo'; |
| 13 | + case Bar = 'bar'; |
| 14 | +} |
| 15 | + |
| 16 | +enum TestIntBackedEnum: int |
| 17 | +{ |
| 18 | + case One = 1; |
| 19 | + case Two = 2; |
| 20 | +} |
| 21 | + |
10 | 22 | class RepositoryTest extends TestCase |
11 | 23 | { |
12 | 24 | /** |
@@ -372,4 +384,60 @@ public function testItThrowsAnExceptionWhenTryingToGetNonFloatValueAsFloat(): vo |
372 | 384 |
|
373 | 385 | $this->repository->float('a.b'); |
374 | 386 | } |
| 387 | + |
| 388 | + public function testItGetsAsEnumFromStringValue(): void |
| 389 | + { |
| 390 | + $this->repository->set('string_enum', 'foo'); |
| 391 | + |
| 392 | + $this->assertSame( |
| 393 | + TestStringBackedEnum::Foo, |
| 394 | + $this->repository->enum('string_enum', TestStringBackedEnum::class) |
| 395 | + ); |
| 396 | + } |
| 397 | + |
| 398 | + public function testItGetsAsEnumFromIntValue(): void |
| 399 | + { |
| 400 | + $this->repository->set('int_enum', 1); |
| 401 | + |
| 402 | + $this->assertSame( |
| 403 | + TestIntBackedEnum::One, |
| 404 | + $this->repository->enum('int_enum', TestIntBackedEnum::class) |
| 405 | + ); |
| 406 | + } |
| 407 | + |
| 408 | + public function testItGetsAsEnumWhenValueIsAlreadyEnumInstance(): void |
| 409 | + { |
| 410 | + $this->repository->set('enum_instance', TestStringBackedEnum::Bar); |
| 411 | + |
| 412 | + $this->assertSame( |
| 413 | + TestStringBackedEnum::Bar, |
| 414 | + $this->repository->enum('enum_instance', TestStringBackedEnum::class) |
| 415 | + ); |
| 416 | + } |
| 417 | + |
| 418 | + public function testItGetsAsEnumWithDefault(): void |
| 419 | + { |
| 420 | + $this->assertSame( |
| 421 | + TestStringBackedEnum::Foo, |
| 422 | + $this->repository->enum('non_existent_key', TestStringBackedEnum::class, TestStringBackedEnum::Foo) |
| 423 | + ); |
| 424 | + } |
| 425 | + |
| 426 | + public function testItThrowsAnExceptionWhenEnumValueIsInvalid(): void |
| 427 | + { |
| 428 | + $this->repository->set('invalid_enum', 'invalid'); |
| 429 | + |
| 430 | + $this->expectException(InvalidArgumentException::class); |
| 431 | + $this->expectExceptionMessage('Configuration value for key [invalid_enum] is not a valid value for enum'); |
| 432 | + |
| 433 | + $this->repository->enum('invalid_enum', TestStringBackedEnum::class); |
| 434 | + } |
| 435 | + |
| 436 | + public function testItThrowsAnExceptionWhenEnumValueIsNull(): void |
| 437 | + { |
| 438 | + $this->expectException(InvalidArgumentException::class); |
| 439 | + $this->expectExceptionMessage('Configuration value for key [non_existent] must be a valid backed enum value'); |
| 440 | + |
| 441 | + $this->repository->enum('non_existent', TestStringBackedEnum::class); |
| 442 | + } |
375 | 443 | } |
0 commit comments