|
| 1 | +<?php |
| 2 | +declare(strict_types = 1); |
| 3 | + |
| 4 | +namespace LanguageServer\Tests\Diagnostics; |
| 5 | + |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use phpDocumentor\Reflection\DocBlockFactory; |
| 8 | +use LanguageServer\{ |
| 9 | + DefinitionResolver, TreeAnalyzer |
| 10 | +}; |
| 11 | +use LanguageServer\Index\{Index}; |
| 12 | +use LanguageServer\Protocol\{ |
| 13 | + Diagnostic, DiagnosticSeverity, Position, Range |
| 14 | +}; |
| 15 | +use function LanguageServer\pathToUri; |
| 16 | +use Microsoft\PhpParser\Parser; |
| 17 | + |
| 18 | +class InvalidThisUsageTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Parse the given file and return diagnostics. |
| 22 | + * |
| 23 | + * @param string $path |
| 24 | + * @return Diagnostic[] |
| 25 | + */ |
| 26 | + private function collectDiagnostics(string $path): array |
| 27 | + { |
| 28 | + $uri = pathToUri($path); |
| 29 | + $parser = new Parser(); |
| 30 | + |
| 31 | + $docBlockFactory = DocBlockFactory::createInstance(); |
| 32 | + $index = new Index; |
| 33 | + $definitionResolver = new DefinitionResolver($index); |
| 34 | + $content = file_get_contents($path); |
| 35 | + |
| 36 | + $treeAnalyzer = new TreeAnalyzer($parser, $content, $docBlockFactory, $definitionResolver, $uri); |
| 37 | + return $treeAnalyzer->getDiagnostics(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Assertions about a diagnostic. |
| 42 | + * |
| 43 | + * @param Diagnostic|null $diagnostic |
| 44 | + * @param int $message |
| 45 | + * @param string $severity |
| 46 | + * @param Range $range |
| 47 | + */ |
| 48 | + private function assertDiagnostic($diagnostic, $message, $severity, $range) |
| 49 | + { |
| 50 | + $this->assertInstanceOf(Diagnostic::class, $diagnostic); |
| 51 | + $this->assertEquals($message, $diagnostic->message); |
| 52 | + $this->assertEquals($severity, $diagnostic->severity); |
| 53 | + $this->assertEquals($range, $diagnostic->range); |
| 54 | + } |
| 55 | + |
| 56 | + public function testThisInStaticMethodProducesError() |
| 57 | + { |
| 58 | + $diagnostics = $this->collectDiagnostics( |
| 59 | + __DIR__ . '/../../fixtures/diagnostics/errors/this_in_static_method.php' |
| 60 | + ); |
| 61 | + |
| 62 | + $this->assertCount(1, $diagnostics); |
| 63 | + $this->assertDiagnostic( |
| 64 | + $diagnostics[0], |
| 65 | + '$this can not be used in static methods.', |
| 66 | + DiagnosticSeverity::ERROR, |
| 67 | + new Range( |
| 68 | + new Position(6, 15), |
| 69 | + new Position(6, 20) |
| 70 | + ) |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + public function testThisInFunctionProducesError() |
| 75 | + { |
| 76 | + $diagnostics = $this->collectDiagnostics( |
| 77 | + __DIR__ . '/../../fixtures/diagnostics/errors/this_in_function.php' |
| 78 | + ); |
| 79 | + |
| 80 | + $this->assertCount(1, $diagnostics); |
| 81 | + $this->assertDiagnostic( |
| 82 | + $diagnostics[0], |
| 83 | + '$this can only be used in an object context.', |
| 84 | + DiagnosticSeverity::ERROR, |
| 85 | + new Range( |
| 86 | + new Position(4, 11), |
| 87 | + new Position(4, 16) |
| 88 | + ) |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + public function testThisInRoot() |
| 93 | + { |
| 94 | + $diagnostics = $this->collectDiagnostics( |
| 95 | + __DIR__ . '/../../fixtures/diagnostics/errors/this_in_root.php' |
| 96 | + ); |
| 97 | + |
| 98 | + $this->assertCount(1, $diagnostics); |
| 99 | + $this->assertDiagnostic( |
| 100 | + $diagnostics[0], |
| 101 | + '$this can only be used in an object context.', |
| 102 | + DiagnosticSeverity::ERROR, |
| 103 | + new Range( |
| 104 | + new Position(2, 5), |
| 105 | + new Position(2, 10) |
| 106 | + ) |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + public function testThisInMethodProducesNoError() |
| 111 | + { |
| 112 | + $diagnostics = $this->collectDiagnostics( |
| 113 | + __DIR__ . '/../../fixtures/diagnostics/baselines/this_in_method.php' |
| 114 | + ); |
| 115 | + |
| 116 | + $this->assertCount(0, $diagnostics); |
| 117 | + } |
| 118 | +} |
0 commit comments