graphql-php/tests/Validator/UniqueOperationNamesTest.php

184 lines
3.6 KiB
PHP
Raw Permalink Normal View History

<?php
2018-09-02 14:08:49 +03:00
declare(strict_types=1);
namespace GraphQL\Tests\Validator;
use GraphQL\Error\FormattedError;
use GraphQL\Language\SourceLocation;
use GraphQL\Validator\Rules\UniqueOperationNames;
2018-07-29 18:43:10 +03:00
class UniqueOperationNamesTest extends ValidatorTestCase
{
// Validate: Unique operation names
/**
* @see it('no operations')
*/
public function testNoOperations() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new UniqueOperationNames(),
'
fragment fragA on Type {
field
}
2018-09-02 14:08:49 +03:00
'
);
}
/**
* @see it('one anon operation')
*/
public function testOneAnonOperation() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new UniqueOperationNames(),
'
{
field
}
2018-09-02 14:08:49 +03:00
'
);
}
/**
* @see it('one named operation')
*/
public function testOneNamedOperation() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new UniqueOperationNames(),
'
query Foo {
field
}
2018-09-02 14:08:49 +03:00
'
);
}
/**
* @see it('multiple operations')
*/
public function testMultipleOperations() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new UniqueOperationNames(),
'
query Foo {
field
}
query Bar {
field
}
2018-09-02 14:08:49 +03:00
'
);
}
/**
* @see it('multiple operations of different types')
*/
public function testMultipleOperationsOfDifferentTypes() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new UniqueOperationNames(),
'
query Foo {
field
}
mutation Bar {
field
}
subscription Baz {
field
}
2018-09-02 14:08:49 +03:00
'
);
}
/**
* @see it('fragment and operation named the same')
*/
public function testFragmentAndOperationNamedTheSame() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new UniqueOperationNames(),
'
query Foo {
...Foo
}
fragment Foo on Type {
field
}
2018-09-02 14:08:49 +03:00
'
);
}
/**
* @see it('multiple operations of same name')
*/
public function testMultipleOperationsOfSameName() : void
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new UniqueOperationNames(),
'
query Foo {
fieldA
}
query Foo {
fieldB
}
2018-09-02 14:08:49 +03:00
',
[$this->duplicateOp('Foo', 2, 13, 5, 13)]
);
}
private function duplicateOp($opName, $l1, $c1, $l2, $c2)
{
return FormattedError::create(
UniqueOperationNames::duplicateOperationNameMessage($opName),
[new SourceLocation($l1, $c1), new SourceLocation($l2, $c2)]
);
}
/**
* @see it('multiple ops of same name of different types (mutation)')
*/
public function testMultipleOpsOfSameNameOfDifferentTypesMutation() : void
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new UniqueOperationNames(),
'
query Foo {
fieldA
}
mutation Foo {
fieldB
}
2018-09-02 14:08:49 +03:00
',
[$this->duplicateOp('Foo', 2, 13, 5, 16)]
);
}
/**
* @see it('multiple ops of same name of different types (subscription)')
*/
public function testMultipleOpsOfSameNameOfDifferentTypesSubscription() : void
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new UniqueOperationNames(),
'
query Foo {
fieldA
}
subscription Foo {
fieldB
}
2018-09-02 14:08:49 +03:00
',
[$this->duplicateOp('Foo', 2, 13, 5, 20)]
);
}
}