2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
2016-04-09 10:36:53 +03:00
|
|
|
namespace GraphQL\Tests\Validator;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2016-10-21 12:39:57 +03:00
|
|
|
use GraphQL\Error\FormattedError;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Language\SourceLocation;
|
|
|
|
use GraphQL\Validator\Rules\ScalarLeafs;
|
|
|
|
|
|
|
|
class ScalarLeafsTest extends TestCase
|
|
|
|
{
|
|
|
|
// Validate: Scalar leafs
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it valid scalar selection
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testValidScalarSelection()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new ScalarLeafs, '
|
|
|
|
fragment scalarSelection on Dog {
|
|
|
|
barks
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it object type missing selection
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testObjectTypeMissingSelection()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new ScalarLeafs, '
|
|
|
|
query directQueryOnObjectWithoutSubFields {
|
|
|
|
human
|
|
|
|
}
|
|
|
|
', [$this->missingObjSubselection('human', 'Human', 3, 9)]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it interface type missing selection
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testInterfaceTypeMissingSelection()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new ScalarLeafs, '
|
|
|
|
{
|
|
|
|
human { pets }
|
|
|
|
}
|
|
|
|
', [$this->missingObjSubselection('pets', '[Pet]', 3, 17)]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it valid scalar selection with args
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testValidScalarSelectionWithArgs()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new ScalarLeafs, '
|
|
|
|
fragment scalarSelectionWithArgs on Dog {
|
|
|
|
doesKnowCommand(dogCommand: SIT)
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it scalar selection not allowed on Boolean
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testScalarSelectionNotAllowedOnBoolean()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new ScalarLeafs, '
|
|
|
|
fragment scalarSelectionsNotAllowedOnBoolean on Dog {
|
|
|
|
barks { sinceWhen }
|
|
|
|
}
|
|
|
|
',
|
|
|
|
[$this->noScalarSubselection('barks', 'Boolean', 3, 15)]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it scalar selection not allowed on Enum
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testScalarSelectionNotAllowedOnEnum()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new ScalarLeafs, '
|
|
|
|
fragment scalarSelectionsNotAllowedOnEnum on Cat {
|
|
|
|
furColor { inHexdec }
|
|
|
|
}
|
|
|
|
',
|
|
|
|
[$this->noScalarSubselection('furColor', 'FurColor', 3, 18)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it scalar selection not allowed with args
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testScalarSelectionNotAllowedWithArgs()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new ScalarLeafs, '
|
|
|
|
fragment scalarSelectionsNotAllowedWithArgs on Dog {
|
|
|
|
doesKnowCommand(dogCommand: SIT) { sinceWhen }
|
|
|
|
}
|
|
|
|
',
|
|
|
|
[$this->noScalarSubselection('doesKnowCommand', 'Boolean', 3, 42)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it Scalar selection not allowed with directives
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testScalarSelectionNotAllowedWithDirectives()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new ScalarLeafs, '
|
|
|
|
fragment scalarSelectionsNotAllowedWithDirectives on Dog {
|
2015-08-17 17:01:55 +03:00
|
|
|
name @include(if: true) { isAlsoHumanName }
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
',
|
2015-08-17 17:01:55 +03:00
|
|
|
[$this->noScalarSubselection('name', 'String', 3, 33)]
|
2015-07-15 20:05:46 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it Scalar selection not allowed with directives and args
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testScalarSelectionNotAllowedWithDirectivesAndArgs()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new ScalarLeafs, '
|
|
|
|
fragment scalarSelectionsNotAllowedWithDirectivesAndArgs on Dog {
|
2015-08-17 17:01:55 +03:00
|
|
|
doesKnowCommand(dogCommand: SIT) @include(if: true) { sinceWhen }
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
',
|
2015-08-17 17:01:55 +03:00
|
|
|
[$this->noScalarSubselection('doesKnowCommand', 'Boolean', 3, 61)]
|
2015-07-15 20:05:46 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function noScalarSubselection($field, $type, $line, $column)
|
|
|
|
{
|
2015-08-17 17:01:55 +03:00
|
|
|
return FormattedError::create(
|
|
|
|
ScalarLeafs::noSubselectionAllowedMessage($field, $type),
|
2015-07-15 20:05:46 +03:00
|
|
|
[new SourceLocation($line, $column)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function missingObjSubselection($field, $type, $line, $column)
|
|
|
|
{
|
2015-08-17 17:01:55 +03:00
|
|
|
return FormattedError::create(
|
|
|
|
ScalarLeafs::requiredSubselectionMessage($field, $type),
|
2015-07-15 20:05:46 +03:00
|
|
|
[new SourceLocation($line, $column)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|