graphql-php/tests/Validator/LoneAnonymousOperationTest.php

153 lines
2.9 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\LoneAnonymousOperation;
2018-07-29 18:43:10 +03:00
class LoneAnonymousOperationTest extends ValidatorTestCase
{
// Validate: Anonymous operation must be alone
/**
* @see it('no operations')
*/
public function testNoOperations() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new LoneAnonymousOperation(),
'
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 LoneAnonymousOperation(),
'
{
field
}
2018-09-02 14:08:49 +03:00
'
);
}
/**
* @see it('multiple named operations')
*/
public function testMultipleNamedOperations() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new LoneAnonymousOperation(),
'
query Foo {
field
}
query Bar {
field
}
2018-09-02 14:08:49 +03:00
'
);
}
/**
* @see it('anon operation with fragment')
*/
public function testAnonOperationWithFragment() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new LoneAnonymousOperation(),
'
{
...Foo
}
fragment Foo on Type {
field
}
2018-09-02 14:08:49 +03:00
'
);
}
/**
* @see it('multiple anon operations')
*/
public function testMultipleAnonOperations() : void
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new LoneAnonymousOperation(),
'
{
fieldA
}
{
fieldB
}
2018-09-02 14:08:49 +03:00
',
[
$this->anonNotAlone(2, 7),
$this->anonNotAlone(5, 7),
]
);
}
private function anonNotAlone($line, $column)
{
return FormattedError::create(
LoneAnonymousOperation::anonOperationNotAloneMessage(),
[new SourceLocation($line, $column)]
);
}
/**
* @see it('anon operation with a mutation')
*/
public function testAnonOperationWithMutation() : void
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new LoneAnonymousOperation(),
'
{
fieldA
}
mutation Foo {
fieldB
}
2018-09-02 14:08:49 +03:00
',
[
$this->anonNotAlone(2, 7),
]
);
}
/**
* @see it('anon operation with a subscription')
*/
public function testAnonOperationWithSubscription() : void
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new LoneAnonymousOperation(),
'
{
fieldA
}
subscription Foo {
fieldB
}
2018-09-02 14:08:49 +03:00
',
[
$this->anonNotAlone(2, 7),
]
);
}
2018-07-29 18:43:10 +03:00
}