1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Tests\Request; |
4
|
|
|
|
5
|
|
|
use GraphQL\Executor\Promise\Adapter\ReactPromiseAdapter; |
6
|
|
|
use GraphQL\Executor\Promise\PromiseAdapter; |
7
|
|
|
use GraphQL\Type\Definition\ObjectType; |
8
|
|
|
use GraphQL\Type\Definition\Type; |
9
|
|
|
use GraphQL\Type\Schema; |
10
|
|
|
use Overblog\GraphQLBundle\Executor\Executor; |
11
|
|
|
use Overblog\GraphQLBundle\Request\Executor as RequestExecutor; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
14
|
|
|
|
15
|
|
|
class ExecutorTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** @var RequestExecutor */ |
18
|
|
|
private $executor; |
19
|
|
|
|
20
|
|
|
/** @var EventDispatcher|\PHPUnit_Framework_MockObject_MockObject */ |
21
|
|
|
private $dispatcher; |
22
|
|
|
|
23
|
|
|
private $request = ['query' => 'query debug{ myField }', 'variables' => [], 'operationName' => null]; |
24
|
|
|
|
25
|
|
|
public function setUp() |
26
|
|
|
{ |
27
|
|
|
$this->dispatcher = $this->getMockBuilder(EventDispatcher::class)->setMethods(['dispatch'])->getMock(); |
28
|
|
|
$this->dispatcher->expects($this->any())->method('dispatch')->willReturnArgument(1); |
29
|
|
|
|
30
|
|
|
$this->executor = $this->createRequestExecutor(); |
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(null, $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(null, $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(null, $this->request); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @expectedException \RuntimeException |
77
|
|
|
* @expectedExceptionMessage At least one schema should be declare. |
78
|
|
|
*/ |
79
|
|
|
public function testGetSchemaNoSchemaFound() |
80
|
|
|
{ |
81
|
|
|
$this->createRequestExecutor()->getSchema('fake'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function createExecutorExecuteMock($returnValue) |
85
|
|
|
{ |
86
|
|
|
$mock = $this->getMockBuilder(Executor::class) |
87
|
|
|
->setMethods(['execute']) |
88
|
|
|
->getMock(); |
89
|
|
|
|
90
|
|
|
$mock->method('execute')->will($this->returnValue($returnValue)); |
91
|
|
|
|
92
|
|
|
return $mock; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function createRequestExecutor() |
96
|
|
|
{ |
97
|
|
|
/** @var PromiseAdapter|\PHPUnit_Framework_MockObject_MockObject $promiseAdapter */ |
98
|
|
|
$promiseAdapter = $this->getMockBuilder(PromiseAdapter::class) |
99
|
|
|
->setMethods(array_merge(['wait'], get_class_methods(PromiseAdapter::class))) |
100
|
|
|
->getMock(); |
101
|
|
|
|
102
|
|
|
return new RequestExecutor(new Executor(), $this->dispatcher, $promiseAdapter); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: