Completed
Push — master ( d52ee7...e7842c )
by Alexandr
03:54
created

ResolveValidatorTest::testValidMethods()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 28
rs 8.8571
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
/*
3
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 5/15/16 5:09 PM
7
*/
8
9
namespace Youshido\Tests\Library\Validator;
10
11
12
use Youshido\GraphQL\Execution\Context\ExecutionContext;
13
use Youshido\GraphQL\Field\Field;
14
use Youshido\GraphQL\Parser\Ast\Argument;
15
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Literal;
16
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Variable;
17
use Youshido\GraphQL\Parser\Ast\Field as AstField;
18
use Youshido\GraphQL\Parser\Ast\Fragment;
19
use Youshido\GraphQL\Parser\Ast\FragmentReference;
20
use Youshido\GraphQL\Parser\Ast\Query;
21
use Youshido\GraphQL\Execution\Request;
22
use Youshido\GraphQL\Type\NonNullType;
23
use Youshido\GraphQL\Type\Object\ObjectType;
24
use Youshido\GraphQL\Type\Scalar\BooleanType;
25
use Youshido\GraphQL\Type\Scalar\IntType;
26
use Youshido\GraphQL\Type\Scalar\StringType;
27
use Youshido\GraphQL\Type\Union\UnionType;
28
use Youshido\GraphQL\Validator\ResolveValidator\ResolveValidator;
29
use Youshido\Tests\DataProvider\TestEnumType;
30
use Youshido\Tests\DataProvider\TestInterfaceType;
31
use Youshido\Tests\DataProvider\TestObjectType;
32
33
class ResolveValidatorTest extends \PHPUnit_Framework_TestCase
34
{
35
36
    public function testValidMethods()
37
    {
38
        $validator = new ResolveValidator(new ExecutionContext());
39
        $this->assertEmpty($validator->getExecutionContext()->getErrors());
40
41
        $object       = new TestObjectType();
42
        $fieldName    = new AstField('name');
43
        $fieldSummary = new AstField('summary');
44
        $this->assertTrue($validator->objectHasField($object, $fieldName));
45
        $this->assertEmpty($validator->getExecutionContext()->getErrors());
46
47
        $this->assertFalse($validator->objectHasField($object, $fieldSummary));
48
        $this->assertNotEmpty($validator->getExecutionContext()->getErrors());
49
50
        $userType = new ObjectType([
51
            'name'       => 'User',
52
            'fields'     => [
53
                'name' => new StringType(),
54
            ],
55
            'interfaces' => [new TestInterfaceType()]
56
        ]);
57
58
        $validator->assertTypeImplementsInterface($userType, new TestInterfaceType());
59
60
        $fragment          = new Fragment('name', 'User', []);
61
        $fragmentReference = new FragmentReference('name');
62
        $validator->assertValidFragmentForField($fragment, $fragmentReference, $userType);
63
    }
64
65
    /**
66
     * @expectedException \Youshido\GraphQL\Validator\Exception\ResolveException
67
     */
68
    public function testInvalidFragmentType()
69
    {
70
        $userType          = new ObjectType([
71
            'name'   => 'User',
72
            'fields' => [
73
                'name' => new StringType(),
74
            ],
75
        ]);
76
        $fragmentReference = new FragmentReference('user');
77
        $fragment          = new Fragment('name', 'Product', []);
78
79
        $validator = new ResolveValidator(new ExecutionContext());
80
        $validator->assertValidFragmentForField($fragment, $fragmentReference, $userType);
81
    }
82
83
    /**
84
     * @expectedException \Youshido\GraphQL\Validator\Exception\ResolveException
85
     */
86
    public function testInvalidInterfaceTypeResolve()
87
    {
88
        $userType = new ObjectType([
89
            'name'   => 'User',
90
            'fields' => [
91
                'name' => new StringType(),
92
            ],
93
        ]);
94
95
        $validator = new ResolveValidator(new ExecutionContext());
96
        $validator->assertTypeImplementsInterface($userType, new TestInterfaceType());
97
    }
98
99
    /**
100
     * @expectedException \Youshido\GraphQL\Validator\Exception\ResolveException
101
     */
102
    public function testInvalidUnionTypeResolve()
103
    {
104
        $union = new UnionType([
105
            'name'        => 'TestUnion',
106
            'types'       => [new TestObjectType()],
107
            'resolveType' => function () {
108
                return new BooleanType();
109
            }
110
        ]);
111
112
        $validator = new ResolveValidator(new ExecutionContext());
113
        $validator->assertTypeInUnionTypes($union->resolveType(new \stdClass()), $union);
114
    }
115
116
    public function testValidUnionTypeResolve()
117
    {
118
        $union = new UnionType([
119
            'name'        => 'TestUnion',
120
            'types'       => [new TestObjectType()],
121
            'resolveType' => function () {
122
                return new TestObjectType();
123
            }
124
        ]);
125
126
        $validator = new ResolveValidator(new ExecutionContext());
127
        try {
128
            $validator->assertTypeInUnionTypes($union->resolveType(new \stdClass()), $union);
129
            $this->assertTrue(true);
130
        } catch (\Exception $e) {
131
            $this->assertTrue(false);
132
        }
133
    }
134
135
    public function testArgumentsValidation()
136
    {
137
        $variable          = new Variable('year', 'Int');
138
        $variableWrongType = new Variable('year', 'String');
139
        $variable->setValue(2016);
140
141
142
        $field     = new Field([
143
            'name' => 'hero',
144
            'type' => new ObjectType([
145
                'name'   => 'User',
146
                'fields' => [
147
                    'name' => new StringType()
148
                ]
149
            ]),
150
            'args' => [
151
                'planet' => new StringType(),
152
                'year'   => new IntType(),
153
            ]
154
        ]);
155
        $validator = new ResolveValidator(new ExecutionContext());
156
        $request   = new Request([]);
157
158
159
        $validQuery                    = new Query('hero', null, [
160
            new Argument('planet', new Literal('earth'))
161
        ]);
162
        $invalidArgumentQuery          = new Query('hero', null, [
163
            new Argument('planets', new Literal('earth'))
164
        ]);
165
        $invalidArgumentTypeQuery      = new Query('hero', null, [
166
            new Argument('year', new Literal('invalid type'))
167
        ]);
168
        $argumentWithVariable          = new Query('hero', null, [
169
            new Argument('year', $variable)
170
        ]);
171
        $argumentWithVariableWrongType = new Query('hero', null, [
172
            new Argument('year', $variableWrongType)
173
        ]);
174
175
176
        $this->assertFalse($validator->getExecutionContext()->hasErrors());
177
178
        $validator->validateArguments($field, $validQuery, $request);
179
        $this->assertFalse($validator->getExecutionContext()->hasErrors());
180
181
        $validator->validateArguments($field, $invalidArgumentQuery, $request);
182
        $this->assertEquals([
183
            ['message' => 'Unknown argument "planets" on field "hero"']
184
        ], $validator->getExecutionContext()->getErrorsArray());
185
        $validator->getExecutionContext()->clearErrors();
186
187
        $validator->validateArguments($field, $invalidArgumentTypeQuery, $request);
188
        $this->assertEquals([
189
            ['message' => 'Not valid type for argument "year" in query "hero"']
190
        ], $validator->getExecutionContext()->getErrorsArray());
191
        $validator->getExecutionContext()->clearErrors();
192
193
        $validator->validateArguments($field, $argumentWithVariable, $request);
194
        $this->assertEquals([
195
            ['message' => 'Variable "year" does not exist for query "hero"']
196
        ], $validator->getExecutionContext()->getErrorsArray());
197
        $validator->getExecutionContext()->clearErrors();
198
199
        $request->setVariables(['year' => '2016']);
200
        $validator->validateArguments($field, $argumentWithVariable, $request);
201
        $this->assertFalse($validator->getExecutionContext()->hasErrors());
202
203
        $validator->validateArguments($field, $argumentWithVariableWrongType, $request);
204
        $this->assertEquals([
205
            ['message' => 'Invalid variable "year" type, allowed type is "Int"']
206
        ], $validator->getExecutionContext()->getErrorsArray());
207
        $validator->getExecutionContext()->clearErrors();
208
209
210
    }
211
}
212