graphql-php/tests/Error/PrintErrorTest.php

104 lines
2.2 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\Error\FormattedError;
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 PrintErrorTest extends TestCase
{
/**
2018-08-28 12:08:43 +03:00
* @see it('prints an line numbers with correct padding')
*/
2018-08-28 12:08:43 +03:00
public function testPrintsAnLineNumbersWithCorrectPadding() : void
{
$singleDigit = new Error(
'Single digit line number with no padding',
null,
new Source('*', 'Test', new SourceLocation(9, 1)),
[0]
);
2018-08-28 12:08:43 +03:00
$actual = FormattedError::printError($singleDigit);
$expected = 'Single digit line number with no padding
Test (9:1)
9: *
^
';
$this->assertEquals($expected, $actual);
$doubleDigit = new Error(
'Left padded first line number',
null,
new Source("*\n", 'Test', new SourceLocation(9, 1)),
[0]
);
2018-08-28 12:08:43 +03:00
$actual = FormattedError::printError($doubleDigit);
$expected = 'Left padded first line number
Test (9:1)
9: *
^
10:
';
$this->assertEquals($expected, $actual);
}
/**
2018-08-28 12:08:43 +03:00
* @see it('prints an error with nodes from different sources')
*/
2018-08-28 12:08:43 +03:00
public function testPrintsAnErrorWithNodesFromDifferentSources() : void
{
2018-08-28 12:08:43 +03:00
$sourceA = Parser::parse(new Source(
'type Foo {
field: String
}',
2018-08-28 12:08:43 +03:00
'SourceA'
));
$fieldTypeA = $sourceA->definitions[0]->fields[0]->type;
2018-08-28 12:08:43 +03:00
$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)
);
}
}