Testing¶
This package includes some tools to make testing easier.
Protocol Factory¶
The ProtocolFactory
is a utility class for creating LSP protocol objects:
<?php
use Phpactor\LanguageServer\Test\ProtocolFactory;
$item = ProtocolFactory::textDocumentItem('uri', 'content');
$initializeParams = ProtocolFactory::initializeParams('/path/to/rootUri');
This is useful as the LSP objects can be complicated and we can assume some defaults using the factory.
Unit Testing Handlers, Services etc.¶
You can use the Language Server Tester to test your handlers, services, commands etc as follows:
<?php
$tester = LanguageServerTesterBuilder::create()
->addHanlder($myHandler)
->addServiceProvider($myServiceProvider)
->addCommand($myCommand)
->build();
$result = $tester->requestAndWait('soMyThing', []);
Lean more about the LanguageServerTester
Integration Testing¶
If you are using the LanguageServerBuilder
to manage the instantiation of
your LanguageServer
then, assuming you are using some kind of dependency
injection container, you can use the tester
method to get the
Language Server Tester.
<?php
$builder = $container->get(LanguageServerBuilder::class);
assert($builder instanceof LanguageServerBuilder);
$tester = $builder->tester();
$response = $tester->requestAndWait('foobar', ['bar' => 'foo']);
$response = $tester->notifyAndWait('foobar', ['bar' => 'foo']);
This will provide the Language Server Tester with the “real” dispatcher.
Language Server Tester¶
The tester provides access to a test transmitter from which you can access any message sent by the server:
<?php
// ...
$messageOrNull = $tester->transmitter()->shift();
You can also use some convenience methods to control the server:
<?php
// ...
$messageOrNull = $tester->textDocument()->open('/uri/to/text.php', 'content');
$tester->services()->start('myservice');
The tester will automatically initialize the server, but you can also pass your own initialization parameters:
<?php
// ...
$tester = $builder->tester(ProtocolFactory::initializeParams('/uri/foobar.php'));