graphql-php/tests/Server/ServerConfigTest.php

232 lines
7.7 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-07-29 18:43:10 +03:00
class ServerConfigTest extends TestCase
{
public function testDefaults() : void
{
$config = ServerConfig::create();
$this->assertEquals(null, $config->getSchema());
$this->assertEquals(null, $config->getContext());
$this->assertEquals(null, $config->getRootValue());
$this->assertEquals(null, $config->getErrorFormatter());
$this->assertEquals(null, $config->getErrorsHandler());
$this->assertEquals(null, $config->getPromiseAdapter());
$this->assertEquals(null, $config->getValidationRules());
2017-08-17 22:54:35 +03:00
$this->assertEquals(null, $config->getFieldResolver());
$this->assertEquals(null, $config->getPersistentQueryLoader());
$this->assertEquals(false, $config->getDebug());
$this->assertEquals(false, $config->getQueryBatching());
}
public function testAllowsSettingSchema() : void
{
$schema = new Schema(['query' => new ObjectType(['name' => 'a', 'fields' => []])]);
$config = ServerConfig::create()
->setSchema($schema);
$this->assertSame($schema, $config->getSchema());
$schema2 = new Schema(['query' => new ObjectType(['name' => 'a', 'fields' => []])]);
$config->setSchema($schema2);
$this->assertSame($schema2, $config->getSchema());
}
public function testAllowsSettingContext() : void
{
$config = ServerConfig::create();
$context = [];
$config->setContext($context);
$this->assertSame($context, $config->getContext());
$context2 = new \stdClass();
$config->setContext($context2);
$this->assertSame($context2, $config->getContext());
}
public function testAllowsSettingRootValue() : void
{
$config = ServerConfig::create();
$rootValue = [];
$config->setRootValue($rootValue);
$this->assertSame($rootValue, $config->getRootValue());
$context2 = new \stdClass();
$config->setRootValue($context2);
$this->assertSame($context2, $config->getRootValue());
}
public function testAllowsSettingErrorFormatter() : void
{
$config = ServerConfig::create();
2018-09-01 23:00:00 +03:00
$formatter = function () {
};
$config->setErrorFormatter($formatter);
$this->assertSame($formatter, $config->getErrorFormatter());
$formatter = 'date'; // test for callable
$config->setErrorFormatter($formatter);
$this->assertSame($formatter, $config->getErrorFormatter());
}
public function testAllowsSettingErrorsHandler() : void
{
$config = ServerConfig::create();
2018-09-01 23:00:00 +03:00
$handler = function () {
};
$config->setErrorsHandler($handler);
$this->assertSame($handler, $config->getErrorsHandler());
$handler = 'date'; // test for callable
$config->setErrorsHandler($handler);
$this->assertSame($handler, $config->getErrorsHandler());
}
public function testAllowsSettingPromiseAdapter() : void
{
$config = ServerConfig::create();
$adapter1 = new SyncPromiseAdapter();
$config->setPromiseAdapter($adapter1);
$this->assertSame($adapter1, $config->getPromiseAdapter());
$adapter2 = new SyncPromiseAdapter();
$config->setPromiseAdapter($adapter2);
$this->assertSame($adapter2, $config->getPromiseAdapter());
}
public function testAllowsSettingValidationRules() : void
{
$config = ServerConfig::create();
$rules = [];
$config->setValidationRules($rules);
$this->assertSame($rules, $config->getValidationRules());
2018-09-01 23:00:00 +03:00
$rules = [function () {
},
];
$config->setValidationRules($rules);
$this->assertSame($rules, $config->getValidationRules());
2018-09-01 23:00:00 +03:00
$rules = function () {
return [function () {
},
];
};
$config->setValidationRules($rules);
$this->assertSame($rules, $config->getValidationRules());
}
public function testAllowsSettingDefaultFieldResolver() : void
{
$config = ServerConfig::create();
2018-09-01 23:00:00 +03:00
$resolver = function () {
};
2017-08-17 22:54:35 +03:00
$config->setFieldResolver($resolver);
$this->assertSame($resolver, $config->getFieldResolver());
$resolver = 'date'; // test for callable
2017-08-17 22:54:35 +03:00
$config->setFieldResolver($resolver);
$this->assertSame($resolver, $config->getFieldResolver());
}
public function testAllowsSettingPersistedQueryLoader() : void
{
$config = ServerConfig::create();
2018-09-01 23:00:00 +03:00
$loader = function () {
};
$config->setPersistentQueryLoader($loader);
$this->assertSame($loader, $config->getPersistentQueryLoader());
$loader = 'date'; // test for callable
$config->setPersistentQueryLoader($loader);
$this->assertSame($loader, $config->getPersistentQueryLoader());
}
public function testAllowsSettingCatchPhpErrors() : void
{
$config = ServerConfig::create();
$config->setDebug(true);
$this->assertSame(true, $config->getDebug());
$config->setDebug(false);
$this->assertSame(false, $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-01 23:00:00 +03:00
'context' => new \stdClass(),
'rootValue' => new \stdClass(),
'errorFormatter' => function () {
},
'promiseAdapter' => new SyncPromiseAdapter(),
'validationRules' => [function () {
},
],
'fieldResolver' => function () {
},
'persistentQueryLoader' => function () {
},
'debug' => true,
'queryBatching' => true,
];
$config = ServerConfig::create($arr);
$this->assertSame($arr['schema'], $config->getSchema());
$this->assertSame($arr['context'], $config->getContext());
$this->assertSame($arr['rootValue'], $config->getRootValue());
$this->assertSame($arr['errorFormatter'], $config->getErrorFormatter());
$this->assertSame($arr['promiseAdapter'], $config->getPromiseAdapter());
$this->assertSame($arr['validationRules'], $config->getValidationRules());
2017-08-17 22:54:35 +03:00
$this->assertSame($arr['fieldResolver'], $config->getFieldResolver());
$this->assertSame($arr['persistentQueryLoader'], $config->getPersistentQueryLoader());
$this->assertSame(true, $config->getDebug());
$this->assertSame(true, $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-01 23:00:00 +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);
}
}