graphql-php/tests/Error/ErrorTest.php

161 lines
4.7 KiB
PHP
Raw Normal View History

<?php
2018-08-28 12:08:43 +03:00
declare(strict_types=1);
namespace GraphQL\Tests\Error;
use GraphQL\Error\Error;
use GraphQL\Language\Parser;
use GraphQL\Language\Source;
use GraphQL\Language\SourceLocation;
2018-07-29 18:43:10 +03:00
use PHPUnit\Framework\TestCase;
2018-07-29 18:43:10 +03:00
class ErrorTest extends TestCase
{
/**
2018-08-28 12:08:43 +03:00
* @see it('uses the stack of an original error')
*/
2018-08-28 12:08:43 +03:00
public function testUsesTheStackOfAnOriginalError() : void
{
2018-08-28 12:08:43 +03:00
$prev = new \Exception('Original');
$err = new Error('msg', null, null, null, null, $prev);
$this->assertSame($err->getPrevious(), $prev);
}
/**
2018-08-28 12:08:43 +03:00
* @see it('converts nodes to positions and locations')
*/
2018-08-28 12:08:43 +03:00
public function testConvertsNodesToPositionsAndLocations() : void
{
2018-08-28 12:08:43 +03:00
$source = new Source('{
field
}');
2018-08-28 12:08:43 +03:00
$ast = Parser::parse($source);
$fieldNode = $ast->definitions[0]->selectionSet->selections[0];
2018-08-28 12:08:43 +03:00
$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());
}
/**
2018-08-28 12:08:43 +03:00
* @see it('converts single node to positions and locations')
*/
2018-08-28 12:08:43 +03:00
public function testConvertSingleNodeToPositionsAndLocations() : void
{
2018-08-28 12:08:43 +03:00
$source = new Source('{
field
}');
2018-08-28 12:08:43 +03:00
$ast = Parser::parse($source);
$fieldNode = $ast->definitions[0]->selectionSet->selections[0];
2018-08-28 12:08:43 +03:00
$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());
}
/**
2018-08-28 12:08:43 +03:00
* @see it('converts node with loc.start === 0 to positions and locations')
*/
2018-08-28 12:08:43 +03:00
public function testConvertsNodeWithStart0ToPositionsAndLocations() : void
{
2018-08-28 12:08:43 +03:00
$source = new Source('{
field
}');
2018-08-28 12:08:43 +03:00
$ast = Parser::parse($source);
$operationNode = $ast->definitions[0];
2018-08-28 12:08:43 +03:00
$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());
}
/**
2018-08-28 12:08:43 +03:00
* @see it('converts source and positions to locations')
*/
2018-08-28 12:08:43 +03:00
public function testConvertsSourceAndPositionsToLocations() : void
{
$source = new Source('{
field
}');
2018-08-28 12:08:43 +03:00
$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());
}
/**
2018-08-28 12:08:43 +03:00
* @see it('serializes to include message')
*/
2018-08-28 12:08:43 +03:00
public function testSerializesToIncludeMessage() : void
{
$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')
*/
2018-08-28 12:08:43 +03:00
public function testSerializesToIncludeMessageAndLocations() : void
{
$node = Parser::parse('{ field }')->definitions[0]->selectionSet->selections[0];
2018-08-28 12:08:43 +03:00
$e = new Error('msg', [$node]);
$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')
*/
2018-08-28 12:08:43 +03:00
public function testSerializesToIncludePath() : void
{
$e = new Error(
'msg',
null,
null,
null,
2018-08-28 12:08:43 +03:00
['path', 3, 'to', 'field']
);
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());
}
/**
2018-08-28 12:08:43 +03:00
* @see it('default error formatter includes extension fields')
*/
2018-08-28 12:08:43 +03:00
public function testDefaultErrorFormatterIncludesExtensionFields() : void
{
$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()
);
}
}