| 
									
										
										
										
											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; | 
					
						
							|  |  |  | use LanguageServer\{Server, Client, LanguageClient, Project}; | 
					
						
							|  |  |  | use LanguageServer\Protocol\{TextDocumentIdentifier, TextDocumentItem, FormattingOptions}; | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var Server\TextDocument | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private $textDocument; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setUp() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-31 10:47:21 +00:00
										 |  |  |         $client = new LanguageClient(new MockProtocolStream, new MockProtocolStream); | 
					
						
							| 
									
										
										
										
											2016-10-08 12:51:49 +00:00
										 |  |  |         $project = new Project($client); | 
					
						
							|  |  |  |         $this->textDocument = new Server\TextDocument($project, $client); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 13:06:02 +00:00
										 |  |  |     public function testFormatting() | 
					
						
							| 
									
										
										
										
											2016-10-08 12:51:49 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-31 10:47:21 +00:00
										 |  |  |         $client = new LanguageClient(new MockProtocolStream, new MockProtocolStream); | 
					
						
							| 
									
										
										
										
											2016-10-08 12:51:49 +00:00
										 |  |  |         $project = new Project($client); | 
					
						
							|  |  |  |         $textDocument = new Server\TextDocument($project, $client); | 
					
						
							| 
									
										
										
										
											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-10-10 13:06:02 +00:00
										 |  |  |         $result = $textDocument->formatting(new TextDocumentIdentifier($uri), new FormattingOptions()); | 
					
						
							| 
									
										
										
										
											2016-10-08 12:51:49 +00:00
										 |  |  |         $this->assertEquals([0 => [ | 
					
						
							|  |  |  |             'range' => [ | 
					
						
							|  |  |  |                 'start' => [ | 
					
						
							|  |  |  |                     'line' => 0, | 
					
						
							|  |  |  |                     'character' => 0 | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |                 'end' => [ | 
					
						
							| 
									
										
										
										
											2016-10-10 13:06:02 +00:00
										 |  |  |                     'line' => 20, | 
					
						
							|  |  |  |                     'character' => 0 | 
					
						
							| 
									
										
										
										
											2016-10-08 12:51:49 +00:00
										 |  |  |                 ] | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             'newText' => $expected | 
					
						
							|  |  |  |         ]], json_decode(json_encode($result), true)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |