GraphQL\Tests\Type\ScalarSerializationTest::testSerializesOutputInt PHP Method

testSerializesOutputInt() public method

    public function testSerializesOutputInt()
    {
        $intType = Type::int();
        $this->assertSame(1, $intType->serialize(1));
        $this->assertSame(123, $intType->serialize('123'));
        $this->assertSame(0, $intType->serialize(0));
        $this->assertSame(-1, $intType->serialize(-1));
        $this->assertSame(0, $intType->serialize(0.1));
        $this->assertSame(1, $intType->serialize(1.1));
        $this->assertSame(-1, $intType->serialize(-1.1));
        $this->assertSame(100000, $intType->serialize(100000.0));
        // Maybe a safe PHP int, but bigger than 2^32, so not
        // representable as a GraphQL Int
        try {
            $intType->serialize(9876504321);
            $this->fail('Expected exception was not thrown');
        } catch (InvariantViolation $e) {
            $this->assertEquals('Int cannot represent non 32-bit signed integer value: 9876504321', $e->getMessage());
        }
        try {
            $intType->serialize(-9876504321);
            $this->fail('Expected exception was not thrown');
        } catch (InvariantViolation $e) {
            $this->assertEquals('Int cannot represent non 32-bit signed integer value: -9876504321', $e->getMessage());
        }
        try {
            $intType->serialize(1.0E+100);
            $this->fail('Expected exception was not thrown');
        } catch (InvariantViolation $e) {
            $this->assertEquals('Int cannot represent non 32-bit signed integer value: 1.0E+100', $e->getMessage());
        }
        try {
            $intType->serialize(-1.0E+100);
            $this->fail('Expected exception was not thrown');
        } catch (InvariantViolation $e) {
            $this->assertEquals('Int cannot represent non 32-bit signed integer value: -1.0E+100', $e->getMessage());
        }
        $this->assertSame(-1, $intType->serialize('-1.1'));
        try {
            $intType->serialize('one');
            $this->fail('Expected exception was not thrown');
        } catch (InvariantViolation $e) {
            $this->assertEquals('Int cannot represent non 32-bit signed integer value: one', $e->getMessage());
        }
        try {
            $intType->serialize('');
            $this->fail('Expected exception was not thrown');
        } catch (InvariantViolation $e) {
            $this->assertEquals('Int cannot represent non 32-bit signed integer value: (empty string)', $e->getMessage());
        }
        $this->assertSame(0, $intType->serialize(false));
        $this->assertSame(1, $intType->serialize(true));
    }