graphql-php/tests/Error/PrintErrorTest.php
Daniel Tschinder cf276340a4 Fix printError/locations for multiple nodes.
If a GraphQLError represents multiple nodes across files (could happen for validation across multiple parsed files) then the reported locations and printError output can be incorrect for the second node. This ensures locations are derived from nodes whenever possible to get correct location and amends comment documentation.
ref: graphql/graphql-js#1131
2018-02-13 10:42:51 +01:00

62 lines
1.2 KiB
PHP

<?php
namespace GraphQL\Tests\Error;
use GraphQL\Error\Error;
use GraphQL\Error\FormattedError;
use GraphQL\Language\Parser;
use GraphQL\Language\Source;
class PrintErrorTest extends \PHPUnit_Framework_TestCase
{
// Describe printError
/**
* @it prints an error with nodes from different sources
*/
public function testPrintsAnErrorWithNodesFromDifferentSources()
{
$sourceA = Parser::parse(new Source('type Foo {
field: String
}',
'SourceA'
));
$fieldTypeA = $sourceA->definitions[0]->fields[0]->type;
$sourceB = Parser::parse(new Source('type Foo {
field: Int
}',
'SourceB'
));
$fieldTypeB = $sourceB->definitions[0]->fields[0]->type;
$error = new Error(
'Example error with two nodes',
[
$fieldTypeA,
$fieldTypeB,
]
);
$this->assertEquals(
'Example error with two nodes
SourceA (2:10)
1: type Foo {
2: field: String
^
3: }
SourceB (2:10)
1: type Foo {
2: field: Int
^
3: }
',
FormattedError::printError($error)
);
}
}