graphql-php/tests/Executor/ListsTest.php

198 lines
4.9 KiB
PHP
Raw Normal View History

2015-07-15 20:05:46 +03:00
<?php
2016-04-09 10:36:53 +03:00
namespace GraphQL\Tests\Executor;
2015-07-15 20:05:46 +03:00
use GraphQL\Error;
2016-04-09 10:36:53 +03:00
use GraphQL\Executor\Executor;
2015-07-15 20:05:46 +03:00
use GraphQL\FormattedError;
use GraphQL\Language\Parser;
use GraphQL\Language\SourceLocation;
use GraphQL\Schema;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
class ListsTest extends \PHPUnit_Framework_TestCase
{
private function check($testType, $testData, $expected)
2015-07-15 20:05:46 +03:00
{
$data = ['test' => $testData];
$dataType = null;
2015-07-15 20:05:46 +03:00
$dataType = new ObjectType([
'name' => 'DataType',
'fields' => function () use (&$testType, &$dataType, $data) {
return [
'test' => [
'type' => $testType
],
'nest' => [
'type' => $dataType,
'resolve' => function () use ($data) {
return $data;
}
]
];
}
]);
2015-07-15 20:05:46 +03:00
$schema = new Schema([
'query' => $dataType
]);
2015-07-15 20:05:46 +03:00
$ast = Parser::parse('{ nest { test } }');
2015-07-15 20:05:46 +03:00
$result = Executor::execute($schema, $ast, $data);
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
// Describe: Execute: Handles list nullability
2015-07-15 20:05:46 +03:00
/**
* @describe [T]
*/
public function testHandlesNullableLists()
2015-07-15 20:05:46 +03:00
{
$type = Type::listOf(Type::int());
// Contains values
$this->check(
$type,
[ 1, 2 ],
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
// Contains null
$this->check(
$type,
[ 1, null, 2 ],
[ 'data' => [ 'nest' => [ 'test' => [ 1, null, 2 ] ] ] ]
);
// Returns null
$this->check(
$type,
null,
[ 'data' => [ 'nest' => [ 'test' => null ] ] ]
);
2015-07-15 20:05:46 +03:00
}
/**
* @describe [T]!
*/
public function testHandlesNonNullableLists()
2015-07-15 20:05:46 +03:00
{
$type = Type::nonNull(Type::listOf(Type::int()));
// Contains values
$this->check(
$type,
[ 1, 2 ],
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
// Contains null
$this->check(
$type,
[ 1, null, 2 ],
[ 'data' => [ 'nest' => [ 'test' => [ 1, null, 2 ] ] ] ]
);
// Returns null
$this->check(
$type,
null,
[
'data' => [ 'nest' => null ],
'errors' => [
FormattedError::create(
'Cannot return null for non-nullable field DataType.test.',
[ new SourceLocation(1, 10) ]
)
2015-07-15 20:05:46 +03:00
]
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @describe [T!]
*/
public function testHandlesListOfNonNulls()
2015-07-15 20:05:46 +03:00
{
$type = Type::listOf(Type::nonNull(Type::int()));
// Contains values
$this->check(
$type,
[ 1, 2 ],
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
// Contains null
$this->check(
$type,
[ 1, null, 2 ],
[
'data' => [ 'nest' => [ 'test' => null ] ],
'errors' => [
FormattedError::create(
'Cannot return null for non-nullable field DataType.test.',
[ new SourceLocation(1, 10) ]
)
2015-07-15 20:05:46 +03:00
]
]
);
// Returns null
$this->check(
$type,
null,
[ 'data' => [ 'nest' => [ 'test' => null ] ] ]
);
2015-07-15 20:05:46 +03:00
}
/**
* @describe [T!]!
*/
public function testHandlesNonNullListOfNonNulls()
2015-07-15 20:05:46 +03:00
{
$type = Type::nonNull(Type::listOf(Type::nonNull(Type::int())));
// Contains values
$this->check(
$type,
[ 1, 2 ],
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
// Contains null
$this->check(
$type,
[ 1, null, 2 ],
[
'data' => [ 'nest' => null ],
'errors' => [
FormattedError::create(
'Cannot return null for non-nullable field DataType.test.',
[ new SourceLocation(1, 10) ]
)
2015-07-15 20:05:46 +03:00
]
]
);
// Returns null
$this->check(
$type,
null,
[
'data' => [ 'nest' => null ],
'errors' => [
FormattedError::create(
'Cannot return null for non-nullable field DataType.test.',
[ new SourceLocation(1, 10) ]
)
2015-07-15 20:05:46 +03:00
]
]
);
2015-07-15 20:05:46 +03:00
}
2016-04-09 10:36:53 +03:00
}