2016-10-18 18:15:21 +03:00
|
|
|
<?php
|
2018-08-28 12:08:43 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-02-13 12:37:00 +03:00
|
|
|
namespace GraphQL\Tests\Error;
|
2016-10-18 18:15:21 +03:00
|
|
|
|
2016-10-21 12:39:57 +03:00
|
|
|
use GraphQL\Error\Error;
|
2016-10-18 18:15:21 +03:00
|
|
|
use GraphQL\Language\Parser;
|
|
|
|
use GraphQL\Language\Source;
|
|
|
|
use GraphQL\Language\SourceLocation;
|
2018-07-29 18:43:10 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-10-18 18:15:21 +03:00
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class ErrorTest extends TestCase
|
2016-10-18 18:15:21 +03:00
|
|
|
{
|
|
|
|
/**
|
2018-08-28 12:08:43 +03:00
|
|
|
* @see it('uses the stack of an original error')
|
2016-10-18 18:15:21 +03:00
|
|
|
*/
|
2018-08-28 12:08:43 +03:00
|
|
|
public function testUsesTheStackOfAnOriginalError() : void
|
2016-10-18 18:15:21 +03:00
|
|
|
{
|
2018-08-28 12:08:43 +03:00
|
|
|
$prev = new \Exception('Original');
|
|
|
|
$err = new Error('msg', null, null, null, null, $prev);
|
2016-10-18 18:15:21 +03:00
|
|
|
|
|
|
|
$this->assertSame($err->getPrevious(), $prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-28 12:08:43 +03:00
|
|
|
* @see it('converts nodes to positions and locations')
|
2016-10-18 18:15:21 +03:00
|
|
|
*/
|
2018-08-28 12:08:43 +03:00
|
|
|
public function testConvertsNodesToPositionsAndLocations() : void
|
2016-10-18 18:15:21 +03:00
|
|
|
{
|
2018-08-28 12:08:43 +03:00
|
|
|
$source = new Source('{
|
2016-10-18 18:15:21 +03:00
|
|
|
field
|
|
|
|
}');
|
2018-08-28 12:08:43 +03:00
|
|
|
$ast = Parser::parse($source);
|
2016-11-19 02:47:55 +03:00
|
|
|
$fieldNode = $ast->definitions[0]->selectionSet->selections[0];
|
2018-08-28 12:08:43 +03:00
|
|
|
$e = new Error('msg', [$fieldNode]);
|
2016-10-18 18:15:21 +03:00
|
|
|
|
2016-11-19 02:47:55 +03:00
|
|
|
$this->assertEquals([$fieldNode], $e->nodes);
|
2016-10-18 18:15:21 +03:00
|
|
|
$this->assertEquals($source, $e->getSource());
|
|
|
|
$this->assertEquals([8], $e->getPositions());
|
|
|
|
$this->assertEquals([new SourceLocation(2, 7)], $e->getLocations());
|
|
|
|
}
|
|
|
|
|
2018-02-11 23:18:54 +03:00
|
|
|
/**
|
2018-08-28 12:08:43 +03:00
|
|
|
* @see it('converts single node to positions and locations')
|
2018-02-11 23:18:54 +03:00
|
|
|
*/
|
2018-08-28 12:08:43 +03:00
|
|
|
public function testConvertSingleNodeToPositionsAndLocations() : void
|
2018-02-11 23:18:54 +03:00
|
|
|
{
|
2018-08-28 12:08:43 +03:00
|
|
|
$source = new Source('{
|
2018-02-11 23:18:54 +03:00
|
|
|
field
|
|
|
|
}');
|
2018-08-28 12:08:43 +03:00
|
|
|
$ast = Parser::parse($source);
|
2018-02-11 23:18:54 +03:00
|
|
|
$fieldNode = $ast->definitions[0]->selectionSet->selections[0];
|
2018-08-28 12:08:43 +03:00
|
|
|
$e = new Error('msg', $fieldNode); // Non-array value.
|
2018-02-11 23:18:54 +03:00
|
|
|
|
|
|
|
$this->assertEquals([$fieldNode], $e->nodes);
|
|
|
|
$this->assertEquals($source, $e->getSource());
|
|
|
|
$this->assertEquals([8], $e->getPositions());
|
|
|
|
$this->assertEquals([new SourceLocation(2, 7)], $e->getLocations());
|
|
|
|
}
|
|
|
|
|
2016-10-18 18:15:21 +03:00
|
|
|
/**
|
2018-08-28 12:08:43 +03:00
|
|
|
* @see it('converts node with loc.start === 0 to positions and locations')
|
2016-10-18 18:15:21 +03:00
|
|
|
*/
|
2018-08-28 12:08:43 +03:00
|
|
|
public function testConvertsNodeWithStart0ToPositionsAndLocations() : void
|
2016-10-18 18:15:21 +03:00
|
|
|
{
|
2018-08-28 12:08:43 +03:00
|
|
|
$source = new Source('{
|
2016-10-18 18:15:21 +03:00
|
|
|
field
|
|
|
|
}');
|
2018-08-28 12:08:43 +03:00
|
|
|
$ast = Parser::parse($source);
|
2016-11-19 02:47:55 +03:00
|
|
|
$operationNode = $ast->definitions[0];
|
2018-08-28 12:08:43 +03:00
|
|
|
$e = new Error('msg', [$operationNode]);
|
2016-10-18 18:15:21 +03:00
|
|
|
|
2016-11-19 02:47:55 +03:00
|
|
|
$this->assertEquals([$operationNode], $e->nodes);
|
2016-10-18 18:15:21 +03:00
|
|
|
$this->assertEquals($source, $e->getSource());
|
|
|
|
$this->assertEquals([0], $e->getPositions());
|
|
|
|
$this->assertEquals([new SourceLocation(1, 1)], $e->getLocations());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-28 12:08:43 +03:00
|
|
|
* @see it('converts source and positions to locations')
|
2016-10-18 18:15:21 +03:00
|
|
|
*/
|
2018-08-28 12:08:43 +03:00
|
|
|
public function testConvertsSourceAndPositionsToLocations() : void
|
2016-10-18 18:15:21 +03:00
|
|
|
{
|
|
|
|
$source = new Source('{
|
|
|
|
field
|
|
|
|
}');
|
2018-08-28 12:08:43 +03:00
|
|
|
$e = new Error('msg', null, $source, [10]);
|
2016-10-18 18:15:21 +03:00
|
|
|
|
|
|
|
$this->assertEquals(null, $e->nodes);
|
|
|
|
$this->assertEquals($source, $e->getSource());
|
|
|
|
$this->assertEquals([10], $e->getPositions());
|
|
|
|
$this->assertEquals([new SourceLocation(2, 9)], $e->getLocations());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-28 12:08:43 +03:00
|
|
|
* @see it('serializes to include message')
|
2016-10-18 18:15:21 +03:00
|
|
|
*/
|
2018-08-28 12:08:43 +03:00
|
|
|
public function testSerializesToIncludeMessage() : void
|
2016-10-18 18:15:21 +03:00
|
|
|
{
|
|
|
|
$e = new Error('msg');
|
|
|
|
$this->assertEquals(['message' => 'msg'], $e->toSerializableArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-28 12:08:43 +03:00
|
|
|
* @see it('serializes to include message and locations')
|
2016-10-18 18:15:21 +03:00
|
|
|
*/
|
2018-08-28 12:08:43 +03:00
|
|
|
public function testSerializesToIncludeMessageAndLocations() : void
|
2016-10-18 18:15:21 +03:00
|
|
|
{
|
|
|
|
$node = Parser::parse('{ field }')->definitions[0]->selectionSet->selections[0];
|
2018-08-28 12:08:43 +03:00
|
|
|
$e = new Error('msg', [$node]);
|
2016-10-18 18:15:21 +03:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
['message' => 'msg', 'locations' => [['line' => 1, 'column' => 3]]],
|
|
|
|
$e->toSerializableArray()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-28 12:08:43 +03:00
|
|
|
* @see it('serializes to include path')
|
2016-10-18 18:15:21 +03:00
|
|
|
*/
|
2018-08-28 12:08:43 +03:00
|
|
|
public function testSerializesToIncludePath() : void
|
2016-10-18 18:15:21 +03:00
|
|
|
{
|
|
|
|
$e = new Error(
|
|
|
|
'msg',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
2018-08-28 12:08:43 +03:00
|
|
|
['path', 3, 'to', 'field']
|
2016-10-18 18:15:21 +03:00
|
|
|
);
|
|
|
|
|
2018-08-28 12:08:43 +03:00
|
|
|
$this->assertEquals(['path', 3, 'to', 'field'], $e->path);
|
|
|
|
$this->assertEquals(['message' => 'msg', 'path' => ['path', 3, 'to', 'field']], $e->toSerializableArray());
|
2016-10-18 18:15:21 +03:00
|
|
|
}
|
2018-02-09 18:26:19 +03:00
|
|
|
|
|
|
|
/**
|
2018-08-28 12:08:43 +03:00
|
|
|
* @see it('default error formatter includes extension fields')
|
2018-02-09 18:26:19 +03:00
|
|
|
*/
|
2018-08-28 12:08:43 +03:00
|
|
|
public function testDefaultErrorFormatterIncludesExtensionFields() : void
|
2018-02-09 18:26:19 +03:00
|
|
|
{
|
|
|
|
$e = new Error(
|
|
|
|
'msg',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
['foo' => 'bar']
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(['foo' => 'bar'], $e->getExtensions());
|
2018-08-28 12:08:43 +03:00
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'message' => 'msg',
|
|
|
|
'extensions' => ['foo' => 'bar'],
|
|
|
|
],
|
|
|
|
$e->toSerializableArray()
|
|
|
|
);
|
2018-02-09 18:26:19 +03:00
|
|
|
}
|
2016-10-18 18:15:21 +03:00
|
|
|
}
|