Repository navigation
Expand file tree
/
Copy pathScalarSerializationTest.php
More file actions
198 lines (173 loc) · 7.53 KB
/
Copy pathScalarSerializationTest.php
File metadata and controls
198 lines (173 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php declare(strict_types=1);
namespace GraphQL\Tests\Type;
use GraphQL\Error\SerializationError;
use GraphQL\Tests\Type\TestClasses\ObjectIdStub;
use GraphQL\Type\Definition\Type;
use PHPUnit\Framework\TestCase;
final class ScalarSerializationTest extends TestCase
{
// Type System: Scalar coercion
/** @see it('serializes output as Int') */
public function testSerializesOutputAsInt(): void
{
$intType = Type::int();
self::assertSame(1, $intType->serialize(1));
self::assertSame(123, $intType->serialize('123'));
self::assertSame(0, $intType->serialize(0));
self::assertSame(-1, $intType->serialize(-1));
self::assertSame(100000, $intType->serialize(1e5));
self::assertSame(0, $intType->serialize(0e5));
self::assertSame(0, $intType->serialize(false));
self::assertSame(1, $intType->serialize(true));
}
/** @return iterable<array{mixed, string}> */
public static function badIntValues(): iterable
{
yield [0.1, 'Int cannot represent non-integer value: 0.1'];
yield [1.1, 'Int cannot represent non-integer value: 1.1'];
yield [-1.1, 'Int cannot represent non-integer value: -1.1'];
yield ['-1.1', 'Int cannot represent non-integer value: "-1.1"'];
yield [9876504321, 'Int cannot represent non 32-bit signed integer value: 9876504321'];
yield [-9876504321, 'Int cannot represent non 32-bit signed integer value: -9876504321'];
yield [1e100, 'Int cannot represent non 32-bit signed integer value: 1.0E+100'];
yield [-1e100, 'Int cannot represent non 32-bit signed integer value: -1.0E+100'];
yield [log(0), 'Int cannot represent non 32-bit signed integer value: -INF'];
yield [acos(8), 'Int cannot represent non-integer value: NAN'];
yield ['one', 'Int cannot represent non-integer value: "one"'];
yield ['', 'Int cannot represent non-integer value: (empty string)'];
yield [[5], 'Int cannot represent non-integer value: [5]'];
}
/**
* @param mixed $value
*
* @dataProvider badIntValues
*/
public function testSerializesOutputAsIntErrors($value, string $expectedError): void
{
// The GraphQL specification does not allow serializing non-integer values
// as Int to avoid accidental data loss.
$intType = Type::int();
$this->expectException(SerializationError::class);
$this->expectExceptionMessage($expectedError);
$intType->serialize($value);
}
/** @see it('serializes output as Float') */
public function testSerializesOutputAsFloat(): void
{
$floatType = Type::float();
self::assertSame(1.0, $floatType->serialize(1));
self::assertSame(0.0, $floatType->serialize(0));
self::assertSame(123.5, $floatType->serialize('123.5'));
self::assertSame(-1.0, $floatType->serialize(-1));
self::assertSame(0.1, $floatType->serialize(0.1));
self::assertSame(1.1, $floatType->serialize(1.1));
self::assertSame(-1.1, $floatType->serialize(-1.1));
self::assertSame(-1.1, $floatType->serialize('-1.1'));
self::assertSame(0.0, $floatType->serialize(false));
self::assertSame(1.0, $floatType->serialize(true));
}
/** @return iterable<array{mixed, string}> */
public static function badFloatValues(): iterable
{
yield ['one', 'Float cannot represent non numeric value: "one"'];
yield ['', 'Float cannot represent non numeric value: (empty string)'];
yield [log(0), 'Float cannot represent non numeric value: -INF'];
yield [acos(8), 'Float cannot represent non numeric value: NAN'];
yield [[5], 'Float cannot represent non numeric value: [5]'];
}
/**
* @param mixed $value
*
* @dataProvider badFloatValues
*/
public function testSerializesOutputFloatErrors($value, string $expectedError): void
{
$floatType = Type::float();
$this->expectException(SerializationError::class);
$this->expectExceptionMessage($expectedError);
$floatType->serialize($value);
}
/** @see it('serializes output as String') */
public function testSerializesOutputAsString(): void
{
$stringType = Type::string();
self::assertSame('string', $stringType->serialize('string'));
self::assertSame('1', $stringType->serialize(1));
self::assertSame('-1.1', $stringType->serialize(-1.1));
self::assertSame('1', $stringType->serialize(true));
self::assertSame('', $stringType->serialize(false));
self::assertSame('', $stringType->serialize(null));
self::assertSame('foo', $stringType->serialize(new class() implements \Stringable {
public function __toString(): string
{
return 'foo';
}
}));
}
/** @return iterable<array{mixed, string}> */
public static function badStringValues(): iterable
{
yield [[1], 'String cannot represent value: [1]'];
yield [new \stdClass(), 'String cannot represent value: instance of stdClass'];
}
/**
* @param mixed $value
*
* @dataProvider badStringValues
*/
public function testSerializesOutputStringErrors($value, string $expectedError): void
{
$stringType = Type::string();
$this->expectException(SerializationError::class);
$this->expectExceptionMessage($expectedError);
$stringType->serialize($value);
}
/** @see it('serializes output as Boolean') */
public function testSerializesOutputAsBoolean(): void
{
$boolType = Type::boolean();
self::assertTrue($boolType->serialize(true));
self::assertTrue($boolType->serialize(1));
self::assertTrue($boolType->serialize('1'));
self::assertTrue($boolType->serialize('string'));
self::assertFalse($boolType->serialize(false));
self::assertFalse($boolType->serialize(0));
self::assertFalse($boolType->serialize('0'));
self::assertFalse($boolType->serialize(''));
}
/** @see it('serializes output as ID') */
public function testSerializesOutputAsID(): void
{
$idType = Type::id();
self::assertSame('string', $idType->serialize('string'));
self::assertSame('false', $idType->serialize('false'));
self::assertSame('', $idType->serialize(''));
self::assertSame('1', $idType->serialize('1'));
self::assertSame('0', $idType->serialize('0'));
self::assertSame('1', $idType->serialize(1));
self::assertSame('0', $idType->serialize(0));
self::assertSame('2', $idType->serialize(new ObjectIdStub(2)));
}
/** @return iterable<array{mixed, string}> */
public static function badIDValues(): iterable
{
yield [new \stdClass(), 'ID cannot represent a non-string and non-integer value: instance of stdClass'];
yield [true, 'ID cannot represent a non-string and non-integer value: true'];
yield [false, 'ID cannot represent a non-string and non-integer value: false'];
yield [-1.1, 'ID cannot represent a non-string and non-integer value: -1.1'];
yield [['abc'], 'ID cannot represent a non-string and non-integer value: ["abc"]'];
}
/**
* @param mixed $value
*
* @dataProvider badIDValues
*/
public function testSerializesOutputAsIDError($value, string $expectedError): void
{
$idType = Type::id();
$this->expectExceptionObject(new SerializationError(
$expectedError
));
$idType->serialize($value);
}
}