Failed Conditions
Pull Request — master (#333)
by Jérémiah
04:09
created

SchemaPrinterTest::testDoesNotAlterAst()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Tests\Language;
6
7
use GraphQL\Language\AST\NameNode;
8
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
9
use GraphQL\Language\Parser;
10
use GraphQL\Language\Printer;
11
use PHPUnit\Framework\TestCase;
12
use Throwable;
13
use function file_get_contents;
14
15
class SchemaPrinterTest extends TestCase
16
{
17
    /**
18
     * @see it('prints minimal ast')
19
     */
20
    public function testPrintsMinimalAst() : void
21
    {
22
        $ast = new ScalarTypeDefinitionNode([
23
            'name' => new NameNode(['value' => 'foo']),
24
        ]);
25
        $this->assertEquals('scalar foo', Printer::doPrint($ast));
26
    }
27
28
    /**
29
     * @see it('produces helpful error messages')
30
     */
31
    public function testProducesHelpfulErrorMessages() : void
32
    {
33
        $this->expectException(Throwable::class);
34
        $this->expectExceptionMessage('Invalid AST Node: {"random":"Data"}');
35
36
        // $badAst1 = { random: 'Data' };
37
        $badAst = (object) ['random' => 'Data'];
38
        Printer::doPrint($badAst);
39
    }
40
41
    /**
42
     * @see it('does not alter ast')
43
     */
44
    public function testDoesNotAlterAst() : void
45
    {
46
        $kitchenSink = file_get_contents(__DIR__ . '/schema-kitchen-sink.graphql');
47
48
        $ast     = Parser::parse($kitchenSink);
49
        $astCopy = $ast->cloneDeep();
50
        Printer::doPrint($ast);
51
52
        $this->assertEquals($astCopy, $ast);
53
    }
54
55
    public function testPrintsKitchenSink() : void
56
    {
57
        $kitchenSink = file_get_contents(__DIR__ . '/schema-kitchen-sink.graphql');
58
59
        $ast     = Parser::parse($kitchenSink);
60
        $printed = Printer::doPrint($ast);
61
62
        $expected = 'schema {
63
  query: QueryType
64
  mutation: MutationType
65
}
66
67
"""
68
This is a description
69
of the `Foo` type.
70
"""
71
type Foo implements Bar & Baz {
72
  one: Type
73
  two(argument: InputType!): Type
74
  three(argument: InputType, other: String): Int
75
  four(argument: String = "string"): String
76
  five(argument: [String] = ["string", "string"]): String
77
  six(argument: InputType = {key: "value"}): Type
78
  seven(argument: Int = null): Type
79
}
80
81
type AnnotatedObject @onObject(arg: "value") {
82
  annotatedField(arg: Type = "default" @onArg): Type @onField
83
}
84
85
type UndefinedType
86
87
extend type Foo {
88
  seven(argument: [String]): Type
89
}
90
91
extend type Foo @onType
92
93
interface Bar {
94
  one: Type
95
  four(argument: String = "string"): String
96
}
97
98
interface AnnotatedInterface @onInterface {
99
  annotatedField(arg: Type @onArg): Type @onField
100
}
101
102
interface UndefinedInterface
103
104
extend interface Bar {
105
  two(argument: InputType!): Type
106
}
107
108
extend interface Bar @onInterface
109
110
union Feed = Story | Article | Advert
111
112
union AnnotatedUnion @onUnion = A | B
113
114
union AnnotatedUnionTwo @onUnion = A | B
115
116
union UndefinedUnion
117
118
extend union Feed = Photo | Video
119
120
extend union Feed @onUnion
121
122
scalar CustomScalar
123
124
scalar AnnotatedScalar @onScalar
125
126
extend scalar CustomScalar @onScalar
127
128
enum Site {
129
  DESKTOP
130
  MOBILE
131
}
132
133
enum AnnotatedEnum @onEnum {
134
  ANNOTATED_VALUE @onEnumValue
135
  OTHER_VALUE
136
}
137
138
enum UndefinedEnum
139
140
extend enum Site {
141
  VR
142
}
143
144
extend enum Site @onEnum
145
146
input InputType {
147
  key: String!
148
  answer: Int = 42
149
}
150
151
input AnnotatedInput @onInputObject {
152
  annotatedField: Type @onField
153
}
154
155
input UndefinedInput
156
157
extend input InputType {
158
  other: Float = 1.23e4
159
}
160
161
extend input InputType @onInputObject
162
163
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
164
165
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
166
167
directive @include2(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
168
';
169
        $this->assertEquals($expected, $printed);
170
    }
171
}
172