forked from nuwave/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectiveLocatorTest.php
116 lines (92 loc) · 3.62 KB
/
DirectiveLocatorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php declare(strict_types=1);
namespace Tests\Unit\Schema;
use GraphQL\Language\Parser;
use Nuwave\Lighthouse\Exceptions\DirectiveException;
use Nuwave\Lighthouse\Schema\DirectiveLocator;
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Schema\Directives\FieldDirective;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
use Nuwave\Lighthouse\Support\Contracts\FieldResolver;
use Nuwave\Lighthouse\Support\Utils;
use Tests\TestCase;
final class DirectiveLocatorTest extends TestCase
{
private DirectiveLocator $directiveLocator;
protected function setUp(): void
{
parent::setUp();
$this->directiveLocator = $this->app->make(DirectiveLocator::class);
}
public function testRegistersLighthouseDirectives(): void
{
$this->assertInstanceOf(
FieldDirective::class,
$this->directiveLocator->create('field'),
);
}
public function testHydratesBaseDirectives(): void
{
$fieldDefinition = Parser::fieldDefinition(/** @lang GraphQL */ '
foo: String @field
');
$fieldDirective = $this
->directiveLocator
->associated($fieldDefinition)
->first();
assert($fieldDirective instanceof FieldDirective);
$this->assertSame(
$fieldDefinition,
Utils::accessProtected($fieldDirective, 'definitionNode'),
);
}
public function testSkipsHydrationForNonBaseDirectives(): void
{
$fieldDefinition = Parser::fieldDefinition(/** @lang GraphQL */ '
foo: String @foo
');
$directive = new class() implements FieldMiddleware {
public static function definition(): string
{
return /** @lang GraphQL */ 'foo';
}
public function handleField(FieldValue $fieldValue): void {}
};
$this->directiveLocator->setResolved('foo', $directive::class);
$directive = $this
->directiveLocator
->associated($fieldDefinition)
->first();
$this->assertNotInstanceOf(BaseDirective::class, $directive);
}
public function testThrowsIfDirectiveNameCanNotBeResolved(): void
{
$this->expectException(DirectiveException::class);
$this->directiveLocator->create('foobar');
}
public function testCreateSingleDirective(): void
{
$fieldDefinition = Parser::fieldDefinition(/** @lang GraphQL */ '
foo: [Foo!]! @hasMany
');
$resolver = $this->directiveLocator->exclusiveOfType($fieldDefinition, FieldResolver::class);
$this->assertInstanceOf(FieldResolver::class, $resolver);
}
public function testThrowsExceptionWhenMultipleFieldResolverDirectives(): void
{
$this->expectException(DirectiveException::class);
$this->expectExceptionMessage("Node bar can only have one directive of type Nuwave\Lighthouse\Support\Contracts\FieldResolver but found [@hasMany, @belongsTo].");
$fieldDefinition = Parser::fieldDefinition(/** @lang GraphQL */ '
bar: [Bar!]! @hasMany @belongsTo
');
$this->directiveLocator->exclusiveOfType($fieldDefinition, FieldResolver::class);
}
public function testCreateMultipleDirectives(): void
{
$fieldDefinition = Parser::fieldDefinition(/** @lang GraphQL */ '
bar: String @can(if: ["viewBar"]) @event
');
$middleware = $this->directiveLocator->associatedOfType($fieldDefinition, FieldMiddleware::class);
$this->assertCount(2, $middleware);
}
}