11<?php
22namespace GraphQL \Tests \Type ;
33
4+ use GraphQL \Error \InvariantViolation ;
45use GraphQL \Error \UserError ;
56use GraphQL \Type \Definition \Type ;
67
@@ -19,53 +20,77 @@ public function testSerializesOutputInt()
1920 $ this ->assertSame (123 , $ intType ->serialize ('123 ' ));
2021 $ this ->assertSame (0 , $ intType ->serialize (0 ));
2122 $ this ->assertSame (-1 , $ intType ->serialize (-1 ));
22- $ this ->assertSame (0 , $ intType ->serialize (0.1 ));
23- $ this ->assertSame (1 , $ intType ->serialize (1.1 ));
24- $ this ->assertSame (-1 , $ intType ->serialize (-1.1 ));
2523 $ this ->assertSame (100000 , $ intType ->serialize (1e5 ));
24+ $ this ->assertSame (0 , $ intType ->serialize (0e5 ));
25+
26+ // The GraphQL specification does not allow serializing non-integer values
27+ // as Int to avoid accidental data loss.
28+ try {
29+ $ intType ->serialize (0.1 );
30+ $ this ->fail ('Expected exception not thrown ' );
31+ } catch (InvariantViolation $ e ) {
32+ $ this ->assertEquals ('Int cannot represent non-integer value: 0.1 ' , $ e ->getMessage ());
33+ }
34+ try {
35+ $ intType ->serialize (1.1 );
36+ $ this ->fail ('Expected exception not thrown ' );
37+ } catch (InvariantViolation $ e ) {
38+ $ this ->assertEquals ('Int cannot represent non-integer value: 1.1 ' , $ e ->getMessage ());
39+ }
40+ try {
41+ $ intType ->serialize (-1.1 );
42+ $ this ->fail ('Expected exception not thrown ' );
43+ } catch (InvariantViolation $ e ) {
44+ $ this ->assertEquals ('Int cannot represent non-integer value: -1.1 ' , $ e ->getMessage ());
45+ }
46+ try {
47+ $ intType ->serialize ('-1.1 ' );
48+ $ this ->fail ('Expected exception not thrown ' );
49+ } catch (InvariantViolation $ e ) {
50+ $ this ->assertEquals ('Int cannot represent non-integer value: "-1.1" ' , $ e ->getMessage ());
51+ }
52+
2653 // Maybe a safe PHP int, but bigger than 2^32, so not
2754 // representable as a GraphQL Int
2855 try {
2956 $ intType ->serialize (9876504321 );
3057 $ this ->fail ('Expected exception was not thrown ' );
31- } catch (UserError $ e ) {
58+ } catch (InvariantViolation $ e ) {
3259 $ this ->assertEquals ('Int cannot represent non 32-bit signed integer value: 9876504321 ' , $ e ->getMessage ());
3360 }
3461
3562 try {
3663 $ intType ->serialize (-9876504321 );
3764 $ this ->fail ('Expected exception was not thrown ' );
38- } catch (UserError $ e ) {
65+ } catch (InvariantViolation $ e ) {
3966 $ this ->assertEquals ('Int cannot represent non 32-bit signed integer value: -9876504321 ' , $ e ->getMessage ());
4067 }
4168
4269 try {
4370 $ intType ->serialize (1e100 );
4471 $ this ->fail ('Expected exception was not thrown ' );
45- } catch (UserError $ e ) {
72+ } catch (InvariantViolation $ e ) {
4673 $ this ->assertEquals ('Int cannot represent non 32-bit signed integer value: 1.0E+100 ' , $ e ->getMessage ());
4774 }
4875
4976 try {
5077 $ intType ->serialize (-1e100 );
5178 $ this ->fail ('Expected exception was not thrown ' );
52- } catch (UserError $ e ) {
79+ } catch (InvariantViolation $ e ) {
5380 $ this ->assertEquals ('Int cannot represent non 32-bit signed integer value: -1.0E+100 ' , $ e ->getMessage ());
5481 }
5582
56- $ this ->assertSame (-1 , $ intType ->serialize ('-1.1 ' ));
57-
5883 try {
5984 $ intType ->serialize ('one ' );
6085 $ this ->fail ('Expected exception was not thrown ' );
61- } catch (UserError $ e ) {
86+ } catch (InvariantViolation $ e ) {
6287 $ this ->assertEquals ('Int cannot represent non 32-bit signed integer value: "one" ' , $ e ->getMessage ());
6388 }
6489
6590 try {
6691 $ intType ->serialize ('' );
6792 $ this ->fail ('Expected exception was not thrown ' );
68- } catch (UserError $ e ) {
93+ } catch (InvariantViolation $ e ) {
6994 $ this ->assertEquals ('Int cannot represent non 32-bit signed integer value: (empty string) ' , $ e ->getMessage ());
7095 }
7196
0 commit comments