2016-10-08 12:51:49 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace LanguageServer\Tests\Server\TextDocument;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use LanguageServer\Tests\MockProtocolStream;
|
2016-12-13 00:51:02 +00:00
|
|
|
use LanguageServer\{Server, Client, LanguageClient, PhpDocumentLoader, DefinitionResolver};
|
|
|
|
use LanguageServer\Index\{Index, ProjectIndex, DependenciesIndex};
|
2016-12-08 01:33:48 +00:00
|
|
|
use LanguageServer\ContentRetriever\FileSystemContentRetriever;
|
2016-11-14 09:25:44 +00:00
|
|
|
use LanguageServer\Protocol\{
|
|
|
|
TextDocumentIdentifier,
|
|
|
|
TextDocumentItem,
|
|
|
|
FormattingOptions,
|
|
|
|
ClientCapabilities,
|
|
|
|
TextEdit,
|
|
|
|
Range,
|
|
|
|
Position
|
|
|
|
};
|
2016-10-10 13:06:02 +00:00
|
|
|
use function LanguageServer\{pathToUri, uriToPath};
|
2016-10-08 12:51:49 +00:00
|
|
|
|
|
|
|
class FormattingTest extends TestCase
|
|
|
|
{
|
2016-10-10 13:06:02 +00:00
|
|
|
public function testFormatting()
|
2016-10-08 12:51:49 +00:00
|
|
|
{
|
2016-12-13 00:51:02 +00:00
|
|
|
$projectIndex = new ProjectIndex(new Index, new DependenciesIndex);
|
2016-10-31 10:47:21 +00:00
|
|
|
$client = new LanguageClient(new MockProtocolStream, new MockProtocolStream);
|
2016-12-13 00:51:02 +00:00
|
|
|
$definitionResolver = new DefinitionResolver($projectIndex);
|
|
|
|
$loader = new PhpDocumentLoader(new FileSystemContentRetriever, $projectIndex, $definitionResolver);
|
|
|
|
$textDocument = new Server\TextDocument($loader, $definitionResolver, $client, $projectIndex);
|
|
|
|
|
2016-10-10 13:06:02 +00:00
|
|
|
$path = realpath(__DIR__ . '/../../../fixtures/format.php');
|
|
|
|
$uri = pathToUri($path);
|
2016-10-08 12:51:49 +00:00
|
|
|
|
|
|
|
// Trigger parsing of source
|
|
|
|
$textDocumentItem = new TextDocumentItem();
|
2016-10-10 13:06:02 +00:00
|
|
|
$textDocumentItem->uri = $uri;
|
2016-10-08 12:51:49 +00:00
|
|
|
$textDocumentItem->languageId = 'php';
|
|
|
|
$textDocumentItem->version = 1;
|
2016-10-10 13:06:02 +00:00
|
|
|
$textDocumentItem->text = file_get_contents($path);
|
2016-10-08 12:51:49 +00:00
|
|
|
$textDocument->didOpen($textDocumentItem);
|
|
|
|
|
|
|
|
// how code should look after formatting
|
|
|
|
$expected = file_get_contents(__DIR__ . '/../../../fixtures/format_expected.php');
|
|
|
|
// Request formatting
|
2016-11-14 09:25:44 +00:00
|
|
|
$result = $textDocument->formatting(new TextDocumentIdentifier($uri), new FormattingOptions())->wait();
|
|
|
|
$this->assertEquals([new TextEdit(new Range(new Position(0, 0), new Position(20, 0)), $expected)], $result);
|
2016-10-08 12:51:49 +00:00
|
|
|
}
|
|
|
|
}
|