Completed
Push — master ( 786636...6c7957 )
by Alexandr
03:01
created

SchemaTest::testStandaloneSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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/12/16 10:11 PM
7
*/
8
9
namespace Youshido\Tests\Schema;
10
11
12
use Youshido\GraphQL\Execution\Processor;
13
use Youshido\GraphQL\Schema\Schema;
14
use Youshido\GraphQL\Type\NonNullType;
15
use Youshido\GraphQL\Type\Object\ObjectType;
16
use Youshido\GraphQL\Type\Scalar\IntType;
17
use Youshido\GraphQL\Type\Scalar\StringType;
18
use Youshido\Tests\DataProvider\TestEmptySchema;
19
use Youshido\Tests\DataProvider\TestObjectType;
20
use Youshido\Tests\DataProvider\TestSchema;
21
22
class SchemaTest extends \PHPUnit_Framework_TestCase
23
{
24
25
    public function testStandaloneEmptySchema()
26
    {
27
        $schema = new TestEmptySchema();
28
        $this->assertFalse($schema->getQueryType()->hasFields());
29
    }
30
31
    public function testStandaloneSchema()
32
    {
33
        $schema = new TestSchema();
34
        $this->assertTrue($schema->getQueryType()->hasFields());
35
        $this->assertTrue($schema->getMutationType()->hasFields());
36
37
        $this->assertEquals(1, count($schema->getMutationType()->getFields()));
38
39
        $schema->addMutationField('changeUser', ['type' => new TestObjectType(), 'resolve' => function () {
40
        }]);
41
        $this->assertEquals(2, count($schema->getMutationType()->getFields()));
42
43
    }
44
45
    public function testSchemaWithoutClosuresSerializable()
46
    {
47
        $schema = new TestEmptySchema();
48
        $schema->getQueryType()->addField('randomInt', [
49
            'type'    => new NonNullType(new IntType()),
50
            'resolve' => 'rand',
51
        ]);
52
53
        $serialized = serialize($schema);
54
        /** @var Schema $unserialized */
55
        $unserialized = unserialize($serialized);
56
57
        $this->assertTrue($unserialized->getQueryType()->hasFields());
58
        $this->assertFalse($unserialized->getMutationType()->hasFields());
59
        $this->assertEquals(1, count($unserialized->getQueryType()->getFields()));
60
    }
61
62
    public function testCustomTypes()
63
    {
64
        $authorType = null;
65
66
        $userInterface = new ObjectType([
67
            'name'        => 'UserInterface',
68
            'fields'      => [
69
                'name' => new StringType(),
70
            ],
71
            'resolveType' => function () use ($authorType) {
72
                return $authorType;
73
            }
74
        ]);
75
76
        $authorType = new ObjectType([
77
            'name'       => 'Author',
78
            'fields'     => [
79
                'name' => new StringType(),
80
            ],
81
            'interfaces' => [$userInterface]
82
        ]);
83
84
        $schema = new Schema([
85
            'query' => new ObjectType([
86
                'name'   => 'QueryType',
87
                'fields' => [
88
                    'user' => [
89
                        'type'    => $userInterface,
90
                        'resolve' => function () {
91
                            return [
92
                                'name' => 'Alex'
93
                            ];
94
                        }
95
                    ]
96
                ]
97
            ])
98
        ]);
99
        $schema->getTypesList()->addType($authorType);
100
        $processor = new Processor($schema);
101
        $processor->processPayload('{ user { name } }');
102
        $this->assertEquals(['data' => ['user' => ['name' => 'Alex']]], $processor->getResponseData());
103
104
        $processor->processPayload('{
105
                    __schema {
106
                        types {
107
                            name
108
                        }
109
                    }
110
                }');
111
        $data = $processor->getResponseData();
112
        $this->assertArraySubset([10 => ['name' => 'Author']], $data['data']['__schema']['types']);
113
114
        $processor->processPayload('{ user { name { } } }');
115
        $result = $processor->getResponseData();
116
117
        $this->assertEquals(['errors' => [[
118
            'message'   => 'Unexpected token "RBRACE"',
119
            'locations' => [
120
                [
121
                    'line'   => 1,
122
                    'column' => 19
123
                ]
124
            ]
125
        ]]], $result);
126
        $processor->getExecutionContext()->clearErrors();
127
128
        $processor->processPayload('{ user { name { invalidSelection } } }');
129
        $result = $processor->getResponseData();
130
131
        $this->assertEquals(['data' => ['user' => null], 'errors' => [[
132
            'message'   => 'You can\'t specify fields for scalar type "String"',
133
            'locations' => [
134
                [
135
                    'line'   => 1,
136
                    'column' => 10
137
                ]
138
            ]
139
        ]]], $result);
140
    }
141
142
}
143