graphql-php/tests/Validator/ScalarLeafsTest.php

172 lines
4.3 KiB
PHP
Raw Normal View History

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
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
/**
* @see it('valid scalar selection')
*/
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
}
/**
* @see it('object type missing selection')
*/
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
}
/**
* @see it('interface type missing selection')
*/
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
}
/**
* @see it('valid scalar selection with args')
*/
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
}
/**
* @see it('scalar selection not allowed on Boolean')
*/
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
}
/**
* @see it('scalar selection not allowed on Enum')
*/
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
);
}
/**
* @see it('scalar selection not allowed with args')
*/
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
);
}
/**
* @see it('Scalar selection not allowed with directives')
*/
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 {
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
);
}
/**
* @see it('Scalar selection not allowed with directives and args')
*/
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 {
doesKnowCommand(dogCommand: SIT) @include(if: true) { sinceWhen }
2015-07-15 20:05:46 +03:00
}
',
[$this->noScalarSubselection('doesKnowCommand', 'Boolean', 3, 61)]
2015-07-15 20:05:46 +03:00
);
}
}