Skip to content

Commit

Permalink
Improve PlaneEnumTest (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlocati authored Oct 29, 2023
1 parent 62453fc commit ef9171a
Showing 1 changed file with 71 additions and 48 deletions.
119 changes: 71 additions & 48 deletions test/tests/PlaneEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,72 +12,95 @@

final class PlaneEnumTest extends TestCase
{
private static ReflectionEnum $planeClass;

/**
* {@inheritdoc}
*
* @see \PHPUnit\Framework\TestCase::setUpBeforeClass()
*/
public static function setUpBeforeClass(): void
public static function provideEnumCases(): array
{
self::$planeClass = new ReflectionEnum(Plane::class);
$planeClass = new ReflectionEnum(Plane::class);
$previousCase = null;
return array_map(
static function (ReflectionEnumBackedCase $value) use (&$previousCase): array {
$case = $value->getValue();
$result = [$value->getValue(), $previousCase];
$previousCase = $case;
return $result;
},
$planeClass->getCases()
);
}

public function testEnumType(): void
{
$type = self::$planeClass->getBackingType();
$planeClass = new ReflectionEnum(Plane::class);
$type = $planeClass->getBackingType();
$this->assertNotNull($type);
$this->assertTrue($type->isBuiltin());
$this->assertSame('int', $type->getName());
}

public function testEnumNamesAndValues(): void
public function testEnumeratedCases(): void
{
$previousCase = null;
foreach (self::$planeClass->getCases() as $case) {
$this->assertInstanceOf(ReflectionEnumBackedCase::class, $case);
$this->assertIsInt($case->getBackingValue());
$this->assertMatchesRegularExpression('/[^0-9]' . $case->getBackingValue() . '$/', $case->getName());
if ($previousCase !== null) {
$this->assertSame($previousCase->getBackingValue() + 1, $case->getBackingValue());
$casesList = self::provideEnumCases();
$this->assertGreaterThan(0, count($casesList));
foreach ($casesList as $index => [, $previousCase]) {
if ($index === 0) {
$this->assertNull($previousCase);
} else {
$this->assertInstanceOf(Plane::class, $previousCase);
}
$previousCase = $case;
}
$this->assertNotNull($previousCase);
}

public function testInfo(): void
/**
* @dataProvider provideEnumCases
*/
public function testEnumNamesAndValues(Plane $case, ?Plane $previousCase): void
{
$previousInfo = null;
$previousNames = [];
$previousShortNames = [];
foreach (self::$planeClass->getCases() as $case) {
$infoAttributes = $case->getAttributes(PlaneInfo::class);
$this->assertCount(1, $infoAttributes);
$info = $infoAttributes[0]->newInstance();
$this->assertInstanceOf(PlaneInfo::class, $info);
$this->assertEquals($info, PlaneInfo::from($case->getValue()));
/** @var PlaneInfo $info */
$this->assertGreaterThanOrEqual(0, $info->fromCodepoint);
$this->assertGreaterThanOrEqual($info->fromCodepoint, $info->toCodepoint);
if ($previousInfo !== null) {
$this->assertSame($previousInfo->toCodepoint + 1, $info->fromCodepoint);
}
$this->assertMatchesRegularExpression('/[^0-9]' . $case->value . '$/', $case->name);
if ($previousCase !== null) {
$this->assertSame($previousCase->value + 1, $case->value);
}
}

/**
* @dataProvider provideEnumCases
*/
public function testInfo(Plane $case, ?Plane $previousCase): void
{
$info = PlaneInfo::from($case);
$this->assertInstanceOf(PlaneInfo::class, $info);
$caseReflection = new ReflectionEnumBackedCase($case, $case->name);
$caseAttributes = $caseReflection->getAttributes(PlaneInfo::class);
$this->assertCount(1, $caseAttributes);
$this->assertEquals($info, $caseAttributes[0]->newInstance());
$this->assertGreaterThanOrEqual(0, $info->fromCodepoint);
$this->assertGreaterThanOrEqual($info->fromCodepoint, $info->toCodepoint);
if ($info->unassigned) {
$this->assertSame('', $info->name);
$this->assertSame('', $info->shortName);
} else {
$this->assertNotSame('', $info->name);
$this->assertNotSame('', $info->shortName);
}
if ($previousCase !== null) {
$previousInfo = PlaneInfo::from($previousCase);
$this->assertSame($previousInfo->toCodepoint + 1, $info->fromCodepoint);
}
}

public function testNonDuplicatedNames(): void
{
$allNames = [];
$allShortNames = [];
foreach (self::provideEnumCases() as [$case]) {
$info = PlaneInfo::from($case);
if ($info->unassigned) {
$this->assertSame('', $info->name);
$this->assertSame('', $info->shortName);
} else {
$this->assertNotSame('', $info->name);
$this->assertNotSame('', $info->shortName);
$this->assertNotContains($info->name, $previousNames);
$this->assertNotContains($info->shortName, $previousShortNames);
$previousNames[] = $info->name;
$previousShortNames[] = $info->shortName;
continue;
}
$previousInfo = $info;
$this->assertNotContains($info->name, $allNames);
$this->assertNotContains($info->shortName, $allShortNames);
$allNames[] = $info->name;
$allShortNames[] = $info->shortName;
}
$this->assertNotNull($previousInfo);
$this->assertNotSame([], $previousNames);
$this->assertNotSame([], $allNames);
$this->assertNotSame([], $allShortNames);
}
}

0 comments on commit ef9171a

Please sign in to comment.