graphql-php/tests/Server/StandardServerTest.php

107 lines
2.9 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\Executor\ExecutionResult;
use GraphQL\Server\Helper;
use GraphQL\Server\ServerConfig;
use GraphQL\Server\StandardServer;
use GraphQL\Tests\Server\Psr7\PsrRequestStub;
2018-09-01 23:00:00 +03:00
use function json_encode;
2018-07-29 18:43:10 +03:00
class StandardServerTest extends ServerTestCase
{
2018-09-01 23:00:00 +03:00
/** @var ServerConfig */
private $config;
public function setUp()
{
2018-09-01 23:00:00 +03:00
$schema = $this->buildSchema();
$this->config = ServerConfig::create()
->setSchema($schema);
}
public function testSimpleRequestExecutionWithOutsideParsing() : void
{
2018-09-01 23:00:00 +03:00
$body = json_encode(['query' => '{f1}']);
$parsedBody = $this->parseRawRequest('application/json', $body);
2018-09-01 23:00:00 +03:00
$server = new StandardServer($this->config);
2018-09-01 23:00:00 +03:00
$result = $server->executeRequest($parsedBody);
$expected = [
2018-09-01 23:00:00 +03:00
'data' => ['f1' => 'f1'],
];
2018-09-19 18:12:09 +03:00
self::assertEquals($expected, $result->toArray(true));
}
2018-09-01 23:00:00 +03:00
private function parseRawRequest($contentType, $content, $method = 'POST')
{
2018-09-01 23:00:00 +03:00
$_SERVER['CONTENT_TYPE'] = $contentType;
$_SERVER['REQUEST_METHOD'] = $method;
2018-09-01 23:00:00 +03:00
$helper = new Helper();
2018-09-01 23:00:00 +03:00
return $helper->parseHttpRequest(function () use ($content) {
return $content;
});
}
2018-09-01 23:00:00 +03:00
public function testSimplePsrRequestExecution() : void
{
2018-09-01 23:00:00 +03:00
$body = ['query' => '{f1}'];
$expected = [
2018-09-01 23:00:00 +03:00
'data' => ['f1' => 'f1'],
];
$request = $this->preparePsrRequest('application/json', $body);
$this->assertPsrRequestEquals($expected, $request);
}
2018-09-01 23:00:00 +03:00
private function preparePsrRequest($contentType, $parsedBody)
{
2018-09-01 23:00:00 +03:00
$psrRequest = new PsrRequestStub();
$psrRequest->headers['content-type'] = [$contentType];
$psrRequest->method = 'POST';
$psrRequest->parsedBody = $parsedBody;
return $psrRequest;
}
private function assertPsrRequestEquals($expected, $request)
{
$result = $this->executePsrRequest($request);
2018-09-19 18:12:09 +03:00
self::assertArraySubset($expected, $result->toArray(true));
2018-09-01 23:00:00 +03:00
return $result;
}
2018-09-01 23:00:00 +03:00
private function executePsrRequest($psrRequest)
{
2018-09-01 23:00:00 +03:00
$server = new StandardServer($this->config);
$result = $server->executePsrRequest($psrRequest);
2018-09-19 18:12:09 +03:00
self::assertInstanceOf(ExecutionResult::class, $result);
2018-09-01 23:00:00 +03:00
return $result;
}
2018-09-01 23:00:00 +03:00
public function testMultipleOperationPsrRequestExecution() : void
{
2018-09-01 23:00:00 +03:00
$body = [
'query' => 'query firstOp {fieldWithPhpError} query secondOp {f1}',
'operationName' => 'secondOp',
];
2018-09-01 23:00:00 +03:00
$expected = [
'data' => ['f1' => 'f1'],
];
$request = $this->preparePsrRequest('application/json', $body);
$this->assertPsrRequestEquals($expected, $request);
}
}