2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Executor;
|
|
|
|
|
|
|
|
use GraphQL\Language\Parser;
|
|
|
|
use GraphQL\Schema;
|
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
|
|
|
|
class DirectivesTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
// Execute: handles directives
|
|
|
|
// works without directives
|
|
|
|
public function testWorksWithoutDirectives()
|
|
|
|
{
|
|
|
|
// basic query works
|
|
|
|
$this->assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery('{ a, b }'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorksOnScalars()
|
|
|
|
{
|
|
|
|
// if true includes scalar
|
2015-08-17 17:01:55 +03:00
|
|
|
$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
|
2015-08-17 17:01:55 +03:00
|
|
|
$this->assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery('{ a, b @include(if: false) }'));
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
// unless false includes scalar
|
2015-08-17 17:01:55 +03:00
|
|
|
$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
|
2015-08-17 17:01:55 +03:00
|
|
|
$this->assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery('{ a, b @skip(if: true) }'));
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorksOnFragmentSpreads()
|
|
|
|
{
|
|
|
|
// if false omits fragment spread
|
|
|
|
$q = '
|
|
|
|
query Q {
|
|
|
|
a
|
2015-08-17 17:01:55 +03:00
|
|
|
...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
|
2015-08-17 17:01:55 +03:00
|
|
|
...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
|
2015-08-17 17:01:55 +03:00
|
|
|
...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
|
2015-08-17 17:01:55 +03:00
|
|
|
...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));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorksOnInlineFragment()
|
|
|
|
{
|
|
|
|
// if false omits inline fragment
|
|
|
|
$q = '
|
|
|
|
query Q {
|
|
|
|
a
|
2015-08-17 17:01:55 +03:00
|
|
|
... on TestType @include(if: false) {
|
2015-07-15 20:05:46 +03:00
|
|
|
b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment Frag on TestType {
|
|
|
|
b
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$this->assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery($q));
|
|
|
|
|
|
|
|
// if true includes inline fragment
|
|
|
|
$q = '
|
|
|
|
query Q {
|
|
|
|
a
|
2015-08-17 17:01:55 +03:00
|
|
|
... on TestType @include(if: true) {
|
2015-07-15 20:05:46 +03:00
|
|
|
b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment Frag on TestType {
|
|
|
|
b
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$this->assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery($q));
|
|
|
|
|
|
|
|
// unless false includes inline fragment
|
|
|
|
$q = '
|
|
|
|
query Q {
|
|
|
|
a
|
2015-08-17 17:01:55 +03:00
|
|
|
... on TestType @skip(if: false) {
|
2015-07-15 20:05:46 +03:00
|
|
|
b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment Frag on TestType {
|
|
|
|
b
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$this->assertEquals(['data' => ['a' => 'a', 'b' => 'b']], $this->executeTestQuery($q));
|
|
|
|
|
|
|
|
// unless true includes inline fragment
|
|
|
|
$q = '
|
|
|
|
query Q {
|
|
|
|
a
|
2015-08-17 17:01:55 +03:00
|
|
|
... on TestType @skip(if: true) {
|
2015-07-15 20:05:46 +03:00
|
|
|
b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment Frag on TestType {
|
|
|
|
b
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$this->assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery($q));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorksOnFragment()
|
|
|
|
{
|
|
|
|
// if false omits fragment
|
|
|
|
$q = '
|
|
|
|
query Q {
|
|
|
|
a
|
|
|
|
...Frag
|
|
|
|
}
|
2015-08-17 17:01:55 +03:00
|
|
|
fragment Frag 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 fragment
|
|
|
|
$q = '
|
|
|
|
query Q {
|
|
|
|
a
|
|
|
|
...Frag
|
|
|
|
}
|
2015-08-17 17:01:55 +03:00
|
|
|
fragment Frag 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 fragment
|
|
|
|
$q = '
|
|
|
|
query Q {
|
|
|
|
a
|
|
|
|
...Frag
|
|
|
|
}
|
2015-08-17 17:01:55 +03:00
|
|
|
fragment Frag 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 omits fragment
|
|
|
|
$q = '
|
|
|
|
query Q {
|
|
|
|
a
|
|
|
|
...Frag
|
|
|
|
}
|
2015-08-17 17:01:55 +03:00
|
|
|
fragment Frag on TestType @skip(if: true) {
|
2015-07-15 20:05:46 +03:00
|
|
|
b
|
|
|
|
}
|
|
|
|
';
|
|
|
|
$this->assertEquals(['data' => ['a' => 'a']], $this->executeTestQuery($q));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static $schema;
|
|
|
|
|
|
|
|
private static $data;
|
|
|
|
|
|
|
|
private static function getSchema()
|
|
|
|
{
|
|
|
|
return self::$schema ?: (self::$schema = new Schema(new ObjectType([
|
|
|
|
'name' => 'TestType',
|
|
|
|
'fields' => [
|
|
|
|
'a' => ['type' => Type::string()],
|
|
|
|
'b' => ['type' => Type::string()]
|
|
|
|
]
|
|
|
|
])));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function getData()
|
|
|
|
{
|
|
|
|
return self::$data ?: (self::$data = [
|
2015-08-17 17:01:55 +03:00
|
|
|
'a' => 'a',
|
|
|
|
'b' => 'b'
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function executeTestQuery($doc)
|
|
|
|
{
|
2015-08-17 17:01:55 +03:00
|
|
|
return Executor::execute(self::getSchema(), Parser::parse($doc), self::getData())->toArray();
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
}
|