forked from phpactor/language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguageServerTester.php
More file actions
214 lines (183 loc) · 6.17 KB
/
LanguageServerTester.php
File metadata and controls
214 lines (183 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace Phpactor\LanguageServer\Test;
use Amp\Promise;
use Phpactor\LanguageServerProtocol\InitializeParams;
use Phpactor\LanguageServerProtocol\InitializeResult;
use Phpactor\LanguageServer\Core\Dispatcher\Dispatcher;
use Phpactor\LanguageServer\Core\Dispatcher\DispatcherFactory;
use Phpactor\LanguageServer\Core\Rpc\Message;
use Phpactor\LanguageServer\Core\Rpc\NotificationMessage;
use Phpactor\LanguageServer\Core\Rpc\RequestMessage;
use Phpactor\LanguageServer\Core\Rpc\ResponseMessage;
use Phpactor\LanguageServer\Core\Server\Transmitter\LspMessageSerializer;
use Phpactor\LanguageServer\Core\Server\Transmitter\MessageSerializer;
use Phpactor\LanguageServer\Core\Server\Transmitter\TestMessageTransmitter;
use Phpactor\LanguageServer\Test\LanguageServerTester\ServicesTester;
use Phpactor\LanguageServer\Test\LanguageServerTester\TextDocumentTester;
use Phpactor\LanguageServer\Test\LanguageServerTester\WorkspaceTester;
use RuntimeException;
use function Amp\Promise\wait;
final class LanguageServerTester
{
/**
* @var Dispatcher
*/
private $dispatcher;
public function __construct(
DispatcherFactory $factory,
private InitializeParams $initializeParams,
private TestMessageTransmitter $transmitter = new TestMessageTransmitter(),
private MessageSerializer $messageSerializer = new LspMessageSerializer(),
) {
$this->dispatcher = $factory->create($this->transmitter, $initializeParams);
}
/**
* @return Promise<ResponseMessage|null>
*/
public function dispatch(Message $message): Promise
{
return $this->dispatcher->dispatch($message);
}
public function dispatchAndWait(Message $message): ?ResponseMessage
{
return wait($this->dispatcher->dispatch($message));
}
/**
* @param array|object $params
* @param int|string $id
* @return Promise<ResponseMessage|null>
*/
public function request(string $method, $params, $id = null): Promise
{
$requestMessage = new RequestMessage($id ?: uniqid(), $method, $this->normalizeParams($params));
return $this->dispatch($requestMessage);
}
/**
* @param array|object $params
* @param int|string $id
*/
public function requestAndWait(string $method, $params, $id = null): ?ResponseMessage
{
return wait($this->request($method, $this->normalizeParams($params), $id));
}
/**
* @param array|object $params
*/
public function mustRequestAndWait(string $method, mixed $params, int|string|null $id = null): ResponseMessage
{
$response = $this->requestAndWait($method, $params);
if (null === $response) {
throw new RuntimeException(sprintf(
'Expected request to method "%s" to return a response, but it did not!',
$method
));
}
return $response;
}
/**
* @param mixed $value
* @param string|int $id
* @return Promise<mixed>
*/
public function respond($id, $value): Promise
{
$requestMessage = new ResponseMessage($id, $value);
return $this->dispatch($requestMessage);
}
/**
* @param mixed $value
* @param string|int $id
* @return mixed
*/
public function respondAndWait($id, $value)
{
return wait($this->respond($id, $value));
}
/**
* @param array|object $params
* @return Promise<ResponseMessage|null>
*/
public function notify(string $method, $params): Promise
{
$notifyMessage = new NotificationMessage($method, $this->normalizeParams($params));
return $this->dispatch($notifyMessage);
}
/**
* @param array|object $params
*/
public function notifyAndWait(string $method, $params): void
{
wait($this->notify($method, $this->normalizeParams($params)));
}
public function transmitter(): TestMessageTransmitter
{
return $this->transmitter;
}
/**
* Initialize the server using the initialization parameters provided when
* this class was instantiated and return the processed ServerCapabilties.
*/
public function initialize(): InitializeResult
{
$response = $this->mustRequestAndWait('initialize', $this->initializeParams);
$this->assertSuccess($response);
$this->notifyAndWait('initialized', []);
$result = $response->result;
if (!$result instanceof InitializeResult) {
throw new RuntimeException(sprintf(
'Initialize did not return an InitializeResult, got "%s"',
is_object($result) ? get_class($result) : gettype($result)
));
}
return $result;
}
public function services(): ServicesTester
{
return new ServicesTester($this);
}
public function textDocument(): TextDocumentTester
{
return new TextDocumentTester($this);
}
/**
* Assert the the response is successful.
*
* @throws RuntimeException if not successful.
*/
public function assertSuccess(ResponseMessage $response): void
{
if ($response->error) {
throw new RuntimeException(sprintf(
'Response has error: %s'."\n".'%s',
$response->error->message,
is_string($response->error->data) ? $response->error->data : json_encode($response->error->data, JSON_PRETTY_PRINT)
));
}
}
public function cancel(int $requestId): void
{
$this->dispatchAndWait(new NotificationMessage('$/cancelRequest', ['id' => $requestId]));
}
public function workspace(): WorkspaceTester
{
return new WorkspaceTester($this);
}
/**
* @param array|object $params
* @return array<string,mixed>
*/
private function normalizeParams($params): array
{
if (is_array($params)) {
return $params;
}
$params = $this->messageSerializer->normalize($params);
if (!is_array($params)) {
throw new RuntimeException(sprintf(
'Could not normalize params, returned as type %s',
gettype($params)
));
}
return $params;
}
}