2017-07-14 15:08:47 +03:00
|
|
|
<?php
|
2018-09-01 23:00:00 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-07-14 15:08:47 +03:00
|
|
|
namespace GraphQL\Tests\Server;
|
|
|
|
|
2017-08-15 14:05:09 +03:00
|
|
|
use GraphQL\Error\InvariantViolation;
|
2017-07-14 15:08:47 +03:00
|
|
|
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
|
|
|
|
use GraphQL\Server\ServerConfig;
|
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
2017-08-15 14:05:09 +03:00
|
|
|
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;
|
2017-07-14 15:08:47 +03:00
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class ServerConfigTest extends TestCase
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testDefaults() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$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());
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsSettingSchema() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$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());
|
2017-07-14 15:08:47 +03:00
|
|
|
|
|
|
|
$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());
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsSettingContext() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$config = ServerConfig::create();
|
|
|
|
|
|
|
|
$context = [];
|
|
|
|
$config->setContext($context);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($context, $config->getContext());
|
2017-07-14 15:08:47 +03:00
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$context2 = new stdClass();
|
2017-07-14 15:08:47 +03:00
|
|
|
$config->setContext($context2);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($context2, $config->getContext());
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsSettingRootValue() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$config = ServerConfig::create();
|
|
|
|
|
|
|
|
$rootValue = [];
|
|
|
|
$config->setRootValue($rootValue);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($rootValue, $config->getRootValue());
|
2017-07-14 15:08:47 +03:00
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$context2 = new stdClass();
|
2017-07-14 15:08:47 +03:00
|
|
|
$config->setRootValue($context2);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($context2, $config->getRootValue());
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsSettingErrorFormatter() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$config = ServerConfig::create();
|
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$formatter = static function () {
|
2018-09-01 23:00:00 +03:00
|
|
|
};
|
2017-07-14 15:08:47 +03:00
|
|
|
$config->setErrorFormatter($formatter);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($formatter, $config->getErrorFormatter());
|
2017-07-14 15:08:47 +03:00
|
|
|
|
|
|
|
$formatter = 'date'; // test for callable
|
|
|
|
$config->setErrorFormatter($formatter);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($formatter, $config->getErrorFormatter());
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsSettingErrorsHandler() : void
|
2017-08-17 12:42:28 +03:00
|
|
|
{
|
|
|
|
$config = ServerConfig::create();
|
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$handler = static function () {
|
2018-09-01 23:00:00 +03:00
|
|
|
};
|
2017-08-17 12:42:28 +03:00
|
|
|
$config->setErrorsHandler($handler);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($handler, $config->getErrorsHandler());
|
2017-08-17 12:42:28 +03:00
|
|
|
|
|
|
|
$handler = 'date'; // test for callable
|
|
|
|
$config->setErrorsHandler($handler);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($handler, $config->getErrorsHandler());
|
2017-08-17 12:42:28 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsSettingPromiseAdapter() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$config = ServerConfig::create();
|
|
|
|
|
|
|
|
$adapter1 = new SyncPromiseAdapter();
|
|
|
|
$config->setPromiseAdapter($adapter1);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($adapter1, $config->getPromiseAdapter());
|
2017-07-14 15:08:47 +03:00
|
|
|
|
|
|
|
$adapter2 = new SyncPromiseAdapter();
|
|
|
|
$config->setPromiseAdapter($adapter2);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($adapter2, $config->getPromiseAdapter());
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsSettingValidationRules() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$config = ServerConfig::create();
|
|
|
|
|
|
|
|
$rules = [];
|
|
|
|
$config->setValidationRules($rules);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($rules, $config->getValidationRules());
|
2017-07-14 15:08:47 +03:00
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$rules = [static function () {
|
2018-09-01 23:00:00 +03:00
|
|
|
},
|
|
|
|
];
|
2017-07-14 15:08:47 +03:00
|
|
|
$config->setValidationRules($rules);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($rules, $config->getValidationRules());
|
2017-07-14 15:08:47 +03:00
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$rules = static function () {
|
|
|
|
return [static function () {
|
2018-09-01 23:00:00 +03:00
|
|
|
},
|
|
|
|
];
|
|
|
|
};
|
2017-07-14 15:08:47 +03:00
|
|
|
$config->setValidationRules($rules);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($rules, $config->getValidationRules());
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsSettingDefaultFieldResolver() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$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());
|
2017-07-14 15:08:47 +03:00
|
|
|
|
|
|
|
$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());
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsSettingPersistedQueryLoader() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$config = ServerConfig::create();
|
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
$loader = static function () {
|
2018-09-01 23:00:00 +03:00
|
|
|
};
|
2017-07-14 15:08:47 +03:00
|
|
|
$config->setPersistentQueryLoader($loader);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($loader, $config->getPersistentQueryLoader());
|
2017-07-14 15:08:47 +03:00
|
|
|
|
|
|
|
$loader = 'date'; // test for callable
|
|
|
|
$config->setPersistentQueryLoader($loader);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertSame($loader, $config->getPersistentQueryLoader());
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsSettingCatchPhpErrors() : void
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
|
|
|
$config = ServerConfig::create();
|
|
|
|
|
|
|
|
$config->setDebug(true);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertTrue($config->getDebug());
|
2017-07-14 15:08:47 +03:00
|
|
|
|
|
|
|
$config->setDebug(false);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertFalse($config->getDebug());
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
2017-08-15 14:05:09 +03:00
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAcceptsArray() : void
|
2017-08-15 14:05:09 +03:00
|
|
|
{
|
|
|
|
$arr = [
|
2018-09-01 23:00:00 +03:00
|
|
|
'schema' => new Schema([
|
|
|
|
'query' => new ObjectType(['name' => 't', 'fields' => ['a' => Type::string()]]),
|
2017-08-15 14:05:09 +03:00
|
|
|
]),
|
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,
|
2017-08-15 14:05:09 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$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());
|
2017-08-15 14:05:09 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testThrowsOnInvalidArrayKey() : void
|
2017-08-15 14:05:09 +03:00
|
|
|
{
|
2018-09-01 23:00:00 +03:00
|
|
|
$arr = ['missingKey' => 'value'];
|
2017-08-15 14:05:09 +03:00
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
$this->expectException(InvariantViolation::class);
|
|
|
|
$this->expectExceptionMessage('Unknown server config option "missingKey"');
|
2017-08-15 14:05:09 +03:00
|
|
|
|
|
|
|
ServerConfig::create($arr);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testInvalidValidationRules() : void
|
2017-08-15 14:05:09 +03:00
|
|
|
{
|
2018-09-26 12:07:23 +03:00
|
|
|
$rules = new stdClass();
|
2017-08-15 14:05:09 +03:00
|
|
|
$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');
|
2017-08-15 14:05:09 +03:00
|
|
|
|
|
|
|
$config->setValidationRules($rules);
|
|
|
|
}
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|