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\Schema; |
15
|
|
|
use GraphQL\Type\Definition\ObjectType; |
16
|
|
|
use GraphQL\Type\Definition\Type; |
17
|
|
|
use Overblog\GraphQLBundle\Executor\Executor; |
18
|
|
|
use Overblog\GraphQLBundle\Request\Executor as RequestExecutor; |
19
|
|
|
|
20
|
|
|
class ExecutorTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
/** @var RequestExecutor */ |
23
|
|
|
private $executor; |
24
|
|
|
|
25
|
|
|
private $request = ['query' => 'query debug{ myField }', 'variables' => [], 'operationName' => null]; |
26
|
|
|
|
27
|
|
|
public function setUp() |
28
|
|
|
{ |
29
|
|
|
$this->executor = new RequestExecutor(new Executor()); |
30
|
|
|
$queryType = new ObjectType([ |
31
|
|
|
'name' => 'Query', |
32
|
|
|
'fields' => [ |
33
|
|
|
'myField' => [ |
34
|
|
|
'type' => Type::boolean(), |
35
|
|
|
'resolve' => function () { |
36
|
|
|
return false; |
37
|
|
|
}, |
38
|
|
|
], |
39
|
|
|
], |
40
|
|
|
]); |
41
|
|
|
$this->executor->addSchema('global', new Schema(['query' => $queryType])); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @expectedException \RuntimeException |
46
|
|
|
* @expectedExceptionMessage Execution result should be an object instantiating "GraphQL\Executor\ExecutionResult" or "GraphQL\Executor\Promise\Promise". |
47
|
|
|
*/ |
48
|
|
|
public function testInvalidExecutorReturnNotObject() |
49
|
|
|
{ |
50
|
|
|
$this->executor->setExecutor($this->createExecutorExecuteMock(false)); |
51
|
|
|
$this->executor->execute($this->request); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @expectedException \RuntimeException |
56
|
|
|
* @expectedExceptionMessage Execution result should be an object instantiating "GraphQL\Executor\ExecutionResult" or "GraphQL\Executor\Promise\Promise". |
57
|
|
|
*/ |
58
|
|
|
public function testInvalidExecutorReturnInvalidObject() |
59
|
|
|
{ |
60
|
|
|
$this->executor->setExecutor($this->createExecutorExecuteMock(new \stdClass())); |
61
|
|
|
$this->executor->execute($this->request); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testDisabledDebugInfo() |
65
|
|
|
{ |
66
|
|
|
$this->assertArrayNotHasKey('debug', $this->executor->disabledDebugInfo()->execute($this->request)->extensions); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testEnabledDebugInfo() |
70
|
|
|
{ |
71
|
|
|
$result = $this->executor->enabledDebugInfo()->execute($this->request); |
72
|
|
|
|
73
|
|
|
$this->assertArrayHasKey('debug', $result->extensions); |
74
|
|
|
$this->assertArrayHasKey('executionTime', $result->extensions['debug']); |
75
|
|
|
$this->assertArrayHasKey('memoryUsage', $result->extensions['debug']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @expectedException \RuntimeException |
80
|
|
|
* @expectedExceptionMessage At least one schema should be declare. |
81
|
|
|
*/ |
82
|
|
|
public function testGetSchemaNoSchemaFound() |
83
|
|
|
{ |
84
|
|
|
(new RequestExecutor(new Executor()))->getSchema('fake'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function createExecutorExecuteMock($returnValue) |
88
|
|
|
{ |
89
|
|
|
$mock = $this->getMockBuilder('Overblog\GraphQLBundle\Executor\Executor') |
90
|
|
|
->setMethods(['execute']) |
91
|
|
|
->getMock(); |
92
|
|
|
|
93
|
|
|
$mock->method('execute')->will($this->returnValue($returnValue)); |
94
|
|
|
|
95
|
|
|
return $mock; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|