1
0
Fork 0
php-language-server/tests/Server/TextDocument/SignatureHelpTest.php

192 lines
7.5 KiB
PHP
Raw Normal View History

2017-12-10 05:10:43 +00:00
<?php
2018-11-27 16:51:40 +00:00
declare(strict_types=1);
2017-12-10 05:10:43 +00:00
namespace LanguageServer\Tests\Server\TextDocument;
use PHPUnit\Framework\TestCase;
use LanguageServer\Tests\MockProtocolStream;
2018-11-27 16:51:40 +00:00
use LanguageServer\{Server, LanguageClient, PhpDocumentLoader, DefinitionResolver};
2017-12-10 05:10:43 +00:00
use LanguageServer\Index\{Index, ProjectIndex, DependenciesIndex};
use LanguageServer\ContentRetriever\FileSystemContentRetriever;
use LanguageServerProtocol\{
2017-12-10 05:10:43 +00:00
TextDocumentIdentifier,
TextEdit,
Range,
Position,
SignatureHelp,
SignatureInformation,
ParameterInformation
};
use function LanguageServer\pathToUri;
class SignatureHelpTest extends TestCase
{
/**
* @var Server\TextDocument
*/
private $textDocument;
/**
* @var PhpDocumentLoader
*/
private $loader;
public function setUp()
{
2018-11-27 16:51:40 +00:00
$client = new LanguageClient(new MockProtocolStream(), new MockProtocolStream());
2017-12-10 05:10:43 +00:00
$index = new Index();
2018-11-27 16:51:40 +00:00
$projectIndex = new ProjectIndex($index, new DependenciesIndex());
2017-12-10 05:10:43 +00:00
$definitionResolver = new DefinitionResolver($projectIndex);
2018-11-27 16:51:40 +00:00
$contentRetriever = new FileSystemContentRetriever();
2017-12-10 05:10:43 +00:00
$this->loader = new PhpDocumentLoader($contentRetriever, $projectIndex, $definitionResolver);
$this->textDocument = new Server\TextDocument($this->loader, $definitionResolver, $client, $projectIndex);
$index->setComplete();
}
/**
* @dataProvider signatureHelpProvider
*/
public function testSignatureHelp(Position $position, SignatureHelp $expectedSignature)
{
$callsUri = pathToUri(__DIR__ . '/../../../fixtures/signature_help/calls.php');
$this->loader->open($callsUri, file_get_contents($callsUri));
2018-11-27 16:51:40 +00:00
$signatureHelp = $this->textDocument->signatureHelp(new TextDocumentIdentifier($callsUri), $position)->wait();
2017-12-10 05:10:43 +00:00
$this->assertEquals($expectedSignature, $signatureHelp);
}
public function signatureHelpProvider(): array
{
return [
'member call' => [
new Position(50, 9),
new SignatureHelp(
[
new SignatureInformation(
'(\\Foo\\SomethingElse $a, int|null $b = null)',
[
2018-11-27 16:51:40 +00:00
new ParameterInformation(
'\\Foo\\SomethingElse $a',
'A param with a different doc type'
),
new ParameterInformation('int|null $b = null', 'Param with default value')
2017-12-10 05:10:43 +00:00
],
'Function doc'
)
],
0,
0
2018-11-27 16:51:40 +00:00
)
2017-12-10 05:10:43 +00:00
],
'member call 2nd param active' => [
new Position(51, 12),
new SignatureHelp(
[
new SignatureInformation(
'(\\Foo\\SomethingElse $a, int|null $b = null)',
[
2018-11-27 16:51:40 +00:00
new ParameterInformation(
'\\Foo\\SomethingElse $a',
'A param with a different doc type'
),
new ParameterInformation('int|null $b = null', 'Param with default value')
2017-12-10 05:10:43 +00:00
],
'Function doc'
)
],
0,
1
2018-11-27 16:51:40 +00:00
)
2017-12-10 05:10:43 +00:00
],
'member call 2nd param active and closing )' => [
new Position(52, 11),
new SignatureHelp(
[
new SignatureInformation(
'(\\Foo\\SomethingElse $a, int|null $b = null)',
[
2018-11-27 16:51:40 +00:00
new ParameterInformation(
'\\Foo\\SomethingElse $a',
'A param with a different doc type'
),
new ParameterInformation('int|null $b = null', 'Param with default value')
2017-12-10 05:10:43 +00:00
],
'Function doc'
)
],
0,
1
2018-11-27 16:51:40 +00:00
)
2017-12-10 05:10:43 +00:00
],
'method with no params' => [
new Position(53, 9),
2018-11-27 16:51:40 +00:00
new SignatureHelp([new SignatureInformation('()', [], 'Method with no params', 0, 0)])
2017-12-10 05:10:43 +00:00
],
'constructor' => [
new Position(48, 14),
new SignatureHelp(
[
new SignatureInformation(
'(string $first, int $second, \Foo\Test $third)',
[
new ParameterInformation('string $first', 'First param'),
new ParameterInformation('int $second', 'Second param'),
2018-11-27 16:51:40 +00:00
new ParameterInformation('\Foo\Test $third', 'Third param with a longer description')
2017-12-10 05:10:43 +00:00
],
'Constructor comment goes here'
)
],
0,
0
2018-11-27 16:51:40 +00:00
)
2017-12-10 05:10:43 +00:00
],
'constructor argument expression list' => [
new Position(49, 16),
new SignatureHelp(
[
new SignatureInformation(
'(string $first, int $second, \Foo\Test $third)',
[
new ParameterInformation('string $first', 'First param'),
new ParameterInformation('int $second', 'Second param'),
2018-11-27 16:51:40 +00:00
new ParameterInformation('\Foo\Test $third', 'Third param with a longer description')
2017-12-10 05:10:43 +00:00
],
'Constructor comment goes here'
)
],
0,
1
2018-11-27 16:51:40 +00:00
)
2017-12-10 05:10:43 +00:00
],
'global function' => [
new Position(57, 15),
new SignatureHelp(
[
2018-11-27 16:51:40 +00:00
new SignatureInformation('(int $i, bool $b = false, \Foo\Test|null ...$things = null)', [
new ParameterInformation('int $i', 'Global function param one'),
new ParameterInformation('bool $b = false', 'Default false param'),
new ParameterInformation('\Foo\Test|null ...$things = null', 'Test things')
])
2017-12-10 05:10:43 +00:00
],
0,
2
)
],
'static method' => [
new Position(60, 10),
new SignatureHelp(
[new SignatureInformation('(mixed $a)', [new ParameterInformation('mixed $a')])],
0,
0
2018-11-27 16:51:40 +00:00
)
2017-12-10 05:10:43 +00:00
],
2018-11-27 16:51:40 +00:00
'no signature help' => [new Position(0, 0), new SignatureHelp([])],
'construct from non fqn (not supported)' => [new Position(62, 9), new SignatureHelp([])],
2017-12-10 05:10:43 +00:00
'construct from non fqn (not supported) argument expression' => [
new Position(63, 11),
2018-11-27 16:51:40 +00:00
new SignatureHelp([])
2017-12-10 05:10:43 +00:00
],
2018-11-27 16:51:40 +00:00
'invalid var' => [new Position(65, 13), new SignatureHelp([])]
2017-12-10 05:10:43 +00:00
];
}
}