graphql-php/tests/Executor/DirectivesTest.php

255 lines
6.4 KiB
PHP
Raw Normal View History

2015-07-15 20:05:46 +03:00
<?php
2018-09-01 18:07:06 +03:00
declare(strict_types=1);
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;
2018-09-01 18:07:06 +03:00
use GraphQL\Language\Source;
2015-07-15 20:05:46 +03:00
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
2018-09-01 18:07:06 +03:00
use GraphQL\Type\Schema;
2018-07-29 18:43:10 +03:00
use PHPUnit\Framework\TestCase;
2015-07-15 20:05:46 +03:00
2018-09-01 18:07:06 +03:00
/**
* Describe: Execute: handles directives
*/
2018-07-29 18:43:10 +03:00
class DirectivesTest extends TestCase
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
/** @var Schema */
private static $schema;
/** @var string[] */
private static $data;
/**
* @see it('basic query works')
*/
public function testWorksWithoutDirectives() : void
2015-07-15 20:05:46 +03:00
{
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery('{ a, b }'));
2015-07-15 20:05:46 +03:00
}
2018-09-01 18:07:06 +03:00
/**
* @param Source|string $doc
* @return mixed[]
*/
private function executeTestQuery($doc) : array
{
return Executor::execute(self::getSchema(), Parser::parse($doc), self::getData())->toArray();
}
private static function getSchema() : Schema
{
if (! self::$schema) {
self::$schema = new Schema([
'query' => new ObjectType([
'name' => 'TestType',
'fields' => [
'a' => ['type' => Type::string()],
'b' => ['type' => Type::string()],
],
]),
]);
}
return self::$schema;
}
/**
* @return string[]
*/
private static function getData() : array
{
return self::$data ?: (self::$data = [
'a' => 'a',
'b' => 'b',
]);
}
public function testWorksOnScalars() : void
2015-07-15 20:05:46 +03:00
{
// if true includes scalar
2018-09-19 18:12:09 +03:00
self::assertEquals(
2018-09-01 18:07:06 +03:00
['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
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery('{ a, b @include(if: false) }'));
2015-07-15 20:05:46 +03:00
// unless false includes scalar
2018-09-19 18:12:09 +03:00
self::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
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery('{ a, b @skip(if: true) }'));
2015-07-15 20:05:46 +03:00
}
public function testWorksOnFragmentSpreads() : void
2015-07-15 20:05:46 +03:00
{
// 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
}
';
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery($q));
2015-07-15 20:05:46 +03:00
// 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
}
';
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery($q));
2015-07-15 20:05:46 +03:00
// 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
}
';
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery($q));
2015-07-15 20:05:46 +03:00
// 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
}
';
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery($q));
2015-07-15 20:05:46 +03:00
}
public function testWorksOnInlineFragment() : void
2015-07-15 20:05:46 +03:00
{
// if false omits inline fragment
$q = '
query Q {
a
... on TestType @include(if: false) {
2015-07-15 20:05:46 +03:00
b
}
}
';
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery($q));
2015-07-15 20:05:46 +03:00
// if true includes inline fragment
$q = '
query Q {
a
... on TestType @include(if: true) {
2015-07-15 20:05:46 +03:00
b
}
}
';
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery($q));
2015-07-15 20:05:46 +03:00
// unless false includes inline fragment
$q = '
query Q {
a
... on TestType @skip(if: false) {
2015-07-15 20:05:46 +03:00
b
}
}
';
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery($q));
2015-07-15 20:05:46 +03:00
// unless true includes inline fragment
$q = '
query Q {
a
... on TestType @skip(if: true) {
2015-07-15 20:05:46 +03:00
b
}
}
';
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery($q));
2015-07-15 20:05:46 +03:00
}
public function testWorksOnAnonymousInlineFragment() : void
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
}
';
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery($q));
2015-07-15 20:05:46 +03:00
// 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
}
';
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery($q));
2015-07-15 20:05:46 +03:00
// 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
}
';
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery($q));
2015-07-15 20:05:46 +03:00
// 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
}
';
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery($q));
2015-07-15 20:05:46 +03:00
}
public function testWorksWithSkipAndIncludeDirectives() : void
{
// include and no skip
2018-09-19 18:12:09 +03:00
self::assertEquals(
['data' => ['a' => 'a', 'b' => 'b']],
$this->executeTestQuery('{ a, b @include(if: true) @skip(if: false) }')
);
// include and skip
2018-09-19 18:12:09 +03:00
self::assertEquals(
['data' => ['a' => 'a']],
$this->executeTestQuery('{ a, b @include(if: true) @skip(if: true) }')
);
// no include or skip
2018-09-19 18:12:09 +03:00
self::assertEquals(
['data' => ['a' => 'a']],
$this->executeTestQuery('{ a, b @include(if: false) @skip(if: false) }')
);
}
2015-07-15 20:05:46 +03:00
}