AbstractEnumTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. declare(strict_types = 1);
  3. namespace DASPRiD\EnumTest;
  4. use DASPRiD\Enum\AbstractEnum;
  5. use DASPRiD\Enum\Exception\CloneNotSupportedException;
  6. use DASPRiD\Enum\Exception\IllegalArgumentException;
  7. use DASPRiD\Enum\Exception\MismatchException;
  8. use DASPRiD\Enum\Exception\SerializeNotSupportedException;
  9. use DASPRiD\Enum\Exception\UnserializeNotSupportedException;
  10. use PHPUnit\Framework\TestCase;
  11. use ReflectionClass;
  12. final class AbstractEnumTest extends TestCase
  13. {
  14. public function setUp(): void
  15. {
  16. $reflectionClass = new ReflectionClass(AbstractEnum::class);
  17. $constantsProperty = $reflectionClass->getProperty('constants');
  18. $constantsProperty->setAccessible(true);
  19. $constantsProperty->setValue([]);
  20. $valuesProperty = $reflectionClass->getProperty('values');
  21. $valuesProperty->setAccessible(true);
  22. $valuesProperty->setValue([]);
  23. $allValuesLoadedProperty = $reflectionClass->getProperty('allValuesLoaded');
  24. $allValuesLoadedProperty->setAccessible(true);
  25. $allValuesLoadedProperty->setValue([]);
  26. }
  27. public function testToString() : void
  28. {
  29. $weekday = WeekDay::FRIDAY();
  30. self::assertSame('FRIDAY', (string) $weekday);
  31. }
  32. public function testName() : void
  33. {
  34. $this->assertSame('WEDNESDAY', WeekDay::WEDNESDAY()->name());
  35. }
  36. public function testOrdinal() : void
  37. {
  38. $this->assertSame(2, WeekDay::WEDNESDAY()->ordinal());
  39. }
  40. public function testSameInstanceIsReturned() : void
  41. {
  42. self::assertSame(WeekDay::FRIDAY(), WeekDay::FRIDAY());
  43. }
  44. public static function testValueOf() : void
  45. {
  46. self::assertSame(WeekDay::FRIDAY(), WeekDay::valueOf('FRIDAY'));
  47. }
  48. public function testValueOfInvalidConstant() : void
  49. {
  50. $this->expectException(IllegalArgumentException::class);
  51. WeekDay::valueOf('CATURDAY');
  52. }
  53. public function testExceptionOnCloneAttempt() : void
  54. {
  55. $this->expectException(CloneNotSupportedException::class);
  56. clone WeekDay::FRIDAY();
  57. }
  58. public function testExceptionOnSerializeAttempt() : void
  59. {
  60. $this->expectException(SerializeNotSupportedException::class);
  61. serialize(WeekDay::FRIDAY());
  62. }
  63. public function testExceptionOnUnserializeAttempt() : void
  64. {
  65. $this->expectException(UnserializeNotSupportedException::class);
  66. unserialize('O:24:"DASPRiD\\EnumTest\\WeekDay":0:{}');
  67. }
  68. public function testReturnValueOfValuesIsSortedByOrdinal() : void
  69. {
  70. // Initialize some week days out of order
  71. WeekDay::SATURDAY();
  72. WeekDay::TUESDAY();
  73. $ordinals = array_values(array_map(function (WeekDay $weekDay) : int {
  74. return $weekDay->ordinal();
  75. }, WeekDay::values()));
  76. self::assertSame([0, 1, 2, 3, 4, 5, 6], $ordinals);
  77. $cachedOrdinals = array_values(array_map(function (WeekDay $weekDay) : int {
  78. return $weekDay->ordinal();
  79. }, WeekDay::values()));
  80. $this->assertSame($ordinals, $cachedOrdinals);
  81. }
  82. public function testCompareTo() : void
  83. {
  84. $this->assertSame(-4, WeekDay::WEDNESDAY()->compareTo(WeekDay::SUNDAY()));
  85. $this->assertSame(4, WeekDay::SUNDAY()->compareTo(WeekDay::WEDNESDAY()));
  86. $this->assertSame(0, WeekDay::WEDNESDAY()->compareTo(WeekDay::WEDNESDAY()));
  87. }
  88. public function testCompareToWrongEnum() : void
  89. {
  90. $this->expectException(MismatchException::class);
  91. WeekDay::MONDAY()->compareTo(Planet::EARTH());
  92. }
  93. public function testParameterizedEnum() : void
  94. {
  95. $planet = Planet::EARTH();
  96. $this->assertSame(5.976e+24, $planet->mass());
  97. $this->assertSame(6.37814e6, $planet->radius());
  98. }
  99. }