graphql-php/tests/Executor/ListsTest.php

691 lines
20 KiB
PHP
Raw Permalink Normal View History

2015-07-15 20:05:46 +03:00
<?php
2016-11-27 00:28:46 +03:00
2016-04-09 10:36:53 +03:00
namespace GraphQL\Tests\Executor;
2015-07-15 20:05:46 +03:00
2016-12-03 00:11:14 +03:00
use GraphQL\Deferred;
use GraphQL\Error\UserError;
2016-04-09 10:36:53 +03:00
use GraphQL\Executor\Executor;
use GraphQL\Error\FormattedError;
2015-07-15 20:05:46 +03:00
use GraphQL\Language\Parser;
use GraphQL\Language\SourceLocation;
use GraphQL\Type\Schema;
2015-07-15 20:05:46 +03:00
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
class ListsTest extends \PHPUnit_Framework_TestCase
{
// Describe: Execute: Handles list nullability
2015-07-15 20:05:46 +03:00
/**
* @describe [T]
*/
2016-11-27 00:28:46 +03:00
public function testHandlesNullableListsWithArray()
2015-07-15 20:05:46 +03:00
{
// Contains values
2016-11-27 00:28:46 +03:00
$this->checkHandlesNullableLists(
[ 1, 2 ],
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
// Contains null
2016-11-27 00:28:46 +03:00
$this->checkHandlesNullableLists(
[ 1, null, 2 ],
[ 'data' => [ 'nest' => [ 'test' => [ 1, null, 2 ] ] ] ]
);
// Returns null
2016-11-27 00:28:46 +03:00
$this->checkHandlesNullableLists(
null,
[ 'data' => [ 'nest' => [ 'test' => null ] ] ]
);
2015-07-15 20:05:46 +03:00
}
/**
2016-11-27 00:28:46 +03:00
* @describe [T]
*/
public function testHandlesNullableListsWithPromiseArray()
{
// Contains values
$this->checkHandlesNullableLists(
2016-12-03 00:11:14 +03:00
new Deferred(function() {
return [1,2];
}),
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
// Contains null
$this->checkHandlesNullableLists(
2016-12-03 00:11:14 +03:00
new Deferred(function() {
return [1, null, 2];
}),
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => [ 1, null, 2 ] ] ] ]
);
// Returns null
$this->checkHandlesNullableLists(
2016-12-03 00:11:14 +03:00
new Deferred(function() {
return null;
}),
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => null ] ] ]
);
// Rejected
$this->checkHandlesNullableLists(
function () {
2016-12-03 00:11:14 +03:00
return new Deferred(function () {
throw new UserError('bad');
2016-12-03 00:11:14 +03:00
});
2016-11-27 00:28:46 +03:00
},
[
'data' => ['nest' => ['test' => null]],
'errors' => [
[
'message' => 'bad',
'locations' => [['line' => 1, 'column' => 10]],
'path' => ['nest', 'test']
]
]
]
);
}
/**
* @describe [T]
*/
2016-11-27 00:28:46 +03:00
public function testHandlesNullableListsWithArrayPromise()
2015-07-15 20:05:46 +03:00
{
2016-11-27 00:28:46 +03:00
// Contains values
$this->checkHandlesNullableLists(
2016-12-03 00:11:14 +03:00
[
new Deferred(function() {
return 1;
}),
new Deferred(function() {
return 2;
})],
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
// Contains null
$this->checkHandlesNullableLists(
2016-12-03 00:11:14 +03:00
[
new Deferred(function() {return 1;}),
new Deferred(function() {return null;}),
new Deferred(function() {return 2;})
],
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => [ 1, null, 2 ] ] ] ]
);
// Returns null
$this->checkHandlesNullableLists(
2016-12-03 00:23:21 +03:00
new Deferred(function() {
return null;
}),
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => null ] ] ]
);
// Contains reject
$this->checkHandlesNullableLists(
function () {
2016-12-03 00:11:14 +03:00
return [
new Deferred(function() {
return 1;
}),
new Deferred(function() {
throw new UserError('bad');
2016-12-03 00:11:14 +03:00
}),
new Deferred(function() {
return 2;
})
];
2016-11-27 00:28:46 +03:00
},
[
'data' => ['nest' => ['test' => [1, null, 2]]],
'errors' => [
[
'message' => 'bad',
'locations' => [['line' => 1, 'column' => 10]],
'path' => ['nest', 'test', 1]
]
]
]
);
}
2016-11-27 00:28:46 +03:00
/**
* @describe [T]!
*/
public function testHandlesNonNullableListsWithArray()
{
// Contains values
2016-11-27 00:28:46 +03:00
$this->checkHandlesNonNullableLists(
[ 1, 2 ],
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
// Contains null
2016-11-27 00:28:46 +03:00
$this->checkHandlesNonNullableLists(
[ 1, null, 2 ],
[ 'data' => [ 'nest' => [ 'test' => [ 1, null, 2 ] ] ] ]
);
// Returns null
2016-11-27 00:28:46 +03:00
$this->checkHandlesNonNullableLists(
null,
[
'data' => [ 'nest' => null ],
'errors' => [
[
'debugMessage' => 'Cannot return null for non-nullable field DataType.test.',
'locations' => [['line' => 1, 'column' => 10]]
]
2015-07-15 20:05:46 +03:00
]
],
true
);
2015-07-15 20:05:46 +03:00
}
/**
2016-11-27 00:28:46 +03:00
* @describe [T]!
*/
public function testHandlesNonNullableListsWithPromiseArray()
{
// Contains values
$this->checkHandlesNonNullableLists(
2016-12-03 00:11:14 +03:00
new Deferred(function() {
return [1,2];
}),
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
// Contains null
$this->checkHandlesNonNullableLists(
2016-12-03 00:11:14 +03:00
new Deferred(function() {
return [1, null, 2];
}),
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => [ 1, null, 2 ] ] ] ]
);
// Returns null
$this->checkHandlesNonNullableLists(
null,
[
'data' => [ 'nest' => null ],
'errors' => [
[
'debugMessage' => 'Cannot return null for non-nullable field DataType.test.',
'locations' => [['line' => 1, 'column' => 10]]
]
2016-11-27 00:28:46 +03:00
]
],
true
2016-11-27 00:28:46 +03:00
);
// Rejected
$this->checkHandlesNonNullableLists(
function () {
2016-12-03 00:11:14 +03:00
return new Deferred(function() {
throw new UserError('bad');
2016-12-03 00:11:14 +03:00
});
2016-11-27 00:28:46 +03:00
},
[
'data' => ['nest' => null],
'errors' => [
[
'message' => 'bad',
'locations' => [['line' => 1, 'column' => 10]],
'path' => ['nest', 'test']
]
]
]
);
}
/**
* @describe [T]!
*/
2016-11-27 00:28:46 +03:00
public function testHandlesNonNullableListsWithArrayPromise()
2015-07-15 20:05:46 +03:00
{
2016-11-27 00:28:46 +03:00
// Contains values
$this->checkHandlesNonNullableLists(
2016-12-03 00:11:14 +03:00
[
new Deferred(function() {
return 1;
}),
new Deferred(function() {
return 2;
})
],
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
// Contains null
$this->checkHandlesNonNullableLists(
2016-12-03 00:11:14 +03:00
[
new Deferred(function() {
return 1;
}),
new Deferred(function() {
return null;
}),
new Deferred(function() {
return 2;
})
],
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => [ 1, null, 2 ] ] ] ]
);
2016-11-27 00:28:46 +03:00
// Contains reject
$this->checkHandlesNonNullableLists(
function () {
2016-12-03 00:11:14 +03:00
return [
new Deferred(function() {
return 1;
}),
new Deferred(function() {
throw new UserError('bad');
2016-12-03 00:11:14 +03:00
}),
new Deferred(function() {
return 2;
})
];
2016-11-27 00:28:46 +03:00
},
[
'data' => ['nest' => ['test' => [1, null, 2]]],
'errors' => [
[
'message' => 'bad',
'locations' => [['line' => 1, 'column' => 10]],
'path' => ['nest', 'test', 1]
]
]
]
);
}
/**
* @describe [T!]
*/
public function testHandlesListOfNonNullsWithArray()
{
// Contains values
2016-11-27 00:28:46 +03:00
$this->checkHandlesListOfNonNulls(
[ 1, 2 ],
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
// Contains null
2016-11-27 00:28:46 +03:00
$this->checkHandlesListOfNonNulls(
[ 1, null, 2 ],
[
'data' => [ 'nest' => [ 'test' => null ] ],
'errors' => [
[
'debugMessage' => 'Cannot return null for non-nullable field DataType.test.',
'locations' => [ ['line' => 1, 'column' => 10] ]
]
2015-07-15 20:05:46 +03:00
]
],
true
);
// Returns null
2016-11-27 00:28:46 +03:00
$this->checkHandlesListOfNonNulls(
null,
[ 'data' => [ 'nest' => [ 'test' => null ] ] ]
);
2015-07-15 20:05:46 +03:00
}
/**
2016-11-27 00:28:46 +03:00
* @describe [T!]
*/
2016-11-27 00:28:46 +03:00
public function testHandlesListOfNonNullsWithPromiseArray()
2015-07-15 20:05:46 +03:00
{
2016-11-27 00:28:46 +03:00
// Contains values
$this->checkHandlesListOfNonNulls(
2016-12-03 00:11:14 +03:00
new Deferred(function() {
return [1, 2];
}),
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
2016-11-27 00:28:46 +03:00
// Contains null
$this->checkHandlesListOfNonNulls(
2016-12-03 00:11:14 +03:00
new Deferred(function() {
return [1, null, 2];
}),
2016-11-27 00:28:46 +03:00
[
'data' => [ 'nest' => [ 'test' => null ] ],
'errors' => [
[
'debugMessage' => 'Cannot return null for non-nullable field DataType.test.',
'locations' => [['line' => 1, 'column' => 10]]
]
2016-11-27 00:28:46 +03:00
]
],
true
2016-11-27 00:28:46 +03:00
);
// Returns null
$this->checkHandlesListOfNonNulls(
2016-12-03 00:23:21 +03:00
new Deferred(function() {
return null;
}),
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => null ] ] ]
);
// Rejected
$this->checkHandlesListOfNonNulls(
function () {
2016-12-03 00:11:14 +03:00
return new Deferred(function() {
throw new UserError('bad');
2016-12-03 00:11:14 +03:00
});
2016-11-27 00:28:46 +03:00
},
[
'data' => ['nest' => ['test' => null]],
'errors' => [
[
'message' => 'bad',
'locations' => [['line' => 1, 'column' => 10]],
'path' => ['nest', 'test']
]
]
]
);
}
/**
* @describe [T]!
*/
public function testHandlesListOfNonNullsWithArrayPromise()
{
// Contains values
$this->checkHandlesListOfNonNulls(
2016-12-03 00:11:14 +03:00
[
new Deferred(function() {
return 1;
}),
new Deferred(function() {
return 2;
})
],
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
// Contains null
$this->checkHandlesListOfNonNulls(
2016-12-03 00:23:21 +03:00
[
new Deferred(function() {return 1;}),
new Deferred(function() {return null;}),
new Deferred(function() {return 2;})
],
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => null ] ] ]
);
// Contains reject
$this->checkHandlesListOfNonNulls(
function () {
2016-12-03 00:11:14 +03:00
return [
new Deferred(function() {
return 1;
}),
new Deferred(function() {
throw new UserError('bad');
2016-12-03 00:11:14 +03:00
}),
new Deferred(function() {
return 2;
})
];
2016-11-27 00:28:46 +03:00
},
[
'data' => ['nest' => ['test' => null]],
'errors' => [
[
'message' => 'bad',
'locations' => [['line' => 1, 'column' => 10]],
'path' => ['nest', 'test', 1]
]
]
]
);
}
/**
* @describe [T!]!
*/
public function testHandlesNonNullListOfNonNullsWithArray()
{
// Contains values
2016-11-27 00:28:46 +03:00
$this->checkHandlesNonNullListOfNonNulls(
[ 1, 2 ],
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
// Contains null
2016-11-27 00:28:46 +03:00
$this->checkHandlesNonNullListOfNonNulls(
[ 1, null, 2 ],
[
'data' => [ 'nest' => null ],
'errors' => [
[
'debugMessage' => 'Cannot return null for non-nullable field DataType.test.',
'locations' => [['line' => 1, 'column' => 10 ]]
]
2015-07-15 20:05:46 +03:00
]
],
true
);
// Returns null
2016-11-27 00:28:46 +03:00
$this->checkHandlesNonNullListOfNonNulls(
null,
[
'data' => [ 'nest' => null ],
'errors' => [
[
'debugMessage' => 'Cannot return null for non-nullable field DataType.test.',
'locations' => [ ['line' => 1, 'column' => 10] ]
]
2015-07-15 20:05:46 +03:00
]
],
true
);
2015-07-15 20:05:46 +03:00
}
2016-11-27 00:28:46 +03:00
/**
* @describe [T!]!
*/
public function testHandlesNonNullListOfNonNullsWithPromiseArray()
{
// Contains values
$this->checkHandlesNonNullListOfNonNulls(
2016-12-03 00:11:14 +03:00
new Deferred(function() {
return [1, 2];
}),
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
// Contains null
$this->checkHandlesNonNullListOfNonNulls(
2016-12-03 00:11:14 +03:00
new Deferred(function() {
return [1, null, 2];
}),
2016-11-27 00:28:46 +03:00
[
'data' => [ 'nest' => null ],
'errors' => [
[
'debugMessage' => 'Cannot return null for non-nullable field DataType.test.',
'locations' => [ ['line' => 1, 'column' => 10] ]
]
2016-11-27 00:28:46 +03:00
]
],
true
2016-11-27 00:28:46 +03:00
);
// Returns null
$this->checkHandlesNonNullListOfNonNulls(
2016-12-03 00:11:14 +03:00
new Deferred(function() {
return null;
}),
2016-11-27 00:28:46 +03:00
[
'data' => [ 'nest' => null ],
'errors' => [
[
'debugMessage' => 'Cannot return null for non-nullable field DataType.test.',
'locations' => [ ['line' => 1, 'column' => 10] ]
]
2016-11-27 00:28:46 +03:00
]
],
true
2016-11-27 00:28:46 +03:00
);
// Rejected
$this->checkHandlesNonNullListOfNonNulls(
function () {
2016-12-03 00:11:14 +03:00
return new Deferred(function() {
throw new UserError('bad');
2016-12-03 00:11:14 +03:00
});
2016-11-27 00:28:46 +03:00
},
[
'data' => ['nest' => null ],
'errors' => [
[
'message' => 'bad',
'locations' => [['line' => 1, 'column' => 10]],
'path' => ['nest', 'test']
]
]
]
);
}
/**
* @describe [T!]!
*/
public function testHandlesNonNullListOfNonNullsWithArrayPromise()
{
// Contains values
$this->checkHandlesNonNullListOfNonNulls(
2016-12-03 00:11:14 +03:00
[
new Deferred(function() {
return 1;
}),
new Deferred(function() {
return 2;
})
],
2016-11-27 00:28:46 +03:00
[ 'data' => [ 'nest' => [ 'test' => [ 1, 2 ] ] ] ]
);
// Contains null
$this->checkHandlesNonNullListOfNonNulls(
2016-12-03 00:11:14 +03:00
[
new Deferred(function() {
return 1;
}),
new Deferred(function() {
return null;
}),
new Deferred(function() {
return 2;
})
],
2016-11-27 00:28:46 +03:00
[
'data' => [ 'nest' => null ],
'errors' => [
[
'debugMessage' => 'Cannot return null for non-nullable field DataType.test.',
'locations' => [['line' => 1, 'column' => 10]]
]
2016-11-27 00:28:46 +03:00
]
],
true
2016-11-27 00:28:46 +03:00
);
// Contains reject
$this->checkHandlesNonNullListOfNonNulls(
function () {
2016-12-03 00:11:14 +03:00
return [
new Deferred(function() {
return 1;
}),
new Deferred(function() {
throw new UserError('bad');
2016-12-03 00:11:14 +03:00
}),
new Deferred(function() {
return 2;
})
];
2016-11-27 00:28:46 +03:00
},
[
'data' => ['nest' => null ],
'errors' => [
[
'message' => 'bad',
'locations' => [['line' => 1, 'column' => 10]],
'path' => ['nest', 'test']
]
]
]
);
}
private function checkHandlesNullableLists($testData, $expected)
{
2016-12-03 00:11:14 +03:00
$testType = Type::listOf(Type::int());
2016-11-27 00:28:46 +03:00
$this->check($testType, $testData, $expected);
}
private function checkHandlesNonNullableLists($testData, $expected, $debug = false)
2016-11-27 00:28:46 +03:00
{
$testType = Type::nonNull(Type::listOf(Type::int()));
$this->check($testType, $testData, $expected, $debug);
2016-11-27 00:28:46 +03:00
}
private function checkHandlesListOfNonNulls($testData, $expected, $debug = false)
2016-11-27 00:28:46 +03:00
{
$testType = Type::listOf(Type::nonNull(Type::int()));
$this->check($testType, $testData, $expected, $debug);
2016-11-27 00:28:46 +03:00
}
public function checkHandlesNonNullListOfNonNulls($testData, $expected, $debug = false)
2016-11-27 00:28:46 +03:00
{
$testType = Type::nonNull(Type::listOf(Type::nonNull(Type::int())));
$this->check($testType, $testData, $expected, $debug);
2016-11-27 00:28:46 +03:00
}
private function check($testType, $testData, $expected, $debug = false)
2016-11-27 00:28:46 +03:00
{
$data = ['test' => $testData];
$dataType = null;
$dataType = new ObjectType([
'name' => 'DataType',
'fields' => function () use (&$testType, &$dataType, $data) {
return [
'test' => [
'type' => $testType
],
'nest' => [
'type' => $dataType,
'resolve' => function () use ($data) {
return $data;
}
]
];
}
]);
$schema = new Schema([
'query' => $dataType
]);
$ast = Parser::parse('{ nest { test } }');
$result = Executor::execute($schema, $ast, $data);
$this->assertArraySubset($expected, $result->toArray($debug));
2016-11-27 00:28:46 +03:00
}
2016-04-09 10:36:53 +03:00
}