-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTextDocumentHandlerTest.php
More file actions
180 lines (157 loc) · 6.08 KB
/
TextDocumentHandlerTest.php
File metadata and controls
180 lines (157 loc) · 6.08 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
<?php
namespace Phpactor\LanguageServer\Tests\Unit\Handler\TextDocument;
use Phpactor\LanguageServerProtocol\Position;
use Phpactor\LanguageServerProtocol\Range;
use Phpactor\LanguageServerProtocol\ServerCapabilities;
use Phpactor\LanguageServerProtocol\TextDocumentContentChangeIncrementalEvent;
use Phpactor\LanguageServerProtocol\TextDocumentIdentifier;
use Phpactor\LanguageServerProtocol\TextDocumentItem;
use Phpactor\LanguageServerProtocol\TextDocumentSyncKind;
use Phpactor\LanguageServer\Core\Handler\CanRegisterCapabilities;
use Phpactor\LanguageServer\Core\Handler\Handler;
use Phpactor\LanguageServer\Core\Rpc\ResponseMessage;
use Phpactor\LanguageServer\Event\TextDocumentClosed;
use Phpactor\LanguageServer\Event\TextDocumentIncrementallyUpdated;
use Phpactor\LanguageServer\Event\TextDocumentOpened;
use Phpactor\LanguageServer\Event\TextDocumentSaved;
use Phpactor\LanguageServer\Event\TextDocumentUpdated;
use Phpactor\LanguageServer\Handler\TextDocument\TextDocumentHandler;
use Phpactor\LanguageServer\Test\ProtocolFactory;
use Phpactor\LanguageServer\Tests\Unit\Handler\HandlerTestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\EventDispatcher\EventDispatcherInterface;
class TextDocumentHandlerTest extends HandlerTestCase
{
use ProphecyTrait;
/**
* @var ObjectProphecy<EventDispatcherInterface>
*/
private ObjectProphecy $dispatcher;
/**
* @var TextDocumentSyncKind::*
*/
private int $syncKind = TextDocumentSyncKind::FULL;
protected function setUp(): void
{
$this->dispatcher = $this->prophesize(EventDispatcherInterface::class);
}
public function handler(): Handler
{
return new TextDocumentHandler(
$this->dispatcher->reveal(),
$this->syncKind,
);
}
public function testSyncKindFull(): void
{
$handler = $this->handler();
self::assertInstanceOf(CanRegisterCapabilities::class, $handler);
$capabiltiies = new ServerCapabilities();
$handler->registerCapabiltiies($capabiltiies);
self::assertEquals(TextDocumentSyncKind::FULL, $capabiltiies->textDocumentSync);
}
public function testSyncKindIncremental(): void
{
$this->syncKind = TextDocumentSyncKind::INCREMENTAL;
$handler = $this->handler();
self::assertInstanceOf(CanRegisterCapabilities::class, $handler);
$capabiltiies = new ServerCapabilities();
$handler->registerCapabiltiies($capabiltiies);
self::assertEquals(TextDocumentSyncKind::INCREMENTAL, $capabiltiies->textDocumentSync);
}
public function testOpensDocument(): void
{
$textDocument = ProtocolFactory::textDocumentItem('foobar', 'foo');
$this->dispatch('textDocument/didOpen', [
'textDocument' => $textDocument,
]);
$this->dispatcher->dispatch(new TextDocumentOpened($textDocument))->shouldHaveBeenCalled();
}
public function testUpdatesDocument(): void
{
$textDocument = ProtocolFactory::textDocumentItem('foobar', 'foo');
$identifier = ProtocolFactory::versionedTextDocumentIdentifier('foobar', 1);
$this->dispatch('textDocument/didChange', [
'textDocument' => $identifier,
'contentChanges' => [
[
'text' => 'asd',
],
],
]);
$this->dispatcher->dispatch(new TextDocumentUpdated($identifier, 'asd'))->shouldHaveBeenCalled();
}
public function testUpdatesDocumentIncrementally(): void
{
$textDocument = ProtocolFactory::textDocumentItem('foobar', 'foo');
$identifier = ProtocolFactory::versionedTextDocumentIdentifier('foobar', 1);
$this->dispatch('textDocument/didChange', [
'textDocument' => $identifier,
'contentChanges' => [
[
'range' => [
'start' => [
'character' => 0,
'line' => 75,
],
'end' => [
'character' => 0,
'line' => 75,
],
],
'rangeLength' => 1,
'text' => 'hello',
],
],
]);
$this->dispatcher->dispatch(
new TextDocumentIncrementallyUpdated(
$identifier,
[
new TextDocumentContentChangeIncrementalEvent(
new Range(
new Position(75, 0),
new Position(75, 0),
),
rangeLength: 1,
text: 'hello',
),
],
)
)->shouldHaveBeenCalled();
}
public function testWillSave(): void
{
$response = $this->dispatch('textDocument/willSave', [
'textDocument' => ProtocolFactory::textDocumentIdentifier('foobar'),
'reason' => 1,
]);
self::assertInstanceOf(ResponseMessage::class, $response);
self::assertNull($response->result);
}
public function testClosesDocument(): void
{
$document = new TextDocumentItem('foobar', 'php', 1, 'foo');
$document->uri = 'foobar';
$identifier = new TextDocumentIdentifier('foobar');
$this->dispatch('textDocument/didClose', [
'textDocument' => $identifier,
'contentChanges' => [
[
'text' => 'asd',
],
],
]);
$this->dispatcher->dispatch(new TextDocumentClosed($identifier))->shouldHaveBeenCalled();
}
public function testSavesDocument(): void
{
$identifier = ProtocolFactory::versionedTextDocumentIdentifier('foobar', 1);
$this->dispatch('textDocument/didSave', [
'textDocument' => $identifier,
'text' => 'hello',
]);
$this->dispatcher->dispatch(new TextDocumentSaved($identifier, 'hello'))->shouldHaveBeenCalled();
}
}