2017-08-20 21:16:55 +03:00
|
|
|
<?php
|
|
|
|
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;
|
|
|
|
use GraphQL\Tests\Server\Psr7\PsrStreamStub;
|
|
|
|
|
|
|
|
class StandardServerTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ServerConfig
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$schema = $this->buildSchema();
|
|
|
|
$this->config = ServerConfig::create()
|
|
|
|
->setSchema($schema);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleRequestExecutionWithOutsideParsing()
|
|
|
|
{
|
|
|
|
$body = json_encode([
|
|
|
|
'query' => '{f1}'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$parsedBody = $this->parseRawRequest('application/json', $body);
|
|
|
|
$server = new StandardServer($this->config);
|
|
|
|
|
|
|
|
$result = $server->executeRequest($parsedBody);
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'f1' => 'f1',
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $result->toArray(true));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimplePsrRequestExecution()
|
|
|
|
{
|
2017-12-21 09:01:57 +03:00
|
|
|
$body = [
|
2017-08-20 21:16:55 +03:00
|
|
|
'query' => '{f1}'
|
2017-12-21 09:01:57 +03:00
|
|
|
];
|
2017-08-20 21:16:55 +03:00
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'f1' => 'f1'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2017-12-21 09:01:57 +03:00
|
|
|
$request = $this->preparePsrRequest('application/json', $body);
|
|
|
|
$this->assertPsrRequestEquals($expected, $request);
|
2017-08-20 21:16:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function executePsrRequest($psrRequest)
|
|
|
|
{
|
|
|
|
$server = new StandardServer($this->config);
|
|
|
|
$result = $server->executePsrRequest($psrRequest);
|
|
|
|
$this->assertInstanceOf(ExecutionResult::class, $result);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function assertPsrRequestEquals($expected, $request)
|
|
|
|
{
|
|
|
|
$result = $this->executePsrRequest($request);
|
|
|
|
$this->assertArraySubset($expected, $result->toArray(true));
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2017-12-21 09:01:57 +03:00
|
|
|
private function preparePsrRequest($contentType, $parsedBody)
|
2017-08-20 21:16:55 +03:00
|
|
|
{
|
|
|
|
$psrRequest = new PsrRequestStub();
|
|
|
|
$psrRequest->headers['content-type'] = [$contentType];
|
2017-11-26 13:57:32 +03:00
|
|
|
$psrRequest->method = 'POST';
|
2017-08-20 21:16:55 +03:00
|
|
|
$psrRequest->parsedBody = $parsedBody;
|
|
|
|
return $psrRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function parseRawRequest($contentType, $content, $method = 'POST')
|
|
|
|
{
|
|
|
|
$_SERVER['CONTENT_TYPE'] = $contentType;
|
|
|
|
$_SERVER['REQUEST_METHOD'] = $method;
|
|
|
|
|
|
|
|
$helper = new Helper();
|
|
|
|
return $helper->parseHttpRequest(function() use ($content) {
|
|
|
|
return $content;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|