Skip to content

Commit 0a82be2

Browse files
committed
tests: cover selectable attribute
1 parent 5317203 commit 0a82be2

File tree

4 files changed

+314
-0
lines changed

4 files changed

+314
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rebing\GraphQL\Tests\Database\SelectFields\ValidateFieldTests;
6+
7+
use GraphQL\Type\Definition\Type;
8+
use Rebing\GraphQL\Support\Type as GraphQLType;
9+
use Rebing\GraphQL\Tests\Support\Models\Comment;
10+
11+
class CommentType extends GraphQLType
12+
{
13+
protected $attributes = [
14+
'name' => 'Comment',
15+
'model' => Comment::class,
16+
];
17+
18+
public function fields(): array
19+
{
20+
return [
21+
'body' => [
22+
'type' => Type::string(),
23+
],
24+
'id' => [
25+
'type' => Type::nonNull(Type::ID()),
26+
],
27+
'title' => [
28+
'type' => Type::nonNull(Type::string()),
29+
],
30+
];
31+
}
32+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rebing\GraphQL\Tests\Database\SelectFields\ValidateFieldTests;
6+
7+
use GraphQL\Type\Definition\Type;
8+
use Rebing\GraphQL\Support\Facades\GraphQL;
9+
use Rebing\GraphQL\Tests\Support\Models\Post;
10+
use Rebing\GraphQL\Support\Type as GraphQLType;
11+
12+
class PostType extends GraphQLType
13+
{
14+
protected $attributes = [
15+
'name' => 'Post',
16+
'model' => Post::class,
17+
];
18+
19+
public function fields(): array
20+
{
21+
return [
22+
'body_selectable_false' => [
23+
'alias' => 'body',
24+
'type' => Type::string(),
25+
'selectable' => false,
26+
],
27+
'body_selectable_true' => [
28+
'alias' => 'body',
29+
'type' => Type::string(),
30+
'selectable' => true,
31+
],
32+
'body_selectable_null' => [
33+
'alias' => 'body',
34+
'type' => Type::string(),
35+
'selectable' => null,
36+
],
37+
'comments' => [
38+
'type' => Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type('Comment')))),
39+
],
40+
'id' => [
41+
'type' => Type::nonNull(Type::ID()),
42+
],
43+
'title' => [
44+
'type' => Type::nonNull(Type::string()),
45+
],
46+
];
47+
}
48+
}
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rebing\GraphQL\Tests\Database\SelectFields\ValidateFieldTests;
6+
7+
use Rebing\GraphQL\Tests\TestCaseDatabase;
8+
use Rebing\GraphQL\Tests\Support\Models\Post;
9+
use Rebing\GraphQL\Tests\Support\Models\Comment;
10+
use Rebing\GraphQL\Tests\Support\Traits\SqlAssertionTrait;
11+
12+
class ValidateFieldTest extends TestCaseDatabase
13+
{
14+
use SqlAssertionTrait;
15+
16+
public function testSelectableFalse(): void
17+
{
18+
/** @var Post $post */
19+
$post = factory(Post::class)
20+
->create([
21+
'body' => 'post body',
22+
'title' => 'post title',
23+
]);
24+
$comment = factory(Comment::class)
25+
->create([
26+
'body' => 'comment body',
27+
'post_id' => $post->id,
28+
'title' => 'comment title',
29+
]);
30+
31+
$query = <<<'GRAQPHQL'
32+
{
33+
validateFields {
34+
body_selectable_false
35+
title
36+
comments {
37+
id
38+
}
39+
}
40+
}
41+
GRAQPHQL;
42+
43+
$this->sqlCounterReset();
44+
45+
$result = $this->graphql($query);
46+
47+
$this->assertSqlQueries(<<<'SQL'
48+
select "posts"."title", "posts"."id" from "posts";
49+
select "comments"."id", "comments"."post_id" from "comments" where "comments"."post_id" in (?) order by "comments"."id" asc;
50+
SQL
51+
);
52+
53+
$expectedResult = [
54+
'data' => [
55+
'validateFields' => [
56+
[
57+
'body_selectable_false' => null,
58+
'title' => 'post title',
59+
'comments' => [
60+
[
61+
'id' => (string) $comment->id,
62+
],
63+
],
64+
],
65+
],
66+
],
67+
];
68+
$this->assertEquals($expectedResult, $result);
69+
}
70+
71+
public function testSelectableNull(): void
72+
{
73+
/** @var Post $post */
74+
$post = factory(Post::class)
75+
->create([
76+
'body' => 'post body',
77+
'title' => 'post title',
78+
]);
79+
$comment = factory(Comment::class)
80+
->create([
81+
'body' => 'comment body',
82+
'post_id' => $post->id,
83+
'title' => 'comment title',
84+
]);
85+
86+
$query = <<<'GRAQPHQL'
87+
{
88+
validateFields {
89+
body_selectable_null
90+
title
91+
comments {
92+
id
93+
}
94+
}
95+
}
96+
GRAQPHQL;
97+
98+
$this->sqlCounterReset();
99+
100+
$result = $this->graphql($query);
101+
102+
$this->assertSqlQueries(<<<'SQL'
103+
select "posts"."body", "posts"."title", "posts"."id" from "posts";
104+
select "comments"."id", "comments"."post_id" from "comments" where "comments"."post_id" in (?) order by "comments"."id" asc;
105+
SQL
106+
);
107+
108+
$expectedResult = [
109+
'data' => [
110+
'validateFields' => [
111+
[
112+
'body_selectable_null' => 'post body',
113+
'title' => 'post title',
114+
'comments' => [
115+
[
116+
'id' => (string) $comment->id,
117+
],
118+
],
119+
],
120+
],
121+
],
122+
];
123+
$this->assertEquals($expectedResult, $result);
124+
}
125+
126+
public function testSelectableTrue(): void
127+
{
128+
/** @var Post $post */
129+
$post = factory(Post::class)
130+
->create([
131+
'body' => 'post body',
132+
'title' => 'post title',
133+
]);
134+
$comment = factory(Comment::class)
135+
->create([
136+
'body' => 'comment body',
137+
'post_id' => $post->id,
138+
'title' => 'comment title',
139+
]);
140+
141+
$query = <<<'GRAQPHQL'
142+
{
143+
validateFields {
144+
body_selectable_true
145+
title
146+
comments {
147+
id
148+
}
149+
}
150+
}
151+
GRAQPHQL;
152+
153+
$this->sqlCounterReset();
154+
155+
$result = $this->graphql($query);
156+
157+
$this->assertSqlQueries(<<<'SQL'
158+
select "posts"."body", "posts"."title", "posts"."id" from "posts";
159+
select "comments"."id", "comments"."post_id" from "comments" where "comments"."post_id" in (?) order by "comments"."id" asc;
160+
SQL
161+
);
162+
163+
$expectedResult = [
164+
'data' => [
165+
'validateFields' => [
166+
[
167+
'body_selectable_true' => 'post body',
168+
'title' => 'post title',
169+
'comments' => [
170+
[
171+
'id' => (string) $comment->id,
172+
],
173+
],
174+
],
175+
],
176+
],
177+
];
178+
$this->assertEquals($expectedResult, $result);
179+
}
180+
181+
protected function getEnvironmentSetUp($app)
182+
{
183+
parent::getEnvironmentSetUp($app);
184+
185+
$app['config']->set('graphql.schemas.default', [
186+
'query' => [
187+
ValidateFieldsQuery::class,
188+
],
189+
]);
190+
191+
$app['config']->set('graphql.schemas.custom', null);
192+
193+
$app['config']->set('graphql.types', [
194+
CommentType::class,
195+
PostType::class,
196+
]);
197+
}
198+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rebing\GraphQL\Tests\Database\SelectFields\ValidateFieldTests;
6+
7+
use Closure;
8+
use GraphQL\Type\Definition\Type;
9+
use Rebing\GraphQL\Support\Query;
10+
use GraphQL\Type\Definition\ResolveInfo;
11+
use Rebing\GraphQL\Support\SelectFields;
12+
use Rebing\GraphQL\Support\Facades\GraphQL;
13+
use Rebing\GraphQL\Tests\Support\Models\Post;
14+
15+
class ValidateFieldsQuery extends Query
16+
{
17+
protected $attributes = [
18+
'name' => 'validateFields',
19+
];
20+
21+
public function type(): Type
22+
{
23+
return Type::listOf(GraphQL::type('Post'));
24+
}
25+
26+
public function resolve($root, $args, $contxt, ResolveInfo $info, Closure $getSelectFields)
27+
{
28+
/** @var SelectFields $selectFields */
29+
$selectFields = $getSelectFields();
30+
31+
return Post
32+
::select($selectFields->getSelect())
33+
->with($selectFields->getRelations())
34+
->get();
35+
}
36+
}

0 commit comments

Comments
 (0)