graphql-php/tests/Executor/ResolveTest.php

119 lines
3.3 KiB
PHP
Raw Normal View History

<?php
2018-09-01 18:07:06 +03:00
declare(strict_types=1);
namespace GraphQL\Tests\Executor;
use GraphQL\GraphQL;
2018-09-01 18:07:06 +03:00
use GraphQL\Tests\Executor\TestClasses\Adder;
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;
2018-09-01 18:07:06 +03:00
use function json_encode;
use function uniqid;
2018-07-29 18:43:10 +03:00
class ResolveTest extends TestCase
{
// Execute: resolve function
/**
* @see it('default function accesses properties')
*/
public function testDefaultFunctionAccessesProperties() : void
{
$schema = $this->buildSchema(['type' => Type::string()]);
2018-09-01 18:07:06 +03:00
$source = ['test' => 'testValue'];
2018-09-19 18:12:09 +03:00
self::assertEquals(
['data' => ['test' => 'testValue']],
GraphQL::executeQuery($schema, '{ test }', $source)->toArray()
);
}
2018-09-01 18:07:06 +03:00
private function buildSchema($testField)
{
return new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => ['test' => $testField],
]),
]);
}
/**
* @see it('default function calls methods')
*/
public function testDefaultFunctionCallsClosures() : void
{
2018-09-01 18:07:06 +03:00
$schema = $this->buildSchema(['type' => Type::string()]);
$_secret = 'secretValue' . uniqid();
$source = [
2018-09-26 12:07:23 +03:00
'test' => static function () use ($_secret) {
return $_secret;
2018-09-01 18:07:06 +03:00
},
];
2018-09-19 18:12:09 +03:00
self::assertEquals(
['data' => ['test' => $_secret]],
GraphQL::executeQuery($schema, '{ test }', $source)->toArray()
);
}
/**
* @see it('default function passes args and context')
*/
public function testDefaultFunctionPassesArgsAndContext() : void
{
$schema = $this->buildSchema([
'type' => Type::int(),
'args' => [
2018-09-01 18:07:06 +03:00
'addend1' => ['type' => Type::int()],
],
]);
$source = new Adder(700);
$result = GraphQL::executeQuery($schema, '{ test(addend1: 80) }', $source, ['addend2' => 9])->toArray();
2018-09-19 18:12:09 +03:00
self::assertEquals(['data' => ['test' => 789]], $result);
}
/**
* @see it('uses provided resolve function')
*/
public function testUsesProvidedResolveFunction() : void
{
$schema = $this->buildSchema([
2018-09-01 18:07:06 +03:00
'type' => Type::string(),
'args' => [
'aStr' => ['type' => Type::string()],
'aInt' => ['type' => Type::int()],
],
2018-09-26 12:07:23 +03:00
'resolve' => static function ($source, $args) {
return json_encode([$source, $args]);
2018-09-01 18:07:06 +03:00
},
]);
2018-09-19 18:12:09 +03:00
self::assertEquals(
['data' => ['test' => '[null,[]]']],
GraphQL::executeQuery($schema, '{ test }')->toArray()
);
2018-09-19 18:12:09 +03:00
self::assertEquals(
['data' => ['test' => '["Source!",[]]']],
GraphQL::executeQuery($schema, '{ test }', 'Source!')->toArray()
);
2018-09-19 18:12:09 +03:00
self::assertEquals(
['data' => ['test' => '["Source!",{"aStr":"String!"}]']],
GraphQL::executeQuery($schema, '{ test(aStr: "String!") }', 'Source!')->toArray()
);
2018-09-19 18:12:09 +03:00
self::assertEquals(
['data' => ['test' => '["Source!",{"aStr":"String!","aInt":-123}]']],
GraphQL::executeQuery($schema, '{ test(aInt: -123, aStr: "String!") }', 'Source!')->toArray()
);
}
2017-05-08 01:21:20 +03:00
}