ReflectionEnum_getCase.phpt 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. --TEST--
  2. ReflectionEnum::getCases()
  3. --FILE--
  4. <?php
  5. enum Enum_ {
  6. case Foo;
  7. const Bar = self::Foo;
  8. }
  9. enum IntEnum: int {
  10. case Foo = 0;
  11. const Bar = self::Foo;
  12. }
  13. function test(string $enumName, string $caseName) {
  14. try {
  15. $reflectionEnum = new ReflectionEnum($enumName);
  16. var_dump($reflectionEnum->getCase($caseName));
  17. } catch (Throwable $e) {
  18. echo get_class($e) . ': ' . $e->getMessage() . "\n";
  19. }
  20. }
  21. test(Enum_::class, 'Foo');
  22. test(Enum_::class, 'Bar');
  23. test(Enum_::class, 'Baz');
  24. test(IntEnum::class, 'Foo');
  25. test(IntEnum::class, 'Bar');
  26. test(IntEnum::class, 'Baz');
  27. ?>
  28. --EXPECT--
  29. object(ReflectionEnumUnitCase)#2 (2) {
  30. ["name"]=>
  31. string(3) "Foo"
  32. ["class"]=>
  33. string(5) "Enum_"
  34. }
  35. ReflectionException: Enum_::Bar is not a case
  36. ReflectionException: Case Enum_::Baz does not exist
  37. object(ReflectionEnumBackedCase)#2 (2) {
  38. ["name"]=>
  39. string(3) "Foo"
  40. ["class"]=>
  41. string(7) "IntEnum"
  42. }
  43. ReflectionException: IntEnum::Bar is not a case
  44. ReflectionException: Case IntEnum::Baz does not exist