1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\Lumen\GraphQL\Tests\Http; |
4
|
|
|
|
5
|
|
|
use Digia\Lumen\GraphQL\Execution\Processor; |
6
|
|
|
use Digia\Lumen\GraphQL\GraphQLService; |
7
|
|
|
use Digia\Lumen\GraphQL\Http\GraphQLController; |
8
|
|
|
use Digia\Lumen\GraphQL\Models\GraphQLError; |
9
|
|
|
use Digia\Lumen\GraphQL\Tests\TestCase; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
use Illuminate\View\View; |
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class GraphQLControllerTest |
16
|
|
|
* @package ALehdet\ContentApi\Tests\Unit\GraphQL\Http |
17
|
|
|
*/ |
18
|
|
|
class GraphQLControllerTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Tests that the processor is used properly when handling requests |
23
|
|
|
*/ |
24
|
|
|
public function testHandle() |
25
|
|
|
{ |
26
|
|
|
$service = $this->getMockedService([]); |
27
|
|
|
$controller = new GraphQLController($service); |
28
|
|
|
|
29
|
|
|
$response = $controller->handle(new Request()); |
30
|
|
|
|
31
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response); |
32
|
|
|
$this->assertEquals('[]', $response->getContent()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* |
37
|
|
|
*/ |
38
|
|
|
public function testHandleError() |
39
|
|
|
{ |
40
|
|
|
$service = $this->getMockedService([ |
41
|
|
|
'errors' => ['foo' => 'bar'], |
42
|
|
|
'exceptions' => [ |
43
|
|
|
new \Exception('foo'), |
44
|
|
|
], |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
// Make a request and handle it |
48
|
|
|
$controller = new GraphQLController($service); |
49
|
|
|
|
50
|
|
|
$request = new Request(); |
51
|
|
|
$request->merge([ |
52
|
|
|
'query' => 'Some query', |
53
|
|
|
'variables' => ['foo' => 'bar'], |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
$controller->handle($request); |
57
|
|
|
|
58
|
|
|
// Check that the error summary is properly set as an attribute |
59
|
|
|
$graphQLError = $request->attributes->get(GraphQLController::ATTRIBUTE_ERROR); |
60
|
|
|
$this->assertObjectHasAttribute('query', $graphQLError); |
61
|
|
|
$this->assertObjectHasAttribute('variables', $graphQLError); |
62
|
|
|
$this->assertObjectHasAttribute('exceptions', $graphQLError); |
63
|
|
|
|
64
|
|
|
$this->assertEquals(new GraphQLError( |
65
|
|
|
'Some query', |
66
|
|
|
['foo' => 'bar'], |
67
|
|
|
[new \Exception('foo')] |
68
|
|
|
), $graphQLError); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Test render GraphiQL view |
73
|
|
|
*/ |
74
|
|
|
public function testRenderGraphiQL() |
75
|
|
|
{ |
76
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|GraphQLService $service */ |
77
|
|
|
$service = $this->getMockBuilder(GraphQLService::class) |
78
|
|
|
->disableOriginalConstructor() |
79
|
|
|
->getMock(); |
80
|
|
|
|
81
|
|
|
// Make a request and handle it |
82
|
|
|
$controller = new GraphQLController($service); |
83
|
|
|
|
84
|
|
|
$this->assertInstanceOf(View::class, $controller->renderGraphiQL()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $responseData the response data that should be returned |
89
|
|
|
* |
90
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|GraphQLService $service |
91
|
|
|
*/ |
92
|
|
|
private function getMockedService(array $responseData) |
93
|
|
|
{ |
94
|
|
|
// Mock the processor |
95
|
|
|
$processor = $this->getMockBuilder(Processor::class) |
96
|
|
|
->disableOriginalConstructor() |
97
|
|
|
->setMethods(['getResponseData', 'processPayload']) |
98
|
|
|
->getMock(); |
99
|
|
|
|
100
|
|
|
$processor->expects($this->once()) |
101
|
|
|
->method('processPayload') |
102
|
|
|
->willReturn($processor); |
103
|
|
|
|
104
|
|
|
$processor->expects($this->once()) |
105
|
|
|
->method('getResponseData') |
106
|
|
|
->willReturn($responseData); |
107
|
|
|
|
108
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|GraphQLService $service */ |
109
|
|
|
$service = $this->getMockBuilder(GraphQLService::class) |
110
|
|
|
->disableOriginalConstructor() |
111
|
|
|
->setMethods(['getProcessor']) |
112
|
|
|
->getMock(); |
113
|
|
|
|
114
|
|
|
$service->expects($this->once()) |
115
|
|
|
->method('getProcessor') |
116
|
|
|
->willReturn($processor); |
117
|
|
|
|
118
|
|
|
return $service; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|