1
|
|
|
<?php |
2
|
|
|
namespace GraphQL\Tests\Utils; |
3
|
|
|
|
4
|
|
|
use GraphQL\Language\Parser; |
5
|
|
|
use GraphQL\Utils\AST; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class AstFromValueUntypedTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
// Describe: valueFromASTUntyped |
11
|
|
|
|
12
|
|
|
private function assertTestCase($valueText, $expected, array $variables = null) { |
13
|
|
|
$this->assertEquals( |
14
|
|
|
$expected, |
15
|
|
|
AST::valueFromASTUntyped(Parser::parseValue($valueText), $variables) |
16
|
|
|
); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @it parses simple values |
21
|
|
|
*/ |
22
|
|
|
public function testParsesSimpleValues() |
23
|
|
|
{ |
24
|
|
|
$this->assertTestCase('null', null); |
25
|
|
|
$this->assertTestCase('true', true); |
26
|
|
|
$this->assertTestCase('false', false); |
27
|
|
|
$this->assertTestCase('123', 123); |
28
|
|
|
$this->assertTestCase('123.456', 123.456); |
29
|
|
|
$this->assertTestCase('abc123', 'abc123'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @it parses lists of values |
34
|
|
|
*/ |
35
|
|
|
public function testParsesListsOfValues() |
36
|
|
|
{ |
37
|
|
|
$this->assertTestCase('[true, false]', [true, false]); |
38
|
|
|
$this->assertTestCase('[true, 123.45]', [true, 123.45]); |
39
|
|
|
$this->assertTestCase('[true, null]', [true, null]); |
40
|
|
|
$this->assertTestCase('[true, ["foo", 1.2]]', [true, ['foo', 1.2]]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @it parses input objects |
45
|
|
|
*/ |
46
|
|
|
public function testParsesInputObjects() |
47
|
|
|
{ |
48
|
|
|
$this->assertTestCase( |
49
|
|
|
'{ int: 123, bool: false }', |
50
|
|
|
['int' => 123, 'bool' => false] |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$this->assertTestCase( |
54
|
|
|
'{ foo: [ { bar: "baz"} ] }', |
55
|
|
|
['foo' => [['bar' => 'baz']]] |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @it parses enum values as plain strings |
61
|
|
|
*/ |
62
|
|
|
public function testParsesEnumValuesAsPlainStrings() |
63
|
|
|
{ |
64
|
|
|
$this->assertTestCase( |
65
|
|
|
'TEST_ENUM_VALUE', |
66
|
|
|
'TEST_ENUM_VALUE' |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$this->assertTestCase( |
70
|
|
|
'[TEST_ENUM_VALUE]', |
71
|
|
|
['TEST_ENUM_VALUE'] |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @it parses enum values as plain strings |
77
|
|
|
*/ |
78
|
|
|
public function testParsesVariables() |
79
|
|
|
{ |
80
|
|
|
$this->assertTestCase( |
81
|
|
|
'$testVariable', |
82
|
|
|
'foo', |
83
|
|
|
['testVariable' => 'foo'] |
84
|
|
|
); |
85
|
|
|
$this->assertTestCase( |
86
|
|
|
'[$testVariable]', |
87
|
|
|
['foo'], |
88
|
|
|
['testVariable' => 'foo'] |
89
|
|
|
); |
90
|
|
|
$this->assertTestCase( |
91
|
|
|
'{a:[$testVariable]}', |
92
|
|
|
['a' => ['foo']], |
93
|
|
|
['testVariable' => 'foo'] |
94
|
|
|
); |
95
|
|
|
$this->assertTestCase( |
96
|
|
|
'$testVariable', |
97
|
|
|
null, |
98
|
|
|
['testVariable' => null] |
99
|
|
|
); |
100
|
|
|
$this->assertTestCase( |
101
|
|
|
'$testVariable', |
102
|
|
|
null, |
103
|
|
|
[] |
104
|
|
|
); |
105
|
|
|
$this->assertTestCase( |
106
|
|
|
'$testVariable', |
107
|
|
|
null, |
108
|
|
|
null |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|