graphql-php/tests/Utils/AstFromValueTest.php

270 lines
10 KiB
PHP
Raw Normal View History

2016-05-01 00:02:04 +03:00
<?php
2018-09-02 13:44:21 +03:00
declare(strict_types=1);
2016-05-01 00:02:04 +03:00
namespace GraphQL\Tests\Utils;
use GraphQL\Language\AST\BooleanValueNode;
use GraphQL\Language\AST\EnumValueNode;
use GraphQL\Language\AST\FloatValueNode;
use GraphQL\Language\AST\IntValueNode;
use GraphQL\Language\AST\ListValueNode;
use GraphQL\Language\AST\NameNode;
use GraphQL\Language\AST\NullValueNode;
use GraphQL\Language\AST\ObjectFieldNode;
use GraphQL\Language\AST\ObjectValueNode;
use GraphQL\Language\AST\StringValueNode;
2016-05-01 00:02:04 +03:00
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Utils\AST;
2018-07-29 18:43:10 +03:00
use PHPUnit\Framework\TestCase;
2018-09-02 13:44:21 +03:00
use stdClass;
2018-09-26 12:07:23 +03:00
use Throwable;
2016-05-01 00:02:04 +03:00
2018-07-29 18:43:10 +03:00
class AstFromValueTest extends TestCase
2016-05-01 00:02:04 +03:00
{
2018-09-02 13:44:21 +03:00
/** @var stdClass */
private $complexValue;
2016-05-01 00:02:04 +03:00
/**
* @see it('converts boolean values to ASTs')
2016-05-01 00:02:04 +03:00
*/
public function testConvertsBooleanValueToASTs() : void
2016-05-01 00:02:04 +03:00
{
2018-09-19 18:12:09 +03:00
self::assertEquals(new BooleanValueNode(['value' => true]), AST::astFromValue(true, Type::boolean()));
self::assertEquals(new BooleanValueNode(['value' => false]), AST::astFromValue(false, Type::boolean()));
self::assertEquals(new NullValueNode([]), AST::astFromValue(null, Type::boolean()));
self::assertEquals(new BooleanValueNode(['value' => false]), AST::astFromValue(0, Type::boolean()));
self::assertEquals(new BooleanValueNode(['value' => true]), AST::astFromValue(1, Type::boolean()));
self::assertEquals(
2018-09-02 13:44:21 +03:00
new BooleanValueNode(['value' => false]),
AST::astFromValue(0, Type::nonNull(Type::boolean()))
);
2018-09-19 18:12:09 +03:00
self::assertEquals(
2018-09-02 13:44:21 +03:00
null,
AST::astFromValue(null, Type::nonNull(Type::boolean()))
); // Note: null means that AST cannot
2016-05-01 00:02:04 +03:00
}
/**
* @see it('converts Int values to Int ASTs')
2016-05-01 00:02:04 +03:00
*/
public function testConvertsIntValuesToASTs() : void
2016-05-01 00:02:04 +03:00
{
2018-09-19 18:12:09 +03:00
self::assertEquals(new IntValueNode(['value' => '-1']), AST::astFromValue(-1, Type::int()));
self::assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123.0, Type::int()));
self::assertEquals(new IntValueNode(['value' => '10000']), AST::astFromValue(1e4, Type::int()));
self::assertEquals(new IntValueNode(['value' => '0']), AST::astFromValue(0e4, Type::int()));
}
public function testConvertsIntValuesToASTsCannotRepresentNonInteger() : void
{
// GraphQL spec does not allow coercing non-integer values to Int to avoid
// accidental data loss.
2018-09-26 12:07:23 +03:00
$this->expectException(Throwable::class);
2018-07-29 18:43:10 +03:00
$this->expectExceptionMessage('Int cannot represent non-integer value: 123.5');
AST::astFromValue(123.5, Type::int());
}
public function testConvertsIntValuesToASTsCannotRepresentNon32bitsInteger() : void
{
2018-09-26 12:07:23 +03:00
$this->expectException(Throwable::class);
2018-07-29 18:43:10 +03:00
$this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: 1.0E+40');
2018-09-02 13:44:21 +03:00
AST::astFromValue(
1e40,
Type::int()
); // Note: js version will produce 1e+40, both values are valid GraphQL floats
2016-05-01 00:02:04 +03:00
}
/**
* @see it('converts Float values to Int/Float ASTs')
2016-05-01 00:02:04 +03:00
*/
public function testConvertsFloatValuesToIntOrFloatASTs() : void
2016-05-01 00:02:04 +03:00
{
2018-09-19 18:12:09 +03:00
self::assertEquals(new IntValueNode(['value' => '-1']), AST::astFromValue(-1, Type::float()));
self::assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123, Type::float()));
self::assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123.0, Type::float()));
self::assertEquals(new FloatValueNode(['value' => '123.5']), AST::astFromValue(123.5, Type::float()));
self::assertEquals(new IntValueNode(['value' => '10000']), AST::astFromValue(1e4, Type::float()));
self::assertEquals(new FloatValueNode(['value' => '1e+40']), AST::astFromValue(1e40, Type::float()));
self::assertEquals(new IntValueNode(['value' => '0']), AST::astFromValue(0e40, Type::float()));
2016-05-01 00:02:04 +03:00
}
/**
* @see it('converts String values to String ASTs')
2016-05-01 00:02:04 +03:00
*/
public function testConvertsStringValuesToASTs() : void
2016-05-01 00:02:04 +03:00
{
2018-09-19 18:12:09 +03:00
self::assertEquals(new StringValueNode(['value' => 'hello']), AST::astFromValue('hello', Type::string()));
self::assertEquals(new StringValueNode(['value' => 'VALUE']), AST::astFromValue('VALUE', Type::string()));
self::assertEquals(new StringValueNode(['value' => "VA\nLUE"]), AST::astFromValue("VA\nLUE", Type::string()));
self::assertEquals(new StringValueNode(['value' => '123']), AST::astFromValue(123, Type::string()));
self::assertEquals(new StringValueNode(['value' => 'false']), AST::astFromValue(false, Type::string()));
self::assertEquals(new NullValueNode([]), AST::astFromValue(null, Type::string()));
self::assertEquals(null, AST::astFromValue(null, Type::nonNull(Type::string())));
}
/**
* @see it('converts ID values to Int/String ASTs')
*/
public function testConvertIdValuesToIntOrStringASTs() : void
{
2018-09-19 18:12:09 +03:00
self::assertEquals(new StringValueNode(['value' => 'hello']), AST::astFromValue('hello', Type::id()));
self::assertEquals(new StringValueNode(['value' => 'VALUE']), AST::astFromValue('VALUE', Type::id()));
self::assertEquals(new StringValueNode(['value' => "VA\nLUE"]), AST::astFromValue("VA\nLUE", Type::id()));
self::assertEquals(new IntValueNode(['value' => '-1']), AST::astFromValue(-1, Type::id()));
self::assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123, Type::id()));
self::assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue('123', Type::id()));
self::assertEquals(new StringValueNode(['value' => '01']), AST::astFromValue('01', Type::id()));
self::assertEquals(new StringValueNode(['value' => 'false']), AST::astFromValue(false, Type::id()));
self::assertEquals(new NullValueNode([]), AST::astFromValue(null, Type::id()));
self::assertEquals(null, AST::astFromValue(null, Type::nonNull(Type::id())));
2016-11-18 19:59:28 +03:00
}
/**
* @see it('does not converts NonNull values to NullValue')
2016-11-18 19:59:28 +03:00
*/
public function testDoesNotConvertsNonNullValuestoNullValue() : void
2016-11-18 19:59:28 +03:00
{
2018-09-19 18:12:09 +03:00
self::assertNull(AST::astFromValue(null, Type::nonNull(Type::boolean())));
2016-05-01 00:02:04 +03:00
}
/**
* @see it('converts string values to Enum ASTs if possible')
2016-05-01 00:02:04 +03:00
*/
public function testConvertsStringValuesToEnumASTsIfPossible() : void
2016-05-01 00:02:04 +03:00
{
2018-09-19 18:12:09 +03:00
self::assertEquals(new EnumValueNode(['value' => 'HELLO']), AST::astFromValue('HELLO', $this->myEnum()));
self::assertEquals(
2018-09-02 13:44:21 +03:00
new EnumValueNode(['value' => 'COMPLEX']),
AST::astFromValue($this->complexValue(), $this->myEnum())
);
// Note: case sensitive
2018-09-19 18:12:09 +03:00
self::assertNull(AST::astFromValue('hello', $this->myEnum()));
// Note: Not a valid enum value
2018-09-19 18:12:09 +03:00
self::assertNull(AST::astFromValue('VALUE', $this->myEnum()));
2016-05-01 00:02:04 +03:00
}
2018-09-02 13:44:21 +03:00
/**
* @return EnumType
*/
private function myEnum()
{
return new EnumType([
'name' => 'MyEnum',
'values' => [
'HELLO' => [],
'GOODBYE' => [],
'COMPLEX' => ['value' => $this->complexValue()],
],
]);
}
private function complexValue()
{
if (! $this->complexValue) {
2018-09-26 12:07:23 +03:00
$this->complexValue = new stdClass();
2018-09-02 13:44:21 +03:00
$this->complexValue->someArbitrary = 'complexValue';
}
return $this->complexValue;
}
2016-05-01 00:02:04 +03:00
/**
* @see it('converts array values to List ASTs')
2016-05-01 00:02:04 +03:00
*/
public function testConvertsArrayValuesToListASTs() : void
2016-05-01 00:02:04 +03:00
{
$value1 = new ListValueNode([
2016-05-01 00:02:04 +03:00
'values' => [
new StringValueNode(['value' => 'FOO']),
2018-09-02 13:44:21 +03:00
new StringValueNode(['value' => 'BAR']),
],
2016-05-01 00:02:04 +03:00
]);
2018-09-19 18:12:09 +03:00
self::assertEquals($value1, AST::astFromValue(['FOO', 'BAR'], Type::listOf(Type::string())));
2016-05-01 00:02:04 +03:00
$value2 = new ListValueNode([
2016-05-01 00:02:04 +03:00
'values' => [
new EnumValueNode(['value' => 'HELLO']),
new EnumValueNode(['value' => 'GOODBYE']),
2018-09-02 13:44:21 +03:00
],
2016-05-01 00:02:04 +03:00
]);
2018-09-19 18:12:09 +03:00
self::assertEquals($value2, AST::astFromValue(['HELLO', 'GOODBYE'], Type::listOf($this->myEnum())));
2016-05-01 00:02:04 +03:00
}
/**
* @see it('converts list singletons')
2016-05-01 00:02:04 +03:00
*/
public function testConvertsListSingletons() : void
2016-05-01 00:02:04 +03:00
{
2018-09-19 18:12:09 +03:00
self::assertEquals(
2018-09-02 13:44:21 +03:00
new StringValueNode(['value' => 'FOO']),
AST::astFromValue('FOO', Type::listOf(Type::string()))
);
2016-05-01 00:02:04 +03:00
}
/**
* @see it('converts input objects')
2016-05-01 00:02:04 +03:00
*/
public function testConvertsInputObjects() : void
2016-05-01 00:02:04 +03:00
{
$inputObj = new InputObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'MyInputObj',
2016-05-01 00:02:04 +03:00
'fields' => [
'foo' => Type::float(),
2018-09-02 13:44:21 +03:00
'bar' => $this->myEnum(),
],
2016-05-01 00:02:04 +03:00
]);
$expected = new ObjectValueNode([
2016-05-01 00:02:04 +03:00
'fields' => [
$this->objectField('foo', new IntValueNode(['value' => '3'])),
2018-09-02 13:44:21 +03:00
$this->objectField('bar', new EnumValueNode(['value' => 'HELLO'])),
],
2016-05-01 00:02:04 +03:00
]);
$data = ['foo' => 3, 'bar' => 'HELLO'];
2018-09-19 18:12:09 +03:00
self::assertEquals($expected, AST::astFromValue($data, $inputObj));
self::assertEquals($expected, AST::astFromValue((object) $data, $inputObj));
}
/**
2018-09-02 13:44:21 +03:00
* @param mixed $value
2018-09-26 12:07:23 +03:00
*
2018-09-02 13:44:21 +03:00
* @return ObjectFieldNode
*/
2018-09-02 13:44:21 +03:00
private function objectField(string $name, $value)
2016-11-18 19:59:28 +03:00
{
2018-09-02 13:44:21 +03:00
return new ObjectFieldNode([
'name' => new NameNode(['value' => $name]),
'value' => $value,
2016-11-18 19:59:28 +03:00
]);
2016-05-01 00:02:04 +03:00
}
/**
2018-09-02 13:44:21 +03:00
* @see it('converts input objects with explicit nulls')
2016-05-01 00:02:04 +03:00
*/
2018-09-02 13:44:21 +03:00
public function testConvertsInputObjectsWithExplicitNulls() : void
2016-05-01 00:02:04 +03:00
{
2018-09-02 13:44:21 +03:00
$inputObj = new InputObjectType([
'name' => 'MyInputObj',
'fields' => [
'foo' => Type::float(),
'bar' => $this->myEnum(),
],
2016-05-01 00:02:04 +03:00
]);
2018-09-19 18:12:09 +03:00
self::assertEquals(
2018-09-02 13:44:21 +03:00
new ObjectValueNode([
2018-09-26 12:07:23 +03:00
'fields' => [
$this->objectField('foo', new NullValueNode([])),
],
2018-09-02 13:44:21 +03:00
]),
AST::astFromValue(['foo' => null], $inputObj)
);
2016-05-01 00:02:04 +03:00
}
}