mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-25 06:16:05 +03:00
cf276340a4
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
151 lines
4.3 KiB
PHP
151 lines
4.3 KiB
PHP
<?php
|
|
namespace GraphQL\Tests\Error;
|
|
|
|
use GraphQL\Error\Error;
|
|
use GraphQL\Language\Parser;
|
|
use GraphQL\Language\Source;
|
|
use GraphQL\Language\SourceLocation;
|
|
|
|
class ErrorTest extends \PHPUnit_Framework_TestCase
|
|
{
|
|
/**
|
|
* @it uses the stack of an original error
|
|
*/
|
|
public function testUsesTheStackOfAnOriginalError()
|
|
{
|
|
$prev = new \Exception("Original");
|
|
$err = new Error('msg', null, null, null, null, $prev);
|
|
|
|
$this->assertSame($err->getPrevious(), $prev);
|
|
}
|
|
|
|
/**
|
|
* @it converts nodes to positions and locations
|
|
*/
|
|
public function testConvertsNodesToPositionsAndLocations()
|
|
{
|
|
$source = new Source('{
|
|
field
|
|
}');
|
|
$ast = Parser::parse($source);
|
|
$fieldNode = $ast->definitions[0]->selectionSet->selections[0];
|
|
$e = new Error('msg', [ $fieldNode ]);
|
|
|
|
$this->assertEquals([$fieldNode], $e->nodes);
|
|
$this->assertEquals($source, $e->getSource());
|
|
$this->assertEquals([8], $e->getPositions());
|
|
$this->assertEquals([new SourceLocation(2, 7)], $e->getLocations());
|
|
}
|
|
|
|
/**
|
|
* @it converts single node to positions and locations
|
|
*/
|
|
public function testConvertSingleNodeToPositionsAndLocations()
|
|
{
|
|
$source = new Source('{
|
|
field
|
|
}');
|
|
$ast = Parser::parse($source);
|
|
$fieldNode = $ast->definitions[0]->selectionSet->selections[0];
|
|
$e = new Error('msg', $fieldNode); // Non-array value.
|
|
|
|
$this->assertEquals([$fieldNode], $e->nodes);
|
|
$this->assertEquals($source, $e->getSource());
|
|
$this->assertEquals([8], $e->getPositions());
|
|
$this->assertEquals([new SourceLocation(2, 7)], $e->getLocations());
|
|
}
|
|
|
|
/**
|
|
* @it converts node with loc.start === 0 to positions and locations
|
|
*/
|
|
public function testConvertsNodeWithStart0ToPositionsAndLocations()
|
|
{
|
|
$source = new Source('{
|
|
field
|
|
}');
|
|
$ast = Parser::parse($source);
|
|
$operationNode = $ast->definitions[0];
|
|
$e = new Error('msg', [ $operationNode ]);
|
|
|
|
$this->assertEquals([$operationNode], $e->nodes);
|
|
$this->assertEquals($source, $e->getSource());
|
|
$this->assertEquals([0], $e->getPositions());
|
|
$this->assertEquals([new SourceLocation(1, 1)], $e->getLocations());
|
|
}
|
|
|
|
/**
|
|
* @it converts source and positions to locations
|
|
*/
|
|
public function testConvertsSourceAndPositionsToLocations()
|
|
{
|
|
$source = new Source('{
|
|
field
|
|
}');
|
|
$e = new Error('msg', null, $source, [ 10 ]);
|
|
|
|
$this->assertEquals(null, $e->nodes);
|
|
$this->assertEquals($source, $e->getSource());
|
|
$this->assertEquals([10], $e->getPositions());
|
|
$this->assertEquals([new SourceLocation(2, 9)], $e->getLocations());
|
|
}
|
|
|
|
/**
|
|
* @it serializes to include message
|
|
*/
|
|
public function testSerializesToIncludeMessage()
|
|
{
|
|
$e = new Error('msg');
|
|
$this->assertEquals(['message' => 'msg'], $e->toSerializableArray());
|
|
}
|
|
|
|
/**
|
|
* @it serializes to include message and locations
|
|
*/
|
|
public function testSerializesToIncludeMessageAndLocations()
|
|
{
|
|
$node = Parser::parse('{ field }')->definitions[0]->selectionSet->selections[0];
|
|
$e = new Error('msg', [ $node ]);
|
|
|
|
$this->assertEquals(
|
|
['message' => 'msg', 'locations' => [['line' => 1, 'column' => 3]]],
|
|
$e->toSerializableArray()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @it serializes to include path
|
|
*/
|
|
public function testSerializesToIncludePath()
|
|
{
|
|
$e = new Error(
|
|
'msg',
|
|
null,
|
|
null,
|
|
null,
|
|
[ 'path', 3, 'to', 'field' ]
|
|
);
|
|
|
|
$this->assertEquals([ 'path', 3, 'to', 'field' ], $e->path);
|
|
$this->assertEquals(['message' => 'msg', 'path' => [ 'path', 3, 'to', 'field' ]], $e->toSerializableArray());
|
|
}
|
|
|
|
/**
|
|
* @it default error formatter includes extension fields
|
|
*/
|
|
public function testDefaultErrorFormatterIncludesExtensionFields()
|
|
{
|
|
$e = new Error(
|
|
'msg',
|
|
null,
|
|
null,
|
|
null,
|
|
null,
|
|
null,
|
|
['foo' => 'bar']
|
|
);
|
|
|
|
$this->assertEquals(['foo' => 'bar'], $e->getExtensions());
|
|
$this->assertEquals(['message' => 'msg', 'foo' => 'bar'], $e->toSerializableArray());
|
|
}
|
|
}
|