Skip to content

Commit fbd57ba

Browse files
committed
Test FieldResolver
1 parent be4e26a commit fbd57ba

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jerowork\GraphqlAttributeSchema\Test\Doubles\Type;
6+
7+
use Jerowork\GraphqlAttributeSchema\Attribute\Field;
8+
use Jerowork\GraphqlAttributeSchema\Attribute\Type;
9+
10+
#[Type]
11+
final readonly class TestResolvableType
12+
{
13+
#[Field]
14+
public function getName(string $name): string
15+
{
16+
return sprintf('GetName has been called with name %s', $name);
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jerowork\GraphqlAttributeSchema\Test\Doubles\Type;
6+
7+
use Jerowork\GraphqlAttributeSchema\Attribute\Field;
8+
use Jerowork\GraphqlAttributeSchema\Attribute\Type;
9+
use Jerowork\GraphqlAttributeSchema\Test\Doubles\Enum\TestEnumType;
10+
11+
#[Type]
12+
final readonly class TestResolvableTypeWithEnumAsOutput
13+
{
14+
#[Field]
15+
public function getName(string $name): TestEnumType
16+
{
17+
return TestEnumType::from($name);
18+
}
19+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jerowork\GraphqlAttributeSchema\Test\TypeResolver;
6+
7+
use Jerowork\GraphqlAttributeSchema\Parser\Ast;
8+
use Jerowork\GraphqlAttributeSchema\Parser\Node\ArgNode;
9+
use Jerowork\GraphqlAttributeSchema\Parser\Node\EnumNode;
10+
use Jerowork\GraphqlAttributeSchema\Parser\Node\FieldNode;
11+
use Jerowork\GraphqlAttributeSchema\Parser\Node\FieldNodeType;
12+
use Jerowork\GraphqlAttributeSchema\Test\Doubles\Enum\TestEnumType;
13+
use Jerowork\GraphqlAttributeSchema\Test\Doubles\InputType\TestResolvableInputType;
14+
use Jerowork\GraphqlAttributeSchema\Test\Doubles\Type\TestResolvableType;
15+
use Jerowork\GraphqlAttributeSchema\Test\Doubles\Type\TestResolvableTypeWithEnumAsOutput;
16+
use Jerowork\GraphqlAttributeSchema\TypeResolver\FieldResolver;
17+
use PHPUnit\Framework\Attributes\Test;
18+
use PHPUnit\Framework\TestCase;
19+
use Override;
20+
21+
/**
22+
* @internal
23+
*/
24+
final class FieldResolverTest extends TestCase
25+
{
26+
private FieldResolver $fieldResolver;
27+
28+
#[Override]
29+
protected function setUp(): void
30+
{
31+
parent::setUp();
32+
33+
$this->fieldResolver = new FieldResolver();
34+
}
35+
36+
#[Test]
37+
public function itShouldResolveProperty(): void
38+
{
39+
$type = $this->fieldResolver->resolve(new FieldNode(
40+
null,
41+
'string',
42+
'name',
43+
null,
44+
true,
45+
[],
46+
FieldNodeType::Property,
47+
null,
48+
'name',
49+
), new Ast());
50+
51+
self::assertSame('a name', $type(new TestResolvableInputType('a name'), []));
52+
}
53+
54+
#[Test]
55+
public function itShouldResolveMethod(): void
56+
{
57+
$type = $this->fieldResolver->resolve(new FieldNode(
58+
null,
59+
'string',
60+
'name',
61+
null,
62+
true,
63+
[
64+
new ArgNode(
65+
null,
66+
'string',
67+
'name',
68+
null,
69+
true,
70+
'name',
71+
),
72+
],
73+
FieldNodeType::Method,
74+
'getName',
75+
null,
76+
), new Ast());
77+
78+
self::assertSame(
79+
'GetName has been called with name a name',
80+
$type(new TestResolvableType(), [
81+
'name' => 'a name',
82+
]),
83+
);
84+
}
85+
86+
#[Test]
87+
public function itShouldResolveMethodWithEnumOutput(): void
88+
{
89+
$type = $this->fieldResolver->resolve(new FieldNode(
90+
TestEnumType::class,
91+
null,
92+
'name',
93+
null,
94+
true,
95+
[
96+
new ArgNode(
97+
null,
98+
'string',
99+
'name',
100+
null,
101+
true,
102+
'name',
103+
),
104+
],
105+
FieldNodeType::Method,
106+
'getName',
107+
null,
108+
), new Ast(
109+
new EnumNode(
110+
TestEnumType::class,
111+
'TestEnum',
112+
null,
113+
[
114+
'a',
115+
'b',
116+
'c',
117+
'd',
118+
],
119+
),
120+
));
121+
122+
self::assertSame(
123+
'b',
124+
$type(new TestResolvableTypeWithEnumAsOutput(), [
125+
'name' => 'b',
126+
]),
127+
);
128+
}
129+
}

0 commit comments

Comments
 (0)