Skip to content

Commit b4c48bc

Browse files
committed
WIP: Task: test providesTheEnumInstanceTypeWhenAnStaticEnumTypeIsReferencedInTheComponentApi
1 parent 3c164cb commit b4c48bc

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/TypeSystem/Type/EnumType/EnumInstanceType.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ enumStaticType: $enumStaticType,
4242
memberName: null
4343
);
4444
}
45-
45+
4646
public static function fromStaticEnumAndMemberName(EnumStaticType $enumStaticType, string $enumMemberName): self
4747
{
4848
return new self(
@@ -53,6 +53,13 @@ enumStaticType: $enumStaticType,
5353

5454
public function is(TypeInterface $other): bool
5555
{
56+
if ($other === $this) {
57+
return true;
58+
}
59+
if ($other instanceof EnumInstanceType) {
60+
return $other->enumStaticType->is($other->enumStaticType)
61+
&& $other->memberName === $this->memberName;
62+
}
5663
return false;
5764
}
5865
}

test/Unit/TypeSystem/Scope/ComponentScope/ComponentScopeTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222

2323
namespace PackageFactory\ComponentEngine\Test\Unit\TypeSystem\Scope\ComponentScope;
2424

25+
use PackageFactory\ComponentEngine\Module\ModuleId;
2526
use PackageFactory\ComponentEngine\Parser\Ast\ComponentDeclarationNode;
27+
use PackageFactory\ComponentEngine\Parser\Ast\EnumDeclarationNode;
2628
use PackageFactory\ComponentEngine\Parser\Ast\TypeReferenceNode;
2729
use PackageFactory\ComponentEngine\Test\Unit\TypeSystem\Scope\Fixtures\DummyScope;
2830
use PackageFactory\ComponentEngine\TypeSystem\Scope\ComponentScope\ComponentScope;
2931
use PackageFactory\ComponentEngine\TypeSystem\Scope\GlobalScope\GlobalScope;
32+
use PackageFactory\ComponentEngine\TypeSystem\Type\EnumType\EnumStaticType;
3033
use PackageFactory\ComponentEngine\TypeSystem\Type\NumberType\NumberType;
3134
use PackageFactory\ComponentEngine\TypeSystem\Type\StringType\StringType;
3235
use PHPUnit\Framework\TestCase;
@@ -63,6 +66,43 @@ public function providesTheTypesOfComponentApiMembers(): void
6366
);
6467
}
6568

69+
/**
70+
* @test
71+
* @return void
72+
*/
73+
public function providesTheEnumInstanceTypeWhenAnStaticEnumTypeIsReferencedInTheComponentApi(): void
74+
{
75+
$componentDeclarationAsString = <<<EOT
76+
component Foo {
77+
foo: SomeEnum
78+
79+
return <div>{foo}</div>
80+
}
81+
EOT;
82+
$componentDeclarationNode = ComponentDeclarationNode::fromString($componentDeclarationAsString);
83+
$componentScope = new ComponentScope(
84+
componentDeclarationNode: $componentDeclarationNode,
85+
parentScope: new DummyScope([], [
86+
'SomeEnum' => $enumStaticType = EnumStaticType::fromModuleIdAndDeclaration(
87+
ModuleId::fromString("module-a"),
88+
EnumDeclarationNode::fromString(
89+
'enum SomeEnum { A B C }'
90+
)
91+
)
92+
])
93+
);
94+
95+
$expectedType = $enumStaticType->toEnumInstanceType();
96+
$actualType = $componentScope->lookupTypeFor('foo');
97+
98+
$this->assertNotNull($actualType);
99+
100+
$this->assertTrue(
101+
$expectedType->is($actualType),
102+
sprintf('Expected %s, got %s', $expectedType::class, $actualType::class)
103+
);
104+
}
105+
66106
/**
67107
* @test
68108
* @return void
@@ -122,4 +162,4 @@ public function resolvesTypeReferencesUsingParentScope(): void
122162
sprintf('Expected %s, got %s', $expectedType::class, $actualType::class)
123163
);
124164
}
125-
}
165+
}

0 commit comments

Comments
 (0)