Passed
Push — master ( a27dd3...975c9f )
by Vladimir
11:24
created

RepresentLowerThan32Bits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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