graphql-php/tests/Executor/NonNullTest.php

804 lines
23 KiB
PHP
Raw Normal View History

2015-07-15 20:05:46 +03:00
<?php
2016-11-26 22:48:01 +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 NonNullTest extends \PHPUnit_Framework_TestCase
{
/** @var \Exception */
2015-07-15 20:05:46 +03:00
public $syncError;
/** @var \Exception */
2015-07-15 20:05:46 +03:00
public $nonNullSyncError;
2016-11-26 22:48:01 +03:00
/** @var \Exception */
public $promiseError;
/** @var \Exception */
public $nonNullPromiseError;
2015-07-15 20:05:46 +03:00
public $throwingData;
public $nullingData;
public $schema;
public function setUp()
{
$this->syncError = new UserError('sync');
$this->nonNullSyncError = new UserError('nonNullSync');
$this->promiseError = new UserError('promise');
$this->nonNullPromiseError = new UserError('nonNullPromise');
2015-07-15 20:05:46 +03:00
$this->throwingData = [
'sync' => function () {
throw $this->syncError;
},
'nonNullSync' => function () {
throw $this->nonNullSyncError;
},
2016-11-26 22:48:01 +03:00
'promise' => function () {
2016-12-03 00:11:14 +03:00
return new Deferred(function () {
2016-11-26 22:48:01 +03:00
throw $this->promiseError;
});
},
'nonNullPromise' => function () {
2016-12-03 00:11:14 +03:00
return new Deferred(function () {
2016-11-26 22:48:01 +03:00
throw $this->nonNullPromiseError;
});
},
2015-07-15 20:05:46 +03:00
'nest' => function () {
return $this->throwingData;
},
'nonNullNest' => function () {
return $this->throwingData;
},
2016-11-26 22:48:01 +03:00
'promiseNest' => function () {
2016-12-03 00:11:14 +03:00
return new Deferred(function () {
return $this->throwingData;
2016-11-26 22:48:01 +03:00
});
},
'nonNullPromiseNest' => function () {
2016-12-03 00:11:14 +03:00
return new Deferred(function () {
return $this->throwingData;
2016-11-26 22:48:01 +03:00
});
},
2015-07-15 20:05:46 +03:00
];
$this->nullingData = [
'sync' => function () {
return null;
},
'nonNullSync' => function () {
return null;
},
2016-11-26 22:48:01 +03:00
'promise' => function () {
2016-12-03 00:11:14 +03:00
return new Deferred(function () {
return null;
2016-11-26 22:48:01 +03:00
});
},
'nonNullPromise' => function () {
2016-12-03 00:11:14 +03:00
return new Deferred(function () {
return null;
2016-11-26 22:48:01 +03:00
});
},
2015-07-15 20:05:46 +03:00
'nest' => function () {
return $this->nullingData;
},
'nonNullNest' => function () {
return $this->nullingData;
},
2016-11-26 22:48:01 +03:00
'promiseNest' => function () {
2016-12-03 00:11:14 +03:00
return new Deferred(function () {
return $this->nullingData;
2016-11-26 22:48:01 +03:00
});
},
'nonNullPromiseNest' => function () {
2016-12-03 00:11:14 +03:00
return new Deferred(function () {
return $this->nullingData;
2016-11-26 22:48:01 +03:00
});
},
2015-07-15 20:05:46 +03:00
];
$dataType = new ObjectType([
'name' => 'DataType',
'fields' => function() use (&$dataType) {
return [
'sync' => ['type' => Type::string()],
'nonNullSync' => ['type' => Type::nonNull(Type::string())],
2016-11-26 22:48:01 +03:00
'promise' => Type::string(),
'nonNullPromise' => Type::nonNull(Type::string()),
'nest' => $dataType,
2016-11-26 22:48:01 +03:00
'nonNullNest' => Type::nonNull($dataType),
'promiseNest' => $dataType,
'nonNullPromiseNest' => Type::nonNull($dataType),
];
}
2015-07-15 20:05:46 +03:00
]);
$this->schema = new Schema(['query' => $dataType]);
2015-07-15 20:05:46 +03:00
}
// Execute: handles non-nullable types
/**
* @it nulls a nullable field that throws synchronously
*/
2015-07-15 20:05:46 +03:00
public function testNullsANullableFieldThatThrowsSynchronously()
{
$doc = '
query Q {
sync
}
';
$ast = Parser::parse($doc);
$expected = [
'data' => [
'sync' => null,
],
'errors' => [
FormattedError::create(
$this->syncError->getMessage(),
2015-07-15 20:05:46 +03:00
[new SourceLocation(3, 9)]
)
]
];
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray());
2015-07-15 20:05:46 +03:00
}
2016-11-26 22:48:01 +03:00
public function testNullsANullableFieldThatThrowsInAPromise()
{
$doc = '
query Q {
promise
}
';
$ast = Parser::parse($doc);
$expected = [
'data' => [
'promise' => null,
],
'errors' => [
FormattedError::create(
$this->promiseError->getMessage(),
[new SourceLocation(3, 9)]
)
]
];
2016-12-03 00:11:14 +03:00
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray());
2016-11-26 22:48:01 +03:00
}
2015-07-15 20:05:46 +03:00
public function testNullsASynchronouslyReturnedObjectThatContainsANonNullableFieldThatThrowsSynchronously()
{
// nulls a synchronously returned object that contains a non-nullable field that throws synchronously
$doc = '
query Q {
nest {
nonNullSync,
}
}
';
$ast = Parser::parse($doc);
$expected = [
'data' => [
'nest' => null
],
'errors' => [
FormattedError::create($this->nonNullSyncError->getMessage(), [new SourceLocation(4, 11)])
2015-07-15 20:05:46 +03:00
]
];
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray());
2015-07-15 20:05:46 +03:00
}
2016-11-26 22:48:01 +03:00
public function testNullsAsynchronouslyReturnedObjectThatContainsANonNullableFieldThatThrowsInAPromise()
{
$doc = '
query Q {
nest {
nonNullPromise,
}
}
';
$ast = Parser::parse($doc);
$expected = [
'data' => [
'nest' => null
],
'errors' => [
FormattedError::create($this->nonNullPromiseError->getMessage(), [new SourceLocation(4, 11)])
]
];
2016-12-03 00:11:14 +03:00
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray());
2016-11-26 22:48:01 +03:00
}
public function testNullsAnObjectReturnedInAPromiseThatContainsANonNullableFieldThatThrowsSynchronously()
{
$doc = '
query Q {
promiseNest {
nonNullSync,
}
}
';
$ast = Parser::parse($doc);
$expected = [
'data' => [
'promiseNest' => null
],
'errors' => [
FormattedError::create($this->nonNullSyncError->getMessage(), [new SourceLocation(4, 11)])
]
];
2016-12-03 00:11:14 +03:00
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray());
2016-11-26 22:48:01 +03:00
}
public function testNullsAnObjectReturnedInAPromiseThatContainsANonNullableFieldThatThrowsInAPromise()
{
$doc = '
query Q {
promiseNest {
nonNullPromise,
}
}
';
$ast = Parser::parse($doc);
$expected = [
'data' => [
'promiseNest' => null
],
'errors' => [
FormattedError::create($this->nonNullPromiseError->getMessage(), [new SourceLocation(4, 11)])
]
];
2016-12-03 00:11:14 +03:00
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray());
2016-11-26 22:48:01 +03:00
}
2016-12-03 00:11:14 +03:00
/**
* @it nulls a complex tree of nullable fields that throw
*/
2015-07-15 20:05:46 +03:00
public function testNullsAComplexTreeOfNullableFieldsThatThrow()
{
$doc = '
query Q {
nest {
sync
2016-11-26 22:48:01 +03:00
promise
nest {
sync
promise
}
promiseNest {
sync
promise
}
}
promiseNest {
sync
promise
2015-07-15 20:05:46 +03:00
nest {
sync
2016-11-26 22:48:01 +03:00
promise
}
promiseNest {
sync
promise
2015-07-15 20:05:46 +03:00
}
}
}
2016-11-26 22:48:01 +03:00
';
2015-07-15 20:05:46 +03:00
$ast = Parser::parse($doc);
$expected = [
'data' => [
'nest' => [
'sync' => null,
2016-11-26 22:48:01 +03:00
'promise' => null,
2015-07-15 20:05:46 +03:00
'nest' => [
'sync' => null,
2016-11-26 22:48:01 +03:00
'promise' => null,
],
'promiseNest' => [
'sync' => null,
'promise' => null,
],
],
'promiseNest' => [
'sync' => null,
'promise' => null,
'nest' => [
'sync' => null,
'promise' => null,
],
'promiseNest' => [
'sync' => null,
'promise' => null,
],
],
2015-07-15 20:05:46 +03:00
],
'errors' => [
FormattedError::create($this->syncError->getMessage(), [new SourceLocation(4, 11)]),
2016-11-26 22:48:01 +03:00
FormattedError::create($this->syncError->getMessage(), [new SourceLocation(7, 13)]),
FormattedError::create($this->syncError->getMessage(), [new SourceLocation(11, 13)]),
FormattedError::create($this->syncError->getMessage(), [new SourceLocation(16, 11)]),
FormattedError::create($this->syncError->getMessage(), [new SourceLocation(19, 13)]),
2016-12-03 00:11:14 +03:00
FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(5, 11)]),
FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(8, 13)]),
FormattedError::create($this->syncError->getMessage(), [new SourceLocation(23, 13)]),
2016-12-03 00:11:14 +03:00
FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(12, 13)]),
FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(17, 11)]),
FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(20, 13)]),
2016-11-26 22:48:01 +03:00
FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(24, 13)]),
2015-07-15 20:05:46 +03:00
]
];
2016-11-26 22:48:01 +03:00
2016-12-03 00:11:14 +03:00
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray());
2016-11-26 22:48:01 +03:00
}
public function testNullsTheFirstNullableObjectAfterAFieldThrowsInALongChainOfFieldsThatAreNonNull()
{
$doc = '
query Q {
nest {
nonNullNest {
nonNullPromiseNest {
nonNullNest {
nonNullPromiseNest {
nonNullSync
}
}
}
}
}
promiseNest {
nonNullNest {
nonNullPromiseNest {
nonNullNest {
nonNullPromiseNest {
nonNullSync
}
}
}
}
}
anotherNest: nest {
nonNullNest {
nonNullPromiseNest {
nonNullNest {
nonNullPromiseNest {
nonNullPromise
}
}
}
}
}
anotherPromiseNest: promiseNest {
nonNullNest {
nonNullPromiseNest {
nonNullNest {
nonNullPromiseNest {
nonNullPromise
}
}
}
}
}
}
';
$ast = Parser::parse($doc);
$expected = [
'data' => [
'nest' => null,
'promiseNest' => null,
'anotherNest' => null,
'anotherPromiseNest' => null,
],
'errors' => [
FormattedError::create($this->nonNullSyncError->getMessage(), [new SourceLocation(8, 19)]),
FormattedError::create($this->nonNullSyncError->getMessage(), [new SourceLocation(19, 19)]),
FormattedError::create($this->nonNullPromiseError->getMessage(), [new SourceLocation(30, 19)]),
FormattedError::create($this->nonNullPromiseError->getMessage(), [new SourceLocation(41, 19)]),
]
];
2016-12-03 00:11:14 +03:00
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray());
2015-07-15 20:05:46 +03:00
}
public function testNullsANullableFieldThatSynchronouslyReturnsNull()
{
$doc = '
query Q {
sync
}
';
$ast = Parser::parse($doc);
$expected = [
'data' => [
'sync' => null,
]
];
$this->assertEquals($expected, Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray());
2015-07-15 20:05:46 +03:00
}
2016-11-26 22:48:01 +03:00
public function testNullsANullableFieldThatReturnsNullInAPromise()
{
$doc = '
query Q {
promise
}
';
$ast = Parser::parse($doc);
$expected = [
'data' => [
'promise' => null,
]
];
2016-12-03 00:11:14 +03:00
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray());
2016-11-26 22:48:01 +03:00
}
public function testNullsASynchronouslyReturnedObjectThatContainsANonNullableFieldThatReturnsNullSynchronously()
2015-07-15 20:05:46 +03:00
{
$doc = '
query Q {
nest {
nonNullSync,
}
}
';
$ast = Parser::parse($doc);
$expected = [
'data' => [
'nest' => null
],
'errors' => [
[
'debugMessage' => 'Cannot return null for non-nullable field DataType.nonNullSync.',
'locations' => [['line' => 4, 'column' => 11]]
]
2015-07-15 20:05:46 +03:00
]
];
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray(true));
2015-07-15 20:05:46 +03:00
}
2016-11-26 22:48:01 +03:00
public function testNullsASynchronouslyReturnedObjectThatContainsANonNullableFieldThatReturnsNullInAPromise()
2015-07-15 20:05:46 +03:00
{
2016-11-26 22:48:01 +03:00
$doc = '
query Q {
nest {
nonNullPromise,
}
}
';
$ast = Parser::parse($doc);
$expected = [
'data' => [
'nest' => null,
],
'errors' => [
[
'debugMessage' => 'Cannot return null for non-nullable field DataType.nonNullPromise.',
'locations' => [['line' => 4, 'column' => 11]]
],
2016-11-26 22:48:01 +03:00
]
];
2015-07-15 20:05:46 +03:00
$this->assertArraySubset(
$expected,
Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray(true)
);
2016-11-26 22:48:01 +03:00
}
public function testNullsAnObjectReturnedInAPromiseThatContainsANonNullableFieldThatReturnsNullSynchronously()
{
$doc = '
query Q {
promiseNest {
nonNullSync,
}
}
';
$ast = Parser::parse($doc);
$expected = [
'data' => [
'promiseNest' => null,
],
'errors' => [
[
'debugMessage' => 'Cannot return null for non-nullable field DataType.nonNullSync.',
'locations' => [['line' => 4, 'column' => 11]]
],
2016-11-26 22:48:01 +03:00
]
];
$this->assertArraySubset(
$expected,
Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray(true)
);
2016-11-26 22:48:01 +03:00
}
public function testNullsAnObjectReturnedInAPromiseThatContainsANonNullableFieldThatReturnsNullInaAPromise()
{
$doc = '
query Q {
promiseNest {
nonNullPromise,
}
}
';
$ast = Parser::parse($doc);
$expected = [
'data' => [
'promiseNest' => null,
],
'errors' => [
[
'debugMessage' => 'Cannot return null for non-nullable field DataType.nonNullPromise.',
'locations' => [['line' => 4, 'column' => 11]]
],
2016-11-26 22:48:01 +03:00
]
];
$this->assertArraySubset(
$expected,
Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray(true)
);
2016-11-26 22:48:01 +03:00
}
public function testNullsAComplexTreeOfNullableFieldsThatReturnNull()
{
2015-07-15 20:05:46 +03:00
$doc = '
query Q {
nest {
sync
2016-11-26 22:48:01 +03:00
promise
2015-07-15 20:05:46 +03:00
nest {
sync
2016-11-26 22:48:01 +03:00
promise
}
promiseNest {
sync
promise
}
}
promiseNest {
sync
promise
nest {
sync
promise
}
promiseNest {
sync
promise
2015-07-15 20:05:46 +03:00
}
}
}
';
$ast = Parser::parse($doc);
$expected = [
'data' => [
'nest' => [
'sync' => null,
2016-11-26 22:48:01 +03:00
'promise' => null,
2015-07-15 20:05:46 +03:00
'nest' => [
'sync' => null,
2016-11-26 22:48:01 +03:00
'promise' => null,
2015-07-15 20:05:46 +03:00
],
2016-11-26 22:48:01 +03:00
'promiseNest' => [
'sync' => null,
'promise' => null,
]
2015-07-15 20:05:46 +03:00
],
2016-11-26 22:48:01 +03:00
'promiseNest' => [
'sync' => null,
'promise' => null,
'nest' => [
'sync' => null,
'promise' => null,
],
'promiseNest' => [
'sync' => null,
'promise' => null,
]
]
2015-07-15 20:05:46 +03:00
]
];
2016-11-26 22:48:01 +03:00
2016-12-03 00:11:14 +03:00
$actual = Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray();
$this->assertEquals($expected, $actual);
2016-11-26 22:48:01 +03:00
}
public function testNullsTheFirstNullableObjectAfterAFieldReturnsNullInALongChainOfFieldsThatAreNonNull()
{
$doc = '
query Q {
nest {
nonNullNest {
nonNullPromiseNest {
nonNullNest {
nonNullPromiseNest {
nonNullSync
}
}
}
}
}
promiseNest {
nonNullNest {
nonNullPromiseNest {
nonNullNest {
nonNullPromiseNest {
nonNullSync
}
}
}
}
}
anotherNest: nest {
nonNullNest {
nonNullPromiseNest {
nonNullNest {
nonNullPromiseNest {
nonNullPromise
}
}
}
}
}
anotherPromiseNest: promiseNest {
nonNullNest {
nonNullPromiseNest {
nonNullNest {
nonNullPromiseNest {
nonNullPromise
}
}
}
}
}
}
';
$ast = Parser::parse($doc);
$expected = [
'data' => [
'nest' => null,
'promiseNest' => null,
'anotherNest' => null,
'anotherPromiseNest' => null,
],
'errors' => [
['debugMessage' => 'Cannot return null for non-nullable field DataType.nonNullSync.', 'locations' => [ ['line' => 8, 'column' => 19]]],
['debugMessage' => 'Cannot return null for non-nullable field DataType.nonNullSync.', 'locations' => [ ['line' => 19, 'column' => 19]]],
['debugMessage' => 'Cannot return null for non-nullable field DataType.nonNullPromise.', 'locations' => [ ['line' => 30, 'column' => 19]]],
['debugMessage' => 'Cannot return null for non-nullable field DataType.nonNullPromise.', 'locations' => [ ['line' => 41, 'column' => 19]]],
2016-11-26 22:48:01 +03:00
]
];
$this->assertArraySubset(
$expected,
Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray(true)
);
2015-07-15 20:05:46 +03:00
}
2016-12-03 00:11:14 +03:00
/**
* @it nulls the top level if sync non-nullable field throws
*/
2015-07-15 20:05:46 +03:00
public function testNullsTheTopLevelIfSyncNonNullableFieldThrows()
{
$doc = '
query Q { nonNullSync }
';
$expected = [
'errors' => [
FormattedError::create($this->nonNullSyncError->getMessage(), [new SourceLocation(2, 17)])
2015-07-15 20:05:46 +03:00
]
];
2016-11-26 22:48:01 +03:00
$actual = Executor::execute($this->schema, Parser::parse($doc), $this->throwingData)->toArray();
$this->assertArraySubset($expected, $actual);
}
public function testNullsTheTopLevelIfAsyncNonNullableFieldErrors()
{
$doc = '
query Q { nonNullPromise }
';
$ast = Parser::parse($doc);
$expected = [
'errors' => [
FormattedError::create($this->nonNullPromiseError->getMessage(), [new SourceLocation(2, 17)]),
]
];
2016-12-03 00:11:14 +03:00
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray());
2015-07-15 20:05:46 +03:00
}
public function testNullsTheTopLevelIfSyncNonNullableFieldReturnsNull()
{
// nulls the top level if sync non-nullable field returns null
$doc = '
query Q { nonNullSync }
';
$expected = [
'errors' => [
[
'debugMessage' => 'Cannot return null for non-nullable field DataType.nonNullSync.',
'locations' => [['line' => 2, 'column' => 17]]
],
2015-07-15 20:05:46 +03:00
]
];
$this->assertArraySubset(
$expected,
Executor::execute($this->schema, Parser::parse($doc), $this->nullingData)->toArray(true)
);
2015-07-15 20:05:46 +03:00
}
2016-11-26 22:48:01 +03:00
public function testNullsTheTopLevelIfAsyncNonNullableFieldResolvesNull()
{
$doc = '
query Q { nonNullPromise }
';
$ast = Parser::parse($doc);
$expected = [
'errors' => [
[
'debugMessage' => 'Cannot return null for non-nullable field DataType.nonNullPromise.',
'locations' => [['line' => 2, 'column' => 17]]
],
2016-11-26 22:48:01 +03:00
]
];
$this->assertArraySubset(
$expected,
Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray(true)
);
2016-11-26 22:48:01 +03:00
}
2015-07-15 20:05:46 +03:00
}