2018-02-13 12:37:00 +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;
|
|
|
|
|
|
|
|
use GraphQL\Error\Error;
|
|
|
|
use GraphQL\Error\FormattedError;
|
|
|
|
use GraphQL\Language\Parser;
|
|
|
|
use GraphQL\Language\Source;
|
2018-08-07 19:20:39 +03:00
|
|
|
use GraphQL\Language\SourceLocation;
|
2018-07-29 18:43:10 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-02-13 12:37:00 +03:00
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class PrintErrorTest extends TestCase
|
2018-02-13 12:37:00 +03:00
|
|
|
{
|
2018-08-07 19:20:39 +03:00
|
|
|
/**
|
2018-08-28 12:08:43 +03:00
|
|
|
* @see it('prints an line numbers with correct padding')
|
2018-08-07 19:20:39 +03:00
|
|
|
*/
|
2018-08-28 12:08:43 +03:00
|
|
|
public function testPrintsAnLineNumbersWithCorrectPadding() : void
|
2018-08-07 19:20:39 +03:00
|
|
|
{
|
|
|
|
$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);
|
2018-08-07 19:20:39 +03:00
|
|
|
$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
|
2018-08-07 19:20:39 +03:00
|
|
|
|
|
|
|
Test (9:1)
|
|
|
|
9: *
|
|
|
|
^
|
|
|
|
10:
|
|
|
|
';
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
2018-02-13 12:37:00 +03:00
|
|
|
/**
|
2018-08-28 12:08:43 +03:00
|
|
|
* @see it('prints an error with nodes from different sources')
|
2018-02-13 12:37:00 +03:00
|
|
|
*/
|
2018-08-28 12:08:43 +03:00
|
|
|
public function testPrintsAnErrorWithNodesFromDifferentSources() : void
|
2018-02-13 12:37:00 +03:00
|
|
|
{
|
2018-08-28 12:08:43 +03:00
|
|
|
$sourceA = Parser::parse(new Source(
|
|
|
|
'type Foo {
|
2018-02-13 12:37:00 +03:00
|
|
|
field: String
|
|
|
|
}',
|
2018-08-28 12:08:43 +03:00
|
|
|
'SourceA'
|
2018-02-13 12:37:00 +03:00
|
|
|
));
|
|
|
|
|
|
|
|
$fieldTypeA = $sourceA->definitions[0]->fields[0]->type;
|
|
|
|
|
2018-08-28 12:08:43 +03:00
|
|
|
$sourceB = Parser::parse(new Source(
|
|
|
|
'type Foo {
|
2018-02-13 12:37:00 +03:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|