graphql-php/tests/Error/ErrorTest.php

162 lines
4.7 KiB
PHP
Raw Permalink Normal View History

<?php
2018-08-28 12:08:43 +03:00
declare(strict_types=1);
namespace GraphQL\Tests\Error;
2018-09-26 12:07:23 +03:00
use Exception;
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-09-26 12:07:23 +03:00
$prev = new Exception('Original');
2018-08-28 12:08:43 +03:00
$err = new Error('msg', null, null, null, null, $prev);
2018-09-19 18:12:09 +03:00
self::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]);
2018-09-19 18:12:09 +03:00
self::assertEquals([$fieldNode], $e->nodes);
self::assertEquals($source, $e->getSource());
self::assertEquals([8], $e->getPositions());
self::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.
2018-09-19 18:12:09 +03:00
self::assertEquals([$fieldNode], $e->nodes);
self::assertEquals($source, $e->getSource());
self::assertEquals([8], $e->getPositions());
self::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]);
2018-09-19 18:12:09 +03:00
self::assertEquals([$operationNode], $e->nodes);
self::assertEquals($source, $e->getSource());
self::assertEquals([0], $e->getPositions());
self::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]);
2018-09-19 18:12:09 +03:00
self::assertEquals(null, $e->nodes);
self::assertEquals($source, $e->getSource());
self::assertEquals([10], $e->getPositions());
self::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');
2018-09-19 18:12:09 +03:00
self::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]);
2018-09-19 18:12:09 +03:00
self::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-09-19 18:12:09 +03:00
self::assertEquals(['path', 3, 'to', 'field'], $e->path);
self::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']
);
2018-09-19 18:12:09 +03:00
self::assertEquals(['foo' => 'bar'], $e->getExtensions());
self::assertEquals(
2018-08-28 12:08:43 +03:00
[
'message' => 'msg',
'extensions' => ['foo' => 'bar'],
],
$e->toSerializableArray()
);
}
}