2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
2018-09-02 14:08:49 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
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;
|
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class ScalarLeafsTest extends ValidatorTestCase
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
// Validate: Scalar leafs
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('valid scalar selection')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testValidScalarSelection() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new ScalarLeafs(),
|
|
|
|
'
|
2015-07-15 20:05:46 +03:00
|
|
|
fragment scalarSelection on Dog {
|
|
|
|
barks
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('object type missing selection')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testObjectTypeMissingSelection() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new ScalarLeafs(),
|
|
|
|
'
|
2015-07-15 20:05:46 +03:00
|
|
|
query directQueryOnObjectWithoutSubFields {
|
|
|
|
human
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
',
|
|
|
|
[$this->missingObjSubselection('human', 'Human', 3, 9)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function missingObjSubselection($field, $type, $line, $column)
|
|
|
|
{
|
|
|
|
return FormattedError::create(
|
|
|
|
ScalarLeafs::requiredSubselectionMessage($field, $type),
|
|
|
|
[new SourceLocation($line, $column)]
|
|
|
|
);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('interface type missing selection')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testInterfaceTypeMissingSelection() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new ScalarLeafs(),
|
|
|
|
'
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
human { pets }
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
',
|
|
|
|
[$this->missingObjSubselection('pets', '[Pet]', 3, 17)]
|
|
|
|
);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('valid scalar selection with args')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testValidScalarSelectionWithArgs() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new ScalarLeafs(),
|
|
|
|
'
|
2015-07-15 20:05:46 +03:00
|
|
|
fragment scalarSelectionWithArgs on Dog {
|
|
|
|
doesKnowCommand(dogCommand: SIT)
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('scalar selection not allowed on Boolean')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testScalarSelectionNotAllowedOnBoolean() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new ScalarLeafs(),
|
|
|
|
'
|
2015-07-15 20:05:46 +03:00
|
|
|
fragment scalarSelectionsNotAllowedOnBoolean on Dog {
|
|
|
|
barks { sinceWhen }
|
|
|
|
}
|
|
|
|
',
|
2018-09-02 14:08:49 +03:00
|
|
|
[$this->noScalarSubselection('barks', 'Boolean', 3, 15)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function noScalarSubselection($field, $type, $line, $column)
|
|
|
|
{
|
|
|
|
return FormattedError::create(
|
|
|
|
ScalarLeafs::noSubselectionAllowedMessage($field, $type),
|
|
|
|
[new SourceLocation($line, $column)]
|
|
|
|
);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('scalar selection not allowed on Enum')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testScalarSelectionNotAllowedOnEnum() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new ScalarLeafs(),
|
|
|
|
'
|
2015-07-15 20:05:46 +03:00
|
|
|
fragment scalarSelectionsNotAllowedOnEnum on Cat {
|
|
|
|
furColor { inHexdec }
|
|
|
|
}
|
|
|
|
',
|
2018-09-02 14:08:49 +03:00
|
|
|
[$this->noScalarSubselection('furColor', 'FurColor', 3, 18)]
|
2015-07-15 20:05:46 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('scalar selection not allowed with args')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testScalarSelectionNotAllowedWithArgs() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new ScalarLeafs(),
|
|
|
|
'
|
2015-07-15 20:05:46 +03:00
|
|
|
fragment scalarSelectionsNotAllowedWithArgs on Dog {
|
|
|
|
doesKnowCommand(dogCommand: SIT) { sinceWhen }
|
|
|
|
}
|
|
|
|
',
|
2018-09-02 14:08:49 +03:00
|
|
|
[$this->noScalarSubselection('doesKnowCommand', 'Boolean', 3, 42)]
|
2015-07-15 20:05:46 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Scalar selection not allowed with directives')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testScalarSelectionNotAllowedWithDirectives() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new ScalarLeafs(),
|
|
|
|
'
|
2015-07-15 20:05:46 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
',
|
2018-09-02 14:08:49 +03:00
|
|
|
[$this->noScalarSubselection('name', 'String', 3, 33)]
|
2015-07-15 20:05:46 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Scalar selection not allowed with directives and args')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testScalarSelectionNotAllowedWithDirectivesAndArgs() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new ScalarLeafs(),
|
|
|
|
'
|
2015-07-15 20:05:46 +03:00
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|