graphql-php/tests/Executor/DirectivesTest.php

256 lines
6.5 KiB
PHP
Raw Normal View History

2015-07-15 20:05:46 +03:00
<?php
2016-04-09 10:36:53 +03:00
namespace GraphQL\Tests\Executor;
2015-07-15 20:05:46 +03:00
2016-04-09 10:36:53 +03:00
use GraphQL\Executor\Executor;
2015-07-15 20:05:46 +03:00
use GraphQL\Language\Parser;
use GraphQL\Type\Schema;
2015-07-15 20:05:46 +03:00
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
2018-07-29 18:43:10 +03:00
use PHPUnit\Framework\TestCase;
2015-07-15 20:05:46 +03:00
2018-07-29 18:43:10 +03:00
class DirectivesTest extends TestCase
2015-07-15 20:05:46 +03:00
{
// Describe: Execute: handles directives
/**
* @describe works without directives
* @see it('basic query works')
*/
2015-07-15 20:05:46 +03:00
public function testWorksWithoutDirectives()
{
$this->assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery('{ a, b }'));
}
/**
* @describe works on scalars
*/
2015-07-15 20:05:46 +03:00
public function testWorksOnScalars()
{
// if true includes scalar
$this->assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery('{ a, b @include(if: true) }'));
2015-07-15 20:05:46 +03:00
// if false omits on scalar
$this->assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery('{ a, b @include(if: false) }'));
2015-07-15 20:05:46 +03:00
// unless false includes scalar
$this->assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery('{ a, b @skip(if: false) }'));
2015-07-15 20:05:46 +03:00
// unless true omits scalar
$this->assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery('{ a, b @skip(if: true) }'));
2015-07-15 20:05:46 +03:00
}
/**
* @describe works on fragment spreads
*/
2015-07-15 20:05:46 +03:00
public function testWorksOnFragmentSpreads()
{
// if false omits fragment spread
$q = '
query Q {
a
...Frag @include(if: false)
2015-07-15 20:05:46 +03:00
}
fragment Frag on TestType {
b
}
';
$this->assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery($q));
// if true includes fragment spread
$q = '
query Q {
a
...Frag @include(if: true)
2015-07-15 20:05:46 +03:00
}
fragment Frag on TestType {
b
}
';
$this->assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery($q));
// unless false includes fragment spread
$q = '
query Q {
a
...Frag @skip(if: false)
2015-07-15 20:05:46 +03:00
}
fragment Frag on TestType {
b
}
';
$this->assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery($q));
// unless true omits fragment spread
$q = '
query Q {
a
...Frag @skip(if: true)
2015-07-15 20:05:46 +03:00
}
fragment Frag on TestType {
b
}
';
$this->assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery($q));
}
/**
* @describe works on inline fragment
*/
2015-07-15 20:05:46 +03:00
public function testWorksOnInlineFragment()
{
// if false omits inline fragment
$q = '
query Q {
a
... on TestType @include(if: false) {
2015-07-15 20:05:46 +03:00
b
}
}
';
$this->assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery($q));
// if true includes inline fragment
$q = '
query Q {
a
... on TestType @include(if: true) {
2015-07-15 20:05:46 +03:00
b
}
}
';
$this->assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery($q));
// unless false includes inline fragment
$q = '
query Q {
a
... on TestType @skip(if: false) {
2015-07-15 20:05:46 +03:00
b
}
}
';
$this->assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery($q));
// unless true includes inline fragment
$q = '
query Q {
a
... on TestType @skip(if: true) {
2015-07-15 20:05:46 +03:00
b
}
}
';
$this->assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery($q));
}
/**
* @describe works on anonymous inline fragment
*/
public function testWorksOnAnonymousInlineFragment()
2015-07-15 20:05:46 +03:00
{
// if false omits anonymous inline fragment
2015-07-15 20:05:46 +03:00
$q = '
query Q {
a
... @include(if: false) {
b
}
2015-07-15 20:05:46 +03:00
}
';
$this->assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery($q));
// if true includes anonymous inline fragment
2015-07-15 20:05:46 +03:00
$q = '
query Q {
a
... @include(if: true) {
b
}
2015-07-15 20:05:46 +03:00
}
';
$this->assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery($q));
// unless false includes anonymous inline fragment
2015-07-15 20:05:46 +03:00
$q = '
query Q {
a
... @skip(if: false) {
b
}
2015-07-15 20:05:46 +03:00
}
';
$this->assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery($q));
// unless true includes anonymous inline fragment
2015-07-15 20:05:46 +03:00
$q = '
query Q {
a
... @skip(if: true) {
b
}
2015-07-15 20:05:46 +03:00
}
';
$this->assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery($q));
}
/**
* @describe works with skip and include directives
*/
public function testWorksWithSkipAndIncludeDirectives()
{
// include and no skip
$this->assertEquals(
['data' => ['a' => 'a', 'b' => 'b']],
$this->executeTestQuery('{ a, b @include(if: true) @skip(if: false) }')
);
// include and skip
$this->assertEquals(
['data' => ['a' => 'a']],
$this->executeTestQuery('{ a, b @include(if: true) @skip(if: true) }')
);
// no include or skip
$this->assertEquals(
['data' => ['a' => 'a']],
$this->executeTestQuery('{ a, b @include(if: false) @skip(if: false) }')
);
}
2015-07-15 20:05:46 +03:00
private static $schema;
private static $data;
private static function getSchema()
{
if (!self::$schema) {
self::$schema = new Schema([
'query' => new ObjectType([
'name' => 'TestType',
'fields' => [
'a' => ['type' => Type::string()],
'b' => ['type' => Type::string()]
]
])
]);
}
return self::$schema;
2015-07-15 20:05:46 +03:00
}
private static function getData()
{
return self::$data ?: (self::$data = [
'a' => 'a',
'b' => 'b'
2015-07-15 20:05:46 +03:00
]);
}
private function executeTestQuery($doc)
{
return Executor::execute(self::getSchema(), Parser::parse($doc), self::getData())->toArray();
2015-07-15 20:05:46 +03:00
}
}