Skip to content

Commit 7b362d4

Browse files
committed
Test native enums
- UnitEnum Size - Backed string enum Color (enum names deduced from values) - Backed int enum Size (fallbacking to case names)
1 parent 2666646 commit 7b362d4

File tree

9 files changed

+228
-0
lines changed

9 files changed

+228
-0
lines changed

tests/AbstractQueryProviderTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use TheCodingMachine\GraphQLite\Mappers\Root\BaseTypeMapper;
3636
use TheCodingMachine\GraphQLite\Mappers\Root\CompositeRootTypeMapper;
3737
use TheCodingMachine\GraphQLite\Mappers\Root\CompoundTypeMapper;
38+
use TheCodingMachine\GraphQLite\Mappers\Root\EnumTypeMapper;
3839
use TheCodingMachine\GraphQLite\Mappers\Root\FinalRootTypeMapper;
3940
use TheCodingMachine\GraphQLite\Mappers\Root\IteratorTypeMapper;
4041
use TheCodingMachine\GraphQLite\Mappers\Root\MyCLabsEnumTypeMapper;
@@ -335,6 +336,7 @@ protected function buildRootTypeMapper(): RootTypeMapperInterface
335336
$errorRootTypeMapper = new FinalRootTypeMapper($this->getTypeMapper());
336337
$rootTypeMapper = new BaseTypeMapper($errorRootTypeMapper, $this->getTypeMapper(), $topRootTypeMapper);
337338
$rootTypeMapper = new MyCLabsEnumTypeMapper($rootTypeMapper, $this->getAnnotationReader(), $arrayAdapter, []);
339+
$rootTypeMapper = new EnumTypeMapper($rootTypeMapper, $this->getAnnotationReader(), $arrayAdapter, []);
338340
$rootTypeMapper = new CompoundTypeMapper($rootTypeMapper, $topRootTypeMapper, $this->getTypeRegistry(), $this->getTypeMapper());
339341
$rootTypeMapper = new IteratorTypeMapper($rootTypeMapper, $topRootTypeMapper);
340342

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\GraphQLite\Fixtures\Integration\Controllers;
6+
7+
use TheCodingMachine\GraphQLite\Annotations\Query;
8+
use TheCodingMachine\GraphQLite\Fixtures\Integration\Models\Button;
9+
use TheCodingMachine\GraphQLite\Fixtures\Integration\Models\Color;
10+
use TheCodingMachine\GraphQLite\Fixtures\Integration\Models\Size;
11+
use TheCodingMachine\GraphQLite\Fixtures\Integration\Models\State;
12+
13+
class ButtonController
14+
{
15+
/**
16+
* @Query()
17+
*/
18+
public function getButton(Color $color, Size $size, State $state): Button
19+
{
20+
return new Button($color, $size, $state);
21+
}
22+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\GraphQLite\Fixtures\Integration\Models;
6+
7+
use TheCodingMachine\GraphQLite\Annotations\Field;
8+
use TheCodingMachine\GraphQLite\Annotations\Type;
9+
10+
/**
11+
* @Type
12+
*/
13+
class Button
14+
{
15+
/**
16+
* @var Color
17+
*/
18+
private $color;
19+
20+
/**
21+
* @var Size
22+
*/
23+
private $size;
24+
25+
/**
26+
* @var State
27+
*/
28+
private $state;
29+
30+
public function __construct(Color $color, Size $size, State $state)
31+
{
32+
$this->color = $color;
33+
$this->size = $size;
34+
$this->state = $state;
35+
}
36+
37+
/**
38+
* @Field
39+
*/
40+
public function getColor(): Color
41+
{
42+
return $this->color;
43+
}
44+
45+
/**
46+
* @Field
47+
*/
48+
public function getSize(): Size
49+
{
50+
return $this->size;
51+
}
52+
53+
/**
54+
* @Field
55+
*/
56+
public function getState(): State
57+
{
58+
return $this->state;
59+
}
60+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\GraphQLite\Fixtures\Integration\Models;
6+
7+
use TheCodingMachine\GraphQLite\Annotations\Type;
8+
9+
/**
10+
* @Type
11+
*/
12+
enum Color: string
13+
{
14+
case Green = 'green';
15+
case Red = 'red';
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\GraphQLite\Fixtures\Integration\Models;
6+
7+
use TheCodingMachine\GraphQLite\Annotations\Type;
8+
9+
/**
10+
* @Type
11+
*/
12+
enum Size
13+
{
14+
case S;
15+
case M;
16+
case L;
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\GraphQLite\Fixtures\Integration\Models;
6+
7+
use TheCodingMachine\GraphQLite\Annotations\Type;
8+
9+
/**
10+
* @Type
11+
*/
12+
enum State: int
13+
{
14+
case Off = 0;
15+
case On = 1;
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\GraphQLite\Fixtures;
5+
6+
use TheCodingMachine\GraphQLite\Annotations\Query;
7+
8+
class TestControllerWithEnum
9+
{
10+
/**
11+
* @Query()
12+
*/
13+
public function test(State $backedEnum, Size $unitEnum): TestObjectWithEnum
14+
{
15+
return new TestObjectWithEnum($backedEnum, $unitEnum);
16+
}
17+
}

tests/Fixtures/TestObjectWithEnum.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\GraphQLite\Fixtures;
5+
6+
use TheCodingMachine\GraphQLite\Annotations\Field;
7+
use TheCodingMachine\GraphQLite\Annotations\Type;
8+
9+
/**
10+
* @Type
11+
*/
12+
class TestObjectWithEnum
13+
{
14+
/**
15+
* @var State
16+
*/
17+
private $backedEnum;
18+
19+
/**
20+
* @var Size
21+
*/
22+
private $unitEnum;
23+
24+
public function __construct(State $backedEnum, Size $unitEnum)
25+
{
26+
$this->backedEnum = $backedEnum;
27+
$this->unitEnum = $unitEnum;
28+
}
29+
30+
/**
31+
* @Field
32+
*/
33+
public function getBackedEnum(): State
34+
{
35+
return $this->backedEnum;
36+
}
37+
38+
/**
39+
* @Field
40+
*/
41+
public function getUnitEnum(): Size
42+
{
43+
return $this->unitEnum;
44+
}
45+
}

tests/Integration/EndToEndTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface;
3535
use TheCodingMachine\GraphQLite\Mappers\Root\BaseTypeMapper;
3636
use TheCodingMachine\GraphQLite\Mappers\Root\CompoundTypeMapper;
37+
use TheCodingMachine\GraphQLite\Mappers\Root\EnumTypeMapper;
3738
use TheCodingMachine\GraphQLite\Mappers\Root\FinalRootTypeMapper;
3839
use TheCodingMachine\GraphQLite\Mappers\Root\IteratorTypeMapper;
3940
use TheCodingMachine\GraphQLite\Mappers\Root\MyCLabsEnumTypeMapper;
@@ -233,6 +234,7 @@ public function createContainer(array $overloadedServices = []): ContainerInterf
233234
$errorRootTypeMapper = new FinalRootTypeMapper($container->get(RecursiveTypeMapperInterface::class));
234235
$rootTypeMapper = new BaseTypeMapper($errorRootTypeMapper, $container->get(RecursiveTypeMapperInterface::class), $container->get(RootTypeMapperInterface::class));
235236
$rootTypeMapper = new MyCLabsEnumTypeMapper($rootTypeMapper, $container->get(AnnotationReader::class), new ArrayAdapter(), [ $container->get(NamespaceFactory::class)->createNamespace('TheCodingMachine\\GraphQLite\\Fixtures\\Integration\\Models') ]);
237+
$rootTypeMapper = new EnumTypeMapper($rootTypeMapper, $container->get(AnnotationReader::class), new ArrayAdapter(), [ $container->get(NamespaceFactory::class)->createNamespace('TheCodingMachine\\GraphQLite\\Fixtures\\Integration\\Models') ]);
236238
$rootTypeMapper = new CompoundTypeMapper($rootTypeMapper, $container->get(RootTypeMapperInterface::class), $container->get(TypeRegistry::class), $container->get(RecursiveTypeMapperInterface::class));
237239
$rootTypeMapper = new IteratorTypeMapper($rootTypeMapper, $container->get(RootTypeMapperInterface::class));
238240
return $rootTypeMapper;
@@ -1146,6 +1148,37 @@ public function testEndToEndEnums3(): void
11461148
], $this->getSuccessResult($result));
11471149
}
11481150

1151+
public function testEndToEndNativeEnums(): void
1152+
{
1153+
/**
1154+
* @var Schema $schema
1155+
*/
1156+
$schema = $this->mainContainer->get(Schema::class);
1157+
1158+
$gql = '
1159+
query {
1160+
button(color: red, size: M, state: Off) {
1161+
color
1162+
size
1163+
state
1164+
}
1165+
}
1166+
';
1167+
1168+
$result = GraphQL::executeQuery(
1169+
$schema,
1170+
$gql
1171+
);
1172+
1173+
$this->assertSame([
1174+
'button' => [
1175+
'color' => 'red',
1176+
'size' => 'M',
1177+
'state' => 'Off',
1178+
]
1179+
], $this->getSuccessResult($result));
1180+
}
1181+
11491182
public function testEndToEndDateTime(): void
11501183
{
11511184
/**

0 commit comments

Comments
 (0)