2016-05-01 00:02:04 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Tests\Utils;
|
|
|
|
|
2016-11-19 02:12:18 +03:00
|
|
|
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;
|
|
|
|
|
|
|
|
class ASTFromValueTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
// Describe: astFromValue
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @it converts boolean values to ASTs
|
|
|
|
*/
|
|
|
|
public function testConvertsBooleanValueToASTs()
|
|
|
|
{
|
2016-11-19 02:12:18 +03:00
|
|
|
$this->assertEquals(new BooleanValueNode(['value' => true]), AST::astFromValue(true, Type::boolean()));
|
|
|
|
$this->assertEquals(new BooleanValueNode(['value' => false]), AST::astFromValue(false, Type::boolean()));
|
|
|
|
$this->assertEquals(new NullValueNode([]), AST::astFromValue(null, Type::boolean()));
|
|
|
|
$this->assertEquals(new BooleanValueNode(['value' => false]), AST::astFromValue(0, Type::boolean()));
|
|
|
|
$this->assertEquals(new BooleanValueNode(['value' => true]), AST::astFromValue(1, Type::boolean()));
|
|
|
|
$this->assertEquals(new BooleanValueNode(['value' => false]), AST::astFromValue(0, Type::nonNull(Type::boolean())));
|
2016-11-18 19:59:28 +03:00
|
|
|
$this->assertEquals(null, AST::astFromValue(null, Type::nonNull(Type::boolean()))); // Note: null means that AST cannot
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-01 20:11:33 +03:00
|
|
|
* @it converts Int values to Int ASTs
|
2016-05-01 00:02:04 +03:00
|
|
|
*/
|
2016-11-01 20:11:33 +03:00
|
|
|
public function testConvertsIntValuesToASTs()
|
2016-05-01 00:02:04 +03:00
|
|
|
{
|
2016-11-19 02:12:18 +03:00
|
|
|
$this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123.0, Type::int()));
|
|
|
|
$this->assertEquals(new IntValueNode(['value' => '10000']), AST::astFromValue(1e4, Type::int()));
|
2017-07-04 14:27:20 +03:00
|
|
|
$this->assertEquals(new IntValueNode(['value' => '0']), AST::astFromValue(0e4, Type::int()));
|
2017-12-21 09:52:43 +03:00
|
|
|
}
|
2017-07-04 14:27:20 +03:00
|
|
|
|
2017-12-21 09:52:43 +03:00
|
|
|
public function testConvertsIntValuesToASTsCannotRepresentNonInteger()
|
|
|
|
{
|
2017-07-04 14:27:20 +03:00
|
|
|
// GraphQL spec does not allow coercing non-integer values to Int to avoid
|
|
|
|
// accidental data loss.
|
2017-12-21 09:52:43 +03:00
|
|
|
$this->setExpectedException(\Exception::class, 'Int cannot represent non-integer value: 123.5');
|
|
|
|
AST::astFromValue(123.5, Type::int());
|
|
|
|
}
|
2016-11-01 20:11:33 +03:00
|
|
|
|
2017-12-21 09:52:43 +03:00
|
|
|
public function testConvertsIntValuesToASTsCannotRepresentNon32bitsInteger()
|
|
|
|
{
|
|
|
|
$this->setExpectedException(\Exception::class, 'Int cannot represent non 32-bit signed integer value: 1.0E+40');
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-01 20:11:33 +03:00
|
|
|
* @it converts Float values to Int/Float ASTs
|
2016-05-01 00:02:04 +03:00
|
|
|
*/
|
2016-11-01 20:11:33 +03:00
|
|
|
public function testConvertsFloatValuesToIntOrFloatASTs()
|
2016-05-01 00:02:04 +03:00
|
|
|
{
|
2016-11-19 02:12:18 +03:00
|
|
|
$this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123, Type::float()));
|
|
|
|
$this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123.0, Type::float()));
|
|
|
|
$this->assertEquals(new FloatValueNode(['value' => '123.5']), AST::astFromValue(123.5, Type::float()));
|
|
|
|
$this->assertEquals(new IntValueNode(['value' => '10000']), AST::astFromValue(1e4, Type::float()));
|
|
|
|
$this->assertEquals(new FloatValueNode(['value' => '1e+40']), AST::astFromValue(1e40, Type::float()));
|
2017-07-04 14:27:20 +03:00
|
|
|
$this->assertEquals(new IntValueNode(['value' => '0']), AST::astFromValue(0e40, Type::float()));
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-01 20:11:33 +03:00
|
|
|
* @it converts String values to String ASTs
|
2016-05-01 00:02:04 +03:00
|
|
|
*/
|
|
|
|
public function testConvertsStringValuesToASTs()
|
|
|
|
{
|
2016-11-19 02:12:18 +03:00
|
|
|
$this->assertEquals(new StringValueNode(['value' => 'hello']), AST::astFromValue('hello', Type::string()));
|
|
|
|
$this->assertEquals(new StringValueNode(['value' => 'VALUE']), AST::astFromValue('VALUE', Type::string()));
|
|
|
|
$this->assertEquals(new StringValueNode(['value' => 'VA\\nLUE']), AST::astFromValue("VA\nLUE", Type::string()));
|
|
|
|
$this->assertEquals(new StringValueNode(['value' => '123']), AST::astFromValue(123, Type::string()));
|
|
|
|
$this->assertEquals(new StringValueNode(['value' => 'false']), AST::astFromValue(false, Type::string()));
|
|
|
|
$this->assertEquals(new NullValueNode([]), AST::astFromValue(null, Type::string()));
|
2016-11-18 19:59:28 +03:00
|
|
|
$this->assertEquals(null, AST::astFromValue(null, Type::nonNull(Type::string())));
|
2016-11-01 20:11:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @it converts ID values to Int/String ASTs
|
|
|
|
*/
|
|
|
|
public function testConvertIdValuesToIntOrStringASTs()
|
|
|
|
{
|
2016-11-19 02:12:18 +03:00
|
|
|
$this->assertEquals(new StringValueNode(['value' => 'hello']), AST::astFromValue('hello', Type::id()));
|
|
|
|
$this->assertEquals(new StringValueNode(['value' => 'VALUE']), AST::astFromValue('VALUE', Type::id()));
|
|
|
|
$this->assertEquals(new StringValueNode(['value' => 'VA\\nLUE']), AST::astFromValue("VA\nLUE", Type::id()));
|
|
|
|
$this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123, Type::id()));
|
|
|
|
$this->assertEquals(new StringValueNode(['value' => 'false']), AST::astFromValue(false, Type::id()));
|
|
|
|
$this->assertEquals(new NullValueNode([]), AST::astFromValue(null, Type::id()));
|
2016-11-18 19:59:28 +03:00
|
|
|
$this->assertEquals(null, AST::astFromValue(null, Type::nonNull(Type::id())));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @it does not converts NonNull values to NullValue
|
|
|
|
*/
|
|
|
|
public function testDoesNotConvertsNonNullValuestoNullValue()
|
|
|
|
{
|
|
|
|
$this->assertSame(null, AST::astFromValue(null, Type::nonNull(Type::boolean())));
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @it converts string values to Enum ASTs if possible
|
|
|
|
*/
|
|
|
|
public function testConvertsStringValuesToEnumASTsIfPossible()
|
|
|
|
{
|
2016-11-19 02:12:18 +03:00
|
|
|
$this->assertEquals(new EnumValueNode(['value' => 'HELLO']), AST::astFromValue('HELLO', $this->myEnum()));
|
|
|
|
$this->assertEquals(new EnumValueNode(['value' => 'COMPLEX']), AST::astFromValue($this->complexValue(), $this->myEnum()));
|
2016-11-01 20:11:33 +03:00
|
|
|
|
|
|
|
// Note: case sensitive
|
|
|
|
$this->assertEquals(null, AST::astFromValue('hello', $this->myEnum()));
|
|
|
|
|
|
|
|
// Note: Not a valid enum value
|
|
|
|
$this->assertEquals(null, AST::astFromValue('VALUE', $this->myEnum()));
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @it converts array values to List ASTs
|
|
|
|
*/
|
|
|
|
public function testConvertsArrayValuesToListASTs()
|
|
|
|
{
|
2016-11-19 02:12:18 +03:00
|
|
|
$value1 = new ListValueNode([
|
2016-05-01 00:02:04 +03:00
|
|
|
'values' => [
|
2016-11-19 02:12:18 +03:00
|
|
|
new StringValueNode(['value' => 'FOO']),
|
|
|
|
new StringValueNode(['value' => 'BAR'])
|
2016-05-01 00:02:04 +03:00
|
|
|
]
|
|
|
|
]);
|
2016-11-01 20:11:33 +03:00
|
|
|
$this->assertEquals($value1, AST::astFromValue(['FOO', 'BAR'], Type::listOf(Type::string())));
|
2016-05-01 00:02:04 +03:00
|
|
|
|
2016-11-19 02:12:18 +03:00
|
|
|
$value2 = new ListValueNode([
|
2016-05-01 00:02:04 +03:00
|
|
|
'values' => [
|
2016-11-19 02:12:18 +03:00
|
|
|
new EnumValueNode(['value' => 'HELLO']),
|
|
|
|
new EnumValueNode(['value' => 'GOODBYE']),
|
2016-05-01 00:02:04 +03:00
|
|
|
]
|
|
|
|
]);
|
2016-11-01 20:11:33 +03:00
|
|
|
$this->assertEquals($value2, AST::astFromValue(['HELLO', 'GOODBYE'], Type::listOf($this->myEnum())));
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @it converts list singletons
|
|
|
|
*/
|
|
|
|
public function testConvertsListSingletons()
|
|
|
|
{
|
2016-11-19 02:12:18 +03:00
|
|
|
$this->assertEquals(new StringValueNode(['value' => 'FOO']), AST::astFromValue('FOO', Type::listOf(Type::string())));
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @it converts input objects
|
|
|
|
*/
|
|
|
|
public function testConvertsInputObjects()
|
|
|
|
{
|
2016-11-01 20:11:33 +03:00
|
|
|
$inputObj = new InputObjectType([
|
|
|
|
'name' => 'MyInputObj',
|
2016-05-01 00:02:04 +03:00
|
|
|
'fields' => [
|
2016-11-01 20:11:33 +03:00
|
|
|
'foo' => Type::float(),
|
|
|
|
'bar' => $this->myEnum()
|
2016-05-01 00:02:04 +03:00
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
2016-11-19 02:12:18 +03:00
|
|
|
$expected = new ObjectValueNode([
|
2016-05-01 00:02:04 +03:00
|
|
|
'fields' => [
|
2016-11-19 02:12:18 +03:00
|
|
|
$this->objectField('foo', new IntValueNode(['value' => '3'])),
|
|
|
|
$this->objectField('bar', new EnumValueNode(['value' => 'HELLO']))
|
2016-05-01 00:02:04 +03:00
|
|
|
]
|
|
|
|
]);
|
2016-11-01 20:11:33 +03:00
|
|
|
|
|
|
|
$data = ['foo' => 3, 'bar' => 'HELLO'];
|
|
|
|
$this->assertEquals($expected, AST::astFromValue($data, $inputObj));
|
|
|
|
$this->assertEquals($expected, AST::astFromValue((object) $data, $inputObj));
|
|
|
|
}
|
|
|
|
|
2016-11-19 02:12:18 +03:00
|
|
|
/**
|
|
|
|
* @it converts input objects with explicit nulls
|
|
|
|
*/
|
2016-11-18 19:59:28 +03:00
|
|
|
public function testConvertsInputObjectsWithExplicitNulls()
|
|
|
|
{
|
|
|
|
$inputObj = new InputObjectType([
|
|
|
|
'name' => 'MyInputObj',
|
|
|
|
'fields' => [
|
|
|
|
'foo' => Type::float(),
|
|
|
|
'bar' => $this->myEnum()
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
2016-11-19 02:12:18 +03:00
|
|
|
$this->assertEquals(new ObjectValueNode([
|
2016-11-18 19:59:28 +03:00
|
|
|
'fields' => [
|
2016-11-19 02:12:18 +03:00
|
|
|
$this->objectField('foo', new NullValueNode([]))
|
2016-11-18 19:59:28 +03:00
|
|
|
]
|
|
|
|
]), AST::astFromValue(['foo' => null], $inputObj));
|
|
|
|
}
|
|
|
|
|
2016-11-01 20:11:33 +03:00
|
|
|
private $complexValue;
|
|
|
|
|
|
|
|
private function complexValue()
|
|
|
|
{
|
|
|
|
if (!$this->complexValue) {
|
|
|
|
$this->complexValue = new \stdClass();
|
|
|
|
$this->complexValue->someArbitrary = 'complexValue';
|
|
|
|
}
|
|
|
|
return $this->complexValue;
|
2016-05-01 00:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return EnumType
|
|
|
|
*/
|
|
|
|
private function myEnum()
|
|
|
|
{
|
|
|
|
return new EnumType([
|
|
|
|
'name' => 'MyEnum',
|
|
|
|
'values' => [
|
|
|
|
'HELLO' => [],
|
|
|
|
'GOODBYE' => [],
|
2016-11-01 20:11:33 +03:00
|
|
|
'COMPLEX' => ['value' => $this->complexValue()]
|
2016-05-01 00:02:04 +03:00
|
|
|
]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @param $value
|
2016-11-19 02:12:18 +03:00
|
|
|
* @return ObjectFieldNode
|
2016-05-01 00:02:04 +03:00
|
|
|
*/
|
|
|
|
private function objectField($name, $value)
|
|
|
|
{
|
2016-11-19 02:12:18 +03:00
|
|
|
return new ObjectFieldNode([
|
|
|
|
'name' => new NameNode(['value' => $name]),
|
2016-05-01 00:02:04 +03:00
|
|
|
'value' => $value
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|