|
1
|
|
|
<?php |
|
2
|
|
|
namespace GraphQL\Tests\Error; |
|
3
|
|
|
|
|
4
|
|
|
use GraphQL\Error\Error; |
|
5
|
|
|
use GraphQL\Error\FormattedError; |
|
6
|
|
|
use GraphQL\Language\Parser; |
|
7
|
|
|
use GraphQL\Language\Source; |
|
8
|
|
|
use GraphQL\Language\SourceLocation; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
|
|
11
|
|
|
class PrintErrorTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
// Describe printError |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @it prints an line numbers with correct padding |
|
17
|
|
|
*/ |
|
18
|
|
|
public function testPrintsAnLineNumbersWithCorrectPadding() |
|
19
|
|
|
{ |
|
20
|
|
|
$singleDigit = new Error( |
|
21
|
|
|
'Single digit line number with no padding', |
|
22
|
|
|
null, |
|
23
|
|
|
new Source('*', 'Test', new SourceLocation(9, 1)), |
|
24
|
|
|
[0] |
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
|
|
$actual = FormattedError::printError($singleDigit); |
|
28
|
|
|
$expected = 'Single digit line number with no padding |
|
29
|
|
|
|
|
30
|
|
|
Test (9:1) |
|
31
|
|
|
9: * |
|
32
|
|
|
^ |
|
33
|
|
|
'; |
|
34
|
|
|
$this->assertEquals($expected, $actual); |
|
35
|
|
|
|
|
36
|
|
|
$doubleDigit = new Error( |
|
37
|
|
|
'Left padded first line number', |
|
38
|
|
|
null, |
|
39
|
|
|
new Source("*\n", 'Test', new SourceLocation(9, 1)), |
|
40
|
|
|
[0] |
|
41
|
|
|
); |
|
42
|
|
|
$actual = FormattedError::printError($doubleDigit); |
|
43
|
|
|
$expected = 'Left padded first line number |
|
44
|
|
|
|
|
45
|
|
|
Test (9:1) |
|
46
|
|
|
9: * |
|
47
|
|
|
^ |
|
48
|
|
|
10: |
|
49
|
|
|
'; |
|
50
|
|
|
$this->assertEquals($expected, $actual); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @it prints an error with nodes from different sources |
|
55
|
|
|
*/ |
|
56
|
|
|
public function testPrintsAnErrorWithNodesFromDifferentSources() |
|
57
|
|
|
{ |
|
58
|
|
|
$sourceA = Parser::parse(new Source('type Foo { |
|
59
|
|
|
field: String |
|
60
|
|
|
}', |
|
61
|
|
|
'SourceA' |
|
62
|
|
|
)); |
|
63
|
|
|
|
|
64
|
|
|
$fieldTypeA = $sourceA->definitions[0]->fields[0]->type; |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$sourceB = Parser::parse(new Source('type Foo { |
|
67
|
|
|
field: Int |
|
68
|
|
|
}', |
|
69
|
|
|
'SourceB' |
|
70
|
|
|
)); |
|
71
|
|
|
|
|
72
|
|
|
$fieldTypeB = $sourceB->definitions[0]->fields[0]->type; |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
$error = new Error( |
|
76
|
|
|
'Example error with two nodes', |
|
77
|
|
|
[ |
|
78
|
|
|
$fieldTypeA, |
|
79
|
|
|
$fieldTypeB, |
|
80
|
|
|
] |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertEquals( |
|
84
|
|
|
'Example error with two nodes |
|
85
|
|
|
|
|
86
|
|
|
SourceA (2:10) |
|
87
|
|
|
1: type Foo { |
|
88
|
|
|
2: field: String |
|
89
|
|
|
^ |
|
90
|
|
|
3: } |
|
91
|
|
|
|
|
92
|
|
|
SourceB (2:10) |
|
93
|
|
|
1: type Foo { |
|
94
|
|
|
2: field: Int |
|
95
|
|
|
^ |
|
96
|
|
|
3: } |
|
97
|
|
|
', |
|
98
|
|
|
FormattedError::printError($error) |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|