1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Tests\Request; |
13
|
|
|
|
14
|
|
|
use GraphQL\Executor\Promise\Adapter\ReactPromiseAdapter; |
15
|
|
|
use GraphQL\Schema; |
16
|
|
|
use GraphQL\Type\Definition\ObjectType; |
17
|
|
|
use GraphQL\Type\Definition\Type; |
18
|
|
|
use Overblog\GraphQLBundle\Executor\Executor; |
19
|
|
|
use Overblog\GraphQLBundle\Request\Executor as RequestExecutor; |
20
|
|
|
|
21
|
|
|
class ExecutorTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
/** @var RequestExecutor */ |
24
|
|
|
private $executor; |
25
|
|
|
|
26
|
|
|
private $request = ['query' => 'query debug{ myField }', 'variables' => [], 'operationName' => null]; |
27
|
|
|
|
28
|
|
|
public function setUp() |
29
|
|
|
{ |
30
|
|
|
$this->executor = new RequestExecutor(new Executor()); |
31
|
|
|
$queryType = new ObjectType([ |
32
|
|
|
'name' => 'Query', |
33
|
|
|
'fields' => [ |
34
|
|
|
'myField' => [ |
35
|
|
|
'type' => Type::boolean(), |
36
|
|
|
'resolve' => function () { |
37
|
|
|
return false; |
38
|
|
|
}, |
39
|
|
|
], |
40
|
|
|
], |
41
|
|
|
]); |
42
|
|
|
$this->executor->addSchema('global', new Schema(['query' => $queryType])); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @expectedException \RuntimeException |
47
|
|
|
* @expectedExceptionMessage Execution result should be an object instantiating "GraphQL\Executor\ExecutionResult". |
48
|
|
|
*/ |
49
|
|
|
public function testInvalidExecutorReturnNotObject() |
50
|
|
|
{ |
51
|
|
|
$this->executor->setExecutor($this->createExecutorExecuteMock(false)); |
52
|
|
|
$this->executor->execute($this->request); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @expectedException \RuntimeException |
57
|
|
|
* @expectedExceptionMessage Execution result should be an object instantiating "GraphQL\Executor\ExecutionResult". |
58
|
|
|
*/ |
59
|
|
|
public function testInvalidExecutorReturnInvalidObject() |
60
|
|
|
{ |
61
|
|
|
$this->executor->setExecutor($this->createExecutorExecuteMock(new \stdClass())); |
62
|
|
|
$this->executor->execute($this->request); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @expectedException \RuntimeException |
67
|
|
|
* @expectedExceptionMessage PromiseAdapter should be an object instantiating "Overblog\GraphQLBundle\Executor\Promise\PromiseAdapterInterface" or "GraphQL\Executor\Promise\PromiseAdapter" with a "wait" method. |
68
|
|
|
*/ |
69
|
|
|
public function testInvalidExecutorAdapterPromise() |
70
|
|
|
{ |
71
|
|
|
$this->executor->setPromiseAdapter(new ReactPromiseAdapter()); |
72
|
|
|
$this->executor->execute($this->request); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testDisabledDebugInfo() |
76
|
|
|
{ |
77
|
|
|
$this->assertArrayNotHasKey('debug', $this->executor->disabledDebugInfo()->execute($this->request)->extensions); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testEnabledDebugInfo() |
81
|
|
|
{ |
82
|
|
|
$result = $this->executor->enabledDebugInfo()->execute($this->request); |
83
|
|
|
|
84
|
|
|
$this->assertArrayHasKey('debug', $result->extensions); |
85
|
|
|
$this->assertArrayHasKey('executionTime', $result->extensions['debug']); |
86
|
|
|
$this->assertArrayHasKey('memoryUsage', $result->extensions['debug']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @expectedException \RuntimeException |
91
|
|
|
* @expectedExceptionMessage At least one schema should be declare. |
92
|
|
|
*/ |
93
|
|
|
public function testGetSchemaNoSchemaFound() |
94
|
|
|
{ |
95
|
|
|
(new RequestExecutor(new Executor()))->getSchema('fake'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function createExecutorExecuteMock($returnValue) |
99
|
|
|
{ |
100
|
|
|
$mock = $this->getMockBuilder('Overblog\GraphQLBundle\Executor\Executor') |
101
|
|
|
->setMethods(['execute']) |
102
|
|
|
->getMock(); |
103
|
|
|
|
104
|
|
|
$mock->method('execute')->will($this->returnValue($returnValue)); |
105
|
|
|
|
106
|
|
|
return $mock; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|