1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Tests\Type; |
6
|
|
|
|
7
|
|
|
use GraphQL\Error\InvariantViolation; |
8
|
|
|
use GraphQL\Type\Definition\CustomScalarType; |
9
|
|
|
use GraphQL\Type\Definition\Type; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use stdClass; |
12
|
|
|
|
13
|
|
|
class StandardTypesTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** @var Type[] */ |
16
|
|
|
private static $originalStandardTypes; |
17
|
|
|
|
18
|
|
|
public static function setUpBeforeClass() |
19
|
|
|
{ |
20
|
|
|
self::$originalStandardTypes = Type::getStandardTypes(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function tearDown() |
24
|
|
|
{ |
25
|
|
|
parent::tearDown(); |
26
|
|
|
Type::overrideStandardTypes(self::$originalStandardTypes); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testAllowsOverridingStandardTypes() |
30
|
|
|
{ |
31
|
|
|
$originalTypes = Type::getStandardTypes(); |
32
|
|
|
self::assertCount(5, $originalTypes); |
33
|
|
|
self::assertSame(self::$originalStandardTypes, $originalTypes); |
34
|
|
|
|
35
|
|
|
$newBooleanType = $this->createCustomScalarType(Type::BOOLEAN); |
36
|
|
|
$newFloatType = $this->createCustomScalarType(Type::FLOAT); |
37
|
|
|
$newIDType = $this->createCustomScalarType(Type::ID); |
38
|
|
|
$newIntType = $this->createCustomScalarType(Type::INT); |
39
|
|
|
$newStringType = $this->createCustomScalarType(Type::STRING); |
40
|
|
|
|
41
|
|
|
Type::overrideStandardTypes([ |
42
|
|
|
$newStringType, |
43
|
|
|
$newBooleanType, |
44
|
|
|
$newIDType, |
45
|
|
|
$newIntType, |
46
|
|
|
$newFloatType, |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
$types = Type::getStandardTypes(); |
50
|
|
|
self::assertCount(5, $types); |
51
|
|
|
|
52
|
|
|
self::assertSame($newBooleanType, $types[Type::BOOLEAN]); |
53
|
|
|
self::assertSame($newFloatType, $types[Type::FLOAT]); |
54
|
|
|
self::assertSame($newIDType, $types[Type::ID]); |
55
|
|
|
self::assertSame($newIntType, $types[Type::INT]); |
56
|
|
|
self::assertSame($newStringType, $types[Type::STRING]); |
57
|
|
|
|
58
|
|
|
self::assertSame($newBooleanType, Type::boolean()); |
59
|
|
|
self::assertSame($newFloatType, Type::float()); |
60
|
|
|
self::assertSame($newIDType, Type::id()); |
61
|
|
|
self::assertSame($newIntType, Type::int()); |
62
|
|
|
self::assertSame($newStringType, Type::string()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testPreservesOriginalStandardTypes() |
66
|
|
|
{ |
67
|
|
|
$originalTypes = Type::getStandardTypes(); |
68
|
|
|
self::assertCount(5, $originalTypes); |
69
|
|
|
self::assertSame(self::$originalStandardTypes, $originalTypes); |
70
|
|
|
|
71
|
|
|
$newIDType = $this->createCustomScalarType(Type::ID); |
72
|
|
|
$newStringType = $this->createCustomScalarType(Type::STRING); |
73
|
|
|
|
74
|
|
|
Type::overrideStandardTypes([ |
75
|
|
|
$newStringType, |
76
|
|
|
$newIDType, |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
$types = Type::getStandardTypes(); |
80
|
|
|
self::assertCount(5, $types); |
81
|
|
|
|
82
|
|
|
self::assertSame($originalTypes[Type::BOOLEAN], $types[Type::BOOLEAN]); |
83
|
|
|
self::assertSame($originalTypes[Type::FLOAT], $types[Type::FLOAT]); |
84
|
|
|
self::assertSame($originalTypes[Type::INT], $types[Type::INT]); |
85
|
|
|
|
86
|
|
|
self::assertSame($originalTypes[Type::BOOLEAN], Type::boolean()); |
87
|
|
|
self::assertSame($originalTypes[Type::FLOAT], Type::float()); |
88
|
|
|
self::assertSame($originalTypes[Type::INT], Type::int()); |
89
|
|
|
|
90
|
|
|
self::assertSame($newIDType, $types[Type::ID]); |
91
|
|
|
self::assertSame($newStringType, $types[Type::STRING]); |
92
|
|
|
|
93
|
|
|
self::assertSame($newIDType, Type::id()); |
94
|
|
|
self::assertSame($newStringType, Type::string()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getInvalidStandardTypes() |
98
|
|
|
{ |
99
|
|
|
return [ |
100
|
|
|
[null, 'Expecting instance of GraphQL\Type\Definition\Type, got null'], |
101
|
|
|
[5, 'Expecting instance of GraphQL\Type\Definition\Type, got 5'], |
102
|
|
|
['', 'Expecting instance of GraphQL\Type\Definition\Type, got (empty string)'], |
103
|
|
|
[new stdClass(), 'Expecting instance of GraphQL\Type\Definition\Type, got instance of stdClass'], |
104
|
|
|
[[], 'Expecting instance of GraphQL\Type\Definition\Type, got []'], |
105
|
|
|
[$this->createCustomScalarType('NonStandardName'), 'Expecting one of the following names for a standard type: ID, String, Float, Int, Boolean, got NonStandardName'], |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @dataProvider getInvalidStandardTypes |
111
|
|
|
*/ |
112
|
|
|
public function testStandardTypesOverrideDoesSanityChecks($type, string $expectedMessage) |
113
|
|
|
{ |
114
|
|
|
$this->expectException(InvariantViolation::class); |
115
|
|
|
$this->expectExceptionMessage($expectedMessage); |
116
|
|
|
|
117
|
|
|
Type::overrideStandardTypes([ $type ]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function createCustomScalarType($name) |
121
|
|
|
{ |
122
|
|
|
return new CustomScalarType([ |
123
|
|
|
'name' => $name, |
124
|
|
|
'serialize' => static function () { |
125
|
|
|
}, |
126
|
|
|
'parseValue' => static function () { |
127
|
|
|
}, |
128
|
|
|
'parseLiteral' => static function () { |
129
|
|
|
}, |
130
|
|
|
]); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|