Passed
Push — master ( fb5c25...257e8e )
by Max
05:13
created

NonNullScalarValidationTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 146
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testNonNullScalarValidationSuccess() 0 28 1
A setUp() 0 55 2
A testNonNullScalarValidationFail() 0 29 1
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 NonNullScalarValidationTest 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 Schema */
37
    protected $schema;
38
39
    protected function setUp()
40
    {
41
        $this->personType = new ObjectType([
42
            'name' => 'Person',
43
            'fields' => [
44
                'firstName' => [
45
                    'type' => Type::string(),
46
                    'phoneNumbers' => [
47
                        'type' => Type::listOf(Type::string()),
48
                    ],
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->schema = new Schema([
72
            'mutation' => new ObjectType([
73
                'name' => 'Mutation',
74
                'fields' => function () {
75
                    return [
76
                        'updateBook' => new ValidatedFieldDefinition([
77
                            'name' => 'updateBook',
78
                            'type' => $this->bookType,
79
                            'args' => [
80
                                'bookId' => [
81
                                    'type' => Type::nonNull(Type::id()),
82
                                    'errorCodes' => ['bookNotFound'],
83
                                    'validate' => function ($bookId) {
84
                                        if (isset($this->data['books'][$bookId])) {
85
                                            return 0;
86
                                        }
87
88
                                        return ['bookNotFound', 'Unknown book!'];
89
                                    },
90
                                ],
91
                            ],
92
                            'resolve' => function ($value, $args) : array {
93
                                return $this->data['books'][$args['bookId']];
94
                            },
95
                        ]),
96
                    ];
97
                },
98
            ]),
99
        ]);
100
    }
101
102
    public function testNonNullScalarValidationSuccess()
103
    {
104
        $res = GraphQL::executeQuery(
105
            $this->schema,
106
            Utils::nowdoc('
107
				mutation UpdateBook(
108
                        $bookId:ID!
109
                    ) {
110
                        updateBook (bookId: $bookId) {
111
                            valid
112
                            suberrors {
113
                                bookId {
114
                                    code
115
                                    msg
116
                                }
117
                            }
118
                            result {
119
                                title
120
                            }
121
                        }
122
                    }
123
			'),
124
            [],
125
            null,
126
            ['bookId' => 1]
127
        );
128
129
        static::assertTrue($res->data['updateBook']['valid']);
130
    }
131
132
    public function testNonNullScalarValidationFail()
133
    {
134
        $res = GraphQL::executeQuery(
135
            $this->schema,
136
            Utils::nowdoc('
137
				mutation UpdateBook(
138
                        $bookId:ID!
139
                    ) {
140
                        updateBook (bookId: $bookId) {
141
                            valid
142
                            suberrors {
143
                                bookId {
144
                                    code
145
                                    msg
146
                                }
147
                            }
148
                            result {
149
                                title
150
                            }
151
                        }
152
                    }
153
			'),
154
            [],
155
            null,
156
            ['bookId' => 37]
157
        );
158
159
        static::assertEmpty($res->errors);
160
        static::assertFalse($res->data['updateBook']['valid']);
161
    }
162
}
163