1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Tests\Type; |
6
|
|
|
|
7
|
|
|
use GraphQL\Error\Error; |
8
|
|
|
use GraphQL\Type\Definition\Type; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use stdClass; |
11
|
|
|
|
12
|
|
|
class ScalarSerializationTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
// Type System: Scalar coercion |
15
|
|
|
/** |
16
|
|
|
* @see it('serializes output as Int') |
17
|
|
|
*/ |
18
|
|
|
public function testSerializesOutputAsInt() : void |
19
|
|
|
{ |
20
|
|
|
$intType = Type::int(); |
21
|
|
|
|
22
|
|
|
self::assertSame(1, $intType->serialize(1)); |
23
|
|
|
self::assertSame(123, $intType->serialize('123')); |
24
|
|
|
self::assertSame(0, $intType->serialize(0)); |
25
|
|
|
self::assertSame(-1, $intType->serialize(-1)); |
26
|
|
|
self::assertSame(100000, $intType->serialize(1e5)); |
27
|
|
|
self::assertSame(0, $intType->serialize(0e5)); |
28
|
|
|
self::assertSame(0, $intType->serialize(false)); |
29
|
|
|
self::assertSame(1, $intType->serialize(true)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testSerializesOutputIntCannotRepresentFloat1() : void |
33
|
|
|
{ |
34
|
|
|
// The GraphQL specification does not allow serializing non-integer values |
35
|
|
|
// as Int to avoid accidental data loss. |
36
|
|
|
$intType = Type::int(); |
37
|
|
|
$this->expectException(Error::class); |
38
|
|
|
$this->expectExceptionMessage('Int cannot represent non-integer value: 0.1'); |
39
|
|
|
$intType->serialize(0.1); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testSerializesOutputIntCannotRepresentFloat2() : void |
43
|
|
|
{ |
44
|
|
|
$intType = Type::int(); |
45
|
|
|
$this->expectException(Error::class); |
46
|
|
|
$this->expectExceptionMessage('Int cannot represent non-integer value: 1.1'); |
47
|
|
|
$intType->serialize(1.1); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testSerializesOutputIntCannotRepresentNegativeFloat() : void |
51
|
|
|
{ |
52
|
|
|
$intType = Type::int(); |
53
|
|
|
$this->expectException(Error::class); |
54
|
|
|
$this->expectExceptionMessage('Int cannot represent non-integer value: -1.1'); |
55
|
|
|
$intType->serialize(-1.1); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testSerializesOutputIntCannotRepresentNumericString() : void |
59
|
|
|
{ |
60
|
|
|
$intType = Type::int(); |
61
|
|
|
$this->expectException(Error::class); |
62
|
|
|
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: Int cannot represent non-integer value: "-1.1"'); |
63
|
|
|
$intType->serialize('Int cannot represent non-integer value: "-1.1"'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testSerializesOutputIntCannotRepresentBiggerThan32Bits() : void |
67
|
|
|
{ |
68
|
|
|
// Maybe a safe PHP int, but bigger than 2^32, so not |
69
|
|
|
// representable as a GraphQL Int |
70
|
|
|
$intType = Type::int(); |
71
|
|
|
$this->expectException(Error::class); |
72
|
|
|
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: 9876504321'); |
73
|
|
|
$intType->serialize(9876504321); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testSerializesOutputIntCannotRepresentLowerThan32Bits() : void |
77
|
|
|
{ |
78
|
|
|
$intType = Type::int(); |
79
|
|
|
$this->expectException(Error::class); |
80
|
|
|
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: -9876504321'); |
81
|
|
|
$intType->serialize(-9876504321); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testSerializesOutputIntCannotRepresentBiggerThanSigned32Bits() : void |
85
|
|
|
{ |
86
|
|
|
$intType = Type::int(); |
87
|
|
|
$this->expectException(Error::class); |
88
|
|
|
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: 1.0E+100'); |
89
|
|
|
$intType->serialize(1e100); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testSerializesOutputIntCannotRepresentLowerThanSigned32Bits() : void |
93
|
|
|
{ |
94
|
|
|
$intType = Type::int(); |
95
|
|
|
$this->expectException(Error::class); |
96
|
|
|
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: -1.0E+100'); |
97
|
|
|
$intType->serialize(-1e100); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testSerializesOutputIntCannotRepresentString() : void |
101
|
|
|
{ |
102
|
|
|
$intType = Type::int(); |
103
|
|
|
$this->expectException(Error::class); |
104
|
|
|
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: one'); |
105
|
|
|
$intType->serialize('one'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testSerializesOutputIntCannotRepresentEmptyString() : void |
109
|
|
|
{ |
110
|
|
|
$intType = Type::int(); |
111
|
|
|
$this->expectException(Error::class); |
112
|
|
|
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: (empty string)'); |
113
|
|
|
$intType->serialize(''); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @see it('serializes output as Float') |
118
|
|
|
*/ |
119
|
|
|
public function testSerializesOutputAsFloat() : void |
120
|
|
|
{ |
121
|
|
|
$floatType = Type::float(); |
122
|
|
|
|
123
|
|
|
self::assertSame(1.0, $floatType->serialize(1)); |
124
|
|
|
self::assertSame(0.0, $floatType->serialize(0)); |
125
|
|
|
self::assertSame(123.5, $floatType->serialize('123.5')); |
126
|
|
|
self::assertSame(-1.0, $floatType->serialize(-1)); |
127
|
|
|
self::assertSame(0.1, $floatType->serialize(0.1)); |
128
|
|
|
self::assertSame(1.1, $floatType->serialize(1.1)); |
129
|
|
|
self::assertSame(-1.1, $floatType->serialize(-1.1)); |
130
|
|
|
self::assertSame(-1.1, $floatType->serialize('-1.1')); |
131
|
|
|
self::assertSame(0.0, $floatType->serialize(false)); |
132
|
|
|
self::assertSame(1.0, $floatType->serialize(true)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testSerializesOutputFloatCannotRepresentString() : void |
136
|
|
|
{ |
137
|
|
|
$floatType = Type::float(); |
138
|
|
|
$this->expectException(Error::class); |
139
|
|
|
$this->expectExceptionMessage('Float cannot represent non numeric value: one'); |
140
|
|
|
$floatType->serialize('one'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testSerializesOutputFloatCannotRepresentEmptyString() : void |
144
|
|
|
{ |
145
|
|
|
$floatType = Type::float(); |
146
|
|
|
$this->expectException(Error::class); |
147
|
|
|
$this->expectExceptionMessage('Float cannot represent non numeric value: (empty string)'); |
148
|
|
|
$floatType->serialize(''); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @see it('serializes output as String') |
153
|
|
|
*/ |
154
|
|
|
public function testSerializesOutputAsString() : void |
155
|
|
|
{ |
156
|
|
|
$stringType = Type::string(); |
157
|
|
|
|
158
|
|
|
self::assertSame('string', $stringType->serialize('string')); |
159
|
|
|
self::assertSame('1', $stringType->serialize(1)); |
160
|
|
|
self::assertSame('-1.1', $stringType->serialize(-1.1)); |
161
|
|
|
self::assertSame('true', $stringType->serialize(true)); |
162
|
|
|
self::assertSame('false', $stringType->serialize(false)); |
163
|
|
|
self::assertSame('null', $stringType->serialize(null)); |
164
|
|
|
self::assertSame('2', $stringType->serialize(new ObjectIdStub(2))); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testSerializesOutputStringsCannotRepresentArray() : void |
168
|
|
|
{ |
169
|
|
|
$stringType = Type::string(); |
170
|
|
|
$this->expectException(Error::class); |
171
|
|
|
$this->expectExceptionMessage('String cannot represent non scalar value: []'); |
172
|
|
|
$stringType->serialize([]); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function testSerializesOutputStringsCannotRepresentObject() : void |
176
|
|
|
{ |
177
|
|
|
$stringType = Type::string(); |
178
|
|
|
$this->expectException(Error::class); |
179
|
|
|
$this->expectExceptionMessage('String cannot represent non scalar value: instance of stdClass'); |
180
|
|
|
$stringType->serialize(new stdClass()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @see it('serializes output as Boolean') |
185
|
|
|
*/ |
186
|
|
|
public function testSerializesOutputAsBoolean() : void |
187
|
|
|
{ |
188
|
|
|
$boolType = Type::boolean(); |
189
|
|
|
|
190
|
|
|
self::assertTrue($boolType->serialize(true)); |
191
|
|
|
self::assertTrue($boolType->serialize(1)); |
192
|
|
|
self::assertTrue($boolType->serialize('1')); |
193
|
|
|
self::assertTrue($boolType->serialize('string')); |
194
|
|
|
|
195
|
|
|
self::assertFalse($boolType->serialize(false)); |
196
|
|
|
self::assertFalse($boolType->serialize(0)); |
197
|
|
|
self::assertFalse($boolType->serialize('0')); |
198
|
|
|
self::assertFalse($boolType->serialize('')); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @see it('serializes output as ID') |
203
|
|
|
*/ |
204
|
|
|
public function testSerializesOutputAsID() : void |
205
|
|
|
{ |
206
|
|
|
$idType = Type::id(); |
207
|
|
|
|
208
|
|
|
self::assertSame('string', $idType->serialize('string')); |
209
|
|
|
self::assertSame('', $idType->serialize('')); |
210
|
|
|
self::assertSame('1', $idType->serialize('1')); |
211
|
|
|
self::assertSame('1', $idType->serialize(1)); |
212
|
|
|
self::assertSame('0', $idType->serialize(0)); |
213
|
|
|
self::assertSame('true', $idType->serialize(true)); |
214
|
|
|
self::assertSame('false', $idType->serialize(false)); |
215
|
|
|
self::assertSame('2', $idType->serialize(new ObjectIdStub(2))); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testSerializesOutputIDCannotRepresentObject() : void |
219
|
|
|
{ |
220
|
|
|
$idType = Type::id(); |
221
|
|
|
$this->expectException(Error::class); |
222
|
|
|
$this->expectExceptionMessage('ID type cannot represent non scalar value: instance of stdClass'); |
223
|
|
|
$idType->serialize(new stdClass()); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|