Passed
Pull Request — master (#7)
by Max
05:09
created

ScalarValidationTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
c 1
b 0
f 0
dl 0
loc 119
rs 10
wmc 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Tests\Type\ValidatedFieldDefinition;
6
7
use GraphQL\GraphQL;
8
use GraphQL\Tests\Utils;
9
use GraphQL\Type\Definition\ObjectType;
10
use GraphQL\Type\Definition\Type;
11
use GraphQL\Type\Definition\ValidatedFieldDefinition;
12
use GraphQL\Type\Schema;
13
use PHPUnit\Framework\TestCase;
14
15
final class ScalarValidationTest extends TestCase
16
{
17
    /** @var Type */
18
    protected $bookType;
19
20
    /** @var Type */
21
    protected $personType;
22
23
    /** @var mixed[] */
24
    protected $data = [
25
        'people' => [
26
            1 => ['firstName' => 'Wilson'],
27
        ],
28
        'books' => [
29
            1 => [
30
                'title' => 'Where the Red Fern Grows',
31
                'author' => 1,
32
            ],
33
        ],
34
    ];
35
36
    /** @var ObjectType */
37
    protected $query;
38
39
    /** @var Schema */
40
    protected $schema;
41
42
    protected function setUp() : void
43
    {
44
        $this->personType = new ObjectType([
45
            'name' => 'Person',
46
            'fields' => [
47
                'firstName' => [
48
                    'type' => Type::string(),
49
                ],
50
            ],
51
        ]);
52
53
        $this->bookType = new ObjectType([
54
            'name' => 'Book',
55
            'fields' => [
56
                'title' => [
57
                    'type' => Type::string(),
58
                    'resolve' => static function ($book) {
59
                        return $book['title'];
60
                    },
61
                ],
62
                'author' => [
63
                    'type' => $this->personType,
64
                    'resolve' => static function ($book) {
65
                        return $book['author'];
66
                    },
67
                ],
68
            ],
69
        ]);
70
71
        $this->query = new ObjectType(['name' => 'Query']);
72
73
        $this->schema = new Schema([
74
            'query' => $this->query,
75
            'mutation' => new ObjectType([
76
                'name' => 'Mutation',
77
                'fields' => function () {
78
                    return [
79
                        'updateBook' => new ValidatedFieldDefinition([
80
                            'name' => 'updateBook',
81
                            'type' => $this->bookType,
82
                            'args' => [
83
                                'bookId' => [
84
                                    'type' => Type::id(),
85
                                    'errorCodes' => ['bookNotFound'],
86
                                    'validate' => function ($bookId) {
87
                                        if (isset($this->data['books'][$bookId])) {
88
                                            return 0;
89
                                        }
90
91
                                        return ['bookNotFound', 'Unknown book!'];
92
                                    },
93
                                ],
94
                            ],
95
                            'resolve' => static function ($value) : bool {
96
                                return !!$value;
97
                            },
98
                        ]),
99
                    ];
100
                },
101
            ]),
102
        ]);
103
    }
104
105
    public function testNullableScalarValidationOnNullValueSuccess() : void
106
    {
107
        $res = GraphQL::executeQuery(
108
            $this->schema,
109
            Utils::nowdoc('
110
                mutation UpdateBook(
111
                    $bookId:ID
112
                ) {
113
                    updateBook (bookId: $bookId) {
114
                        valid
115
                        fields {
116
                            bookId {
117
                                code
118
                                msg
119
                            }
120
                        }
121
                        result {
122
                            title
123
                        }
124
                    }
125
                }
126
            '),
127
            [],
128
            null,
129
            ['bookId' => null]
130
        );
131
132
        static::assertEmpty($res->errors);
133
        static::assertFalse($res->data['updateBook']['valid']);
134
    }
135
}
136