Skip to content

Commit adea531

Browse files
committed
tests: show that using wildcard references for input don't quite work
1 parent 976d185 commit adea531

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed

tests/Unit/Input/UserInput.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
namespace Rebing\GraphQL\Tests\Unit\Input;
5+
6+
use GraphQL\Type\Definition\Type;
7+
use Rebing\GraphQL\Support\InputType;
8+
9+
class UserInput extends InputType
10+
{
11+
/** @var array<string,mixed> */
12+
protected $attributes = [
13+
'name' => 'UserInput',
14+
];
15+
16+
public function fields(): array
17+
{
18+
return [
19+
'password' => [
20+
'name' => 'password',
21+
'type' => Type::string(),
22+
'rules' => [
23+
'string',
24+
'nullable',
25+
'same:data.*.password_confirmation',
26+
],
27+
],
28+
'password_confirmation' => [
29+
'name' => 'password_confirmation',
30+
'type' => Type::string(),
31+
],
32+
];
33+
}
34+
}

tests/Unit/Input/UserInputTest.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
namespace Rebing\GraphQL\Tests\Unit\Input;
5+
6+
use Rebing\GraphQL\Tests\TestCase;
7+
8+
class UserInputTest extends TestCase
9+
{
10+
/**
11+
* Ref https://github.com/rebing/graphql-laravel/issues/716
12+
*/
13+
public function testWildcardInputValidationOneInput(): void
14+
{
15+
$query = <<<'GRAQPHQL'
16+
mutation {
17+
userUpdate(
18+
data: [
19+
{
20+
password: "yolo"
21+
password_confirmation: "yolo"
22+
}
23+
]
24+
)
25+
}
26+
GRAQPHQL;
27+
28+
$result = $this->graphql($query, [
29+
'expectErrors' => true,
30+
]);
31+
32+
// TODO: otherwise serialized objects appear
33+
$result = json_decode((string) json_encode($result), true);
34+
35+
$expected = [
36+
'errors' => [
37+
[
38+
'message' => 'validation',
39+
'extensions' => [
40+
'category' => 'validation',
41+
'validation' => [
42+
'data.0.password' => [
43+
'The data.0.password and data.*.password confirmation must match.',
44+
],
45+
],
46+
],
47+
'locations' => [
48+
[
49+
'line' => 2,
50+
'column' => 5,
51+
],
52+
],
53+
'path' => [
54+
'userUpdate',
55+
],
56+
],
57+
],
58+
];
59+
self::assertEquals($expected, $result);
60+
}
61+
62+
/**
63+
* Ref https://github.com/rebing/graphql-laravel/issues/716
64+
*/
65+
public function testWildcardInputValidationTwoInputs(): void
66+
{
67+
$query = <<<'GRAQPHQL'
68+
mutation {
69+
userUpdate(
70+
data: [
71+
{
72+
password: "yolo"
73+
password_confirmation: "yolo"
74+
}
75+
{
76+
password: "foo"
77+
password_confirmation: "foo"
78+
}
79+
]
80+
)
81+
}
82+
GRAQPHQL;
83+
84+
$result = $this->graphql($query, [
85+
'expectErrors' => true,
86+
]);
87+
88+
// TODO: otherwise serialized objects appear
89+
$result = json_decode((string) json_encode($result), true);
90+
91+
$expected = [
92+
'errors' => [
93+
[
94+
'message' => 'validation',
95+
'extensions' => [
96+
'category' => 'validation',
97+
'validation' => [
98+
'data.0.password' => [
99+
'The data.0.password and data.*.password confirmation must match.',
100+
],
101+
'data.1.password' => [
102+
'The data.1.password and data.*.password confirmation must match.',
103+
],
104+
],
105+
],
106+
'locations' => [
107+
[
108+
'line' => 2,
109+
'column' => 5,
110+
],
111+
],
112+
'path' => [
113+
'userUpdate',
114+
],
115+
],
116+
],
117+
];
118+
self::assertEquals($expected, $result);
119+
}
120+
121+
protected function getEnvironmentSetUp($app): void
122+
{
123+
$app['config']->set('graphql.schemas.default', [
124+
'mutation' => [
125+
UserUpdateMutation::class,
126+
],
127+
'types' => [
128+
UserInput::class,
129+
],
130+
]);
131+
}
132+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
namespace Rebing\GraphQL\Tests\Unit\Input;
5+
6+
use GraphQL\Type\Definition\Type;
7+
use Rebing\GraphQL\Support\Facades\GraphQL;
8+
use Rebing\GraphQL\Support\Mutation;
9+
10+
class UserUpdateMutation extends Mutation
11+
{
12+
/** @var array<string,string> */
13+
protected $attributes = [
14+
'name' => 'userUpdate',
15+
];
16+
17+
public function type(): Type
18+
{
19+
return Type::nonNull(Type::boolean());
20+
}
21+
22+
public function args(): array
23+
{
24+
return [
25+
'data' => [
26+
'name' => 'data',
27+
'type' => Type::listOf(GraphQL::type('UserInput')),
28+
'rules' => [
29+
'required',
30+
],
31+
],
32+
];
33+
}
34+
35+
public function resolve(): bool
36+
{
37+
return true;
38+
}
39+
}

0 commit comments

Comments
 (0)