graphql-php/tests/Server/ServerConfigTest.php

233 lines
7.6 KiB
PHP
Raw Normal View History

<?php
2018-09-01 23:00:00 +03:00
declare(strict_types=1);
namespace GraphQL\Tests\Server;
use GraphQL\Error\InvariantViolation;
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
use GraphQL\Server\ServerConfig;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
2018-09-01 23:00:00 +03:00
use GraphQL\Type\Schema;
2018-07-29 18:43:10 +03:00
use PHPUnit\Framework\TestCase;
2018-09-26 12:07:23 +03:00
use stdClass;
2018-07-29 18:43:10 +03:00
class ServerConfigTest extends TestCase
{
public function testDefaults() : void
{
$config = ServerConfig::create();
2018-09-19 18:12:09 +03:00
self::assertNull($config->getSchema());
self::assertNull($config->getContext());
self::assertNull($config->getRootValue());
self::assertNull($config->getErrorFormatter());
self::assertNull($config->getErrorsHandler());
self::assertNull($config->getPromiseAdapter());
self::assertNull($config->getValidationRules());
self::assertNull($config->getFieldResolver());
self::assertNull($config->getPersistentQueryLoader());
self::assertFalse($config->getDebug());
self::assertFalse($config->getQueryBatching());
}
public function testAllowsSettingSchema() : void
{
$schema = new Schema(['query' => new ObjectType(['name' => 'a', 'fields' => []])]);
$config = ServerConfig::create()
->setSchema($schema);
2018-09-19 18:12:09 +03:00
self::assertSame($schema, $config->getSchema());
$schema2 = new Schema(['query' => new ObjectType(['name' => 'a', 'fields' => []])]);
$config->setSchema($schema2);
2018-09-19 18:12:09 +03:00
self::assertSame($schema2, $config->getSchema());
}
public function testAllowsSettingContext() : void
{
$config = ServerConfig::create();
$context = [];
$config->setContext($context);
2018-09-19 18:12:09 +03:00
self::assertSame($context, $config->getContext());
2018-09-26 12:07:23 +03:00
$context2 = new stdClass();
$config->setContext($context2);
2018-09-19 18:12:09 +03:00
self::assertSame($context2, $config->getContext());
}
public function testAllowsSettingRootValue() : void
{
$config = ServerConfig::create();
$rootValue = [];
$config->setRootValue($rootValue);
2018-09-19 18:12:09 +03:00
self::assertSame($rootValue, $config->getRootValue());
2018-09-26 12:07:23 +03:00
$context2 = new stdClass();
$config->setRootValue($context2);
2018-09-19 18:12:09 +03:00
self::assertSame($context2, $config->getRootValue());
}
public function testAllowsSettingErrorFormatter() : void
{
$config = ServerConfig::create();
2018-09-26 12:07:23 +03:00
$formatter = static function () {
2018-09-01 23:00:00 +03:00
};
$config->setErrorFormatter($formatter);
2018-09-19 18:12:09 +03:00
self::assertSame($formatter, $config->getErrorFormatter());
$formatter = 'date'; // test for callable
$config->setErrorFormatter($formatter);
2018-09-19 18:12:09 +03:00
self::assertSame($formatter, $config->getErrorFormatter());
}
public function testAllowsSettingErrorsHandler() : void
{
$config = ServerConfig::create();
2018-09-26 12:07:23 +03:00
$handler = static function () {
2018-09-01 23:00:00 +03:00
};
$config->setErrorsHandler($handler);
2018-09-19 18:12:09 +03:00
self::assertSame($handler, $config->getErrorsHandler());
$handler = 'date'; // test for callable
$config->setErrorsHandler($handler);
2018-09-19 18:12:09 +03:00
self::assertSame($handler, $config->getErrorsHandler());
}
public function testAllowsSettingPromiseAdapter() : void
{
$config = ServerConfig::create();
$adapter1 = new SyncPromiseAdapter();
$config->setPromiseAdapter($adapter1);
2018-09-19 18:12:09 +03:00
self::assertSame($adapter1, $config->getPromiseAdapter());
$adapter2 = new SyncPromiseAdapter();
$config->setPromiseAdapter($adapter2);
2018-09-19 18:12:09 +03:00
self::assertSame($adapter2, $config->getPromiseAdapter());
}
public function testAllowsSettingValidationRules() : void
{
$config = ServerConfig::create();
$rules = [];
$config->setValidationRules($rules);
2018-09-19 18:12:09 +03:00
self::assertSame($rules, $config->getValidationRules());
2018-09-26 12:07:23 +03:00
$rules = [static function () {
2018-09-01 23:00:00 +03:00
},
];
$config->setValidationRules($rules);
2018-09-19 18:12:09 +03:00
self::assertSame($rules, $config->getValidationRules());
2018-09-26 12:07:23 +03:00
$rules = static function () {
return [static function () {
2018-09-01 23:00:00 +03:00
},
];
};
$config->setValidationRules($rules);
2018-09-19 18:12:09 +03:00
self::assertSame($rules, $config->getValidationRules());
}
public function testAllowsSettingDefaultFieldResolver() : void
{
$config = ServerConfig::create();
2018-09-26 12:07:23 +03:00
$resolver = static function () {
2018-09-01 23:00:00 +03:00
};
2017-08-17 22:54:35 +03:00
$config->setFieldResolver($resolver);
2018-09-19 18:12:09 +03:00
self::assertSame($resolver, $config->getFieldResolver());
$resolver = 'date'; // test for callable
2017-08-17 22:54:35 +03:00
$config->setFieldResolver($resolver);
2018-09-19 18:12:09 +03:00
self::assertSame($resolver, $config->getFieldResolver());
}
public function testAllowsSettingPersistedQueryLoader() : void
{
$config = ServerConfig::create();
2018-09-26 12:07:23 +03:00
$loader = static function () {
2018-09-01 23:00:00 +03:00
};
$config->setPersistentQueryLoader($loader);
2018-09-19 18:12:09 +03:00
self::assertSame($loader, $config->getPersistentQueryLoader());
$loader = 'date'; // test for callable
$config->setPersistentQueryLoader($loader);
2018-09-19 18:12:09 +03:00
self::assertSame($loader, $config->getPersistentQueryLoader());
}
public function testAllowsSettingCatchPhpErrors() : void
{
$config = ServerConfig::create();
$config->setDebug(true);
2018-09-19 18:12:09 +03:00
self::assertTrue($config->getDebug());
$config->setDebug(false);
2018-09-19 18:12:09 +03:00
self::assertFalse($config->getDebug());
}
public function testAcceptsArray() : void
{
$arr = [
2018-09-01 23:00:00 +03:00
'schema' => new Schema([
'query' => new ObjectType(['name' => 't', 'fields' => ['a' => Type::string()]]),
]),
2018-09-26 12:07:23 +03:00
'context' => new stdClass(),
'rootValue' => new stdClass(),
'errorFormatter' => static function () {
2018-09-01 23:00:00 +03:00
},
'promiseAdapter' => new SyncPromiseAdapter(),
2018-09-26 12:07:23 +03:00
'validationRules' => [static function () {
2018-09-01 23:00:00 +03:00
},
],
2018-09-26 12:07:23 +03:00
'fieldResolver' => static function () {
2018-09-01 23:00:00 +03:00
},
2018-09-26 12:07:23 +03:00
'persistentQueryLoader' => static function () {
2018-09-01 23:00:00 +03:00
},
'debug' => true,
'queryBatching' => true,
];
$config = ServerConfig::create($arr);
2018-09-19 18:12:09 +03:00
self::assertSame($arr['schema'], $config->getSchema());
self::assertSame($arr['context'], $config->getContext());
self::assertSame($arr['rootValue'], $config->getRootValue());
self::assertSame($arr['errorFormatter'], $config->getErrorFormatter());
self::assertSame($arr['promiseAdapter'], $config->getPromiseAdapter());
self::assertSame($arr['validationRules'], $config->getValidationRules());
self::assertSame($arr['fieldResolver'], $config->getFieldResolver());
self::assertSame($arr['persistentQueryLoader'], $config->getPersistentQueryLoader());
self::assertTrue($config->getDebug());
self::assertTrue($config->getQueryBatching());
}
public function testThrowsOnInvalidArrayKey() : void
{
2018-09-01 23:00:00 +03:00
$arr = ['missingKey' => 'value'];
2018-07-29 18:43:10 +03:00
$this->expectException(InvariantViolation::class);
$this->expectExceptionMessage('Unknown server config option "missingKey"');
ServerConfig::create($arr);
}
public function testInvalidValidationRules() : void
{
2018-09-26 12:07:23 +03:00
$rules = new stdClass();
$config = ServerConfig::create();
2018-07-29 18:43:10 +03:00
$this->expectException(InvariantViolation::class);
$this->expectExceptionMessage('Server config expects array of validation rules or callable returning such array, but got instance of stdClass');
$config->setValidationRules($rules);
}
}