191 lines
9.9 KiB
PHP
191 lines
9.9 KiB
PHP
<?php
|
|
|
|
namespace JumPHP\Ide\Tests;
|
|
|
|
use JumPHP\Ide\Php\Index;
|
|
use JumPHP\Ide\Php\Intelligence;
|
|
use JumPHP\Ide\Workspace\MemoryWorkspace;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/** Class completion in type positions (parameters, promoted properties, return types...), the import it adds, the quick fix. */
|
|
final class ClassCompletionTest extends TestCase
|
|
{
|
|
private Intelligence $php;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$ws = new MemoryWorkspace([
|
|
'src/Repository/XxxRepository.php' => "<?php\nnamespace App\\Repository;\n\nclass XxxRepository {}\n",
|
|
'src/Repository/ZooRepository.php' => "<?php\nnamespace App\\Repository;\n\nclass ZooRepository {}\n",
|
|
'src/Service/Xxx.php' => "<?php\nnamespace App\\Service;\n\nclass Xxx {}\n",
|
|
'src/Other/Xxx.php' => "<?php\nnamespace App\\Other;\n\nclass Xxx {}\n",
|
|
'src/Entity/Xxx.php' => "<?php\nnamespace App\\Entity;\n\nclass Xxx {}\n",
|
|
'src/Entity/Product.php' => "<?php\nnamespace App\\Entity;\n\nclass Product {}\n",
|
|
'src/Controller/Other.php' => "<?php\nnamespace App\\Controller;\n\nclass Other {}\n",
|
|
]);
|
|
$index = new Index($ws);
|
|
$index->refresh();
|
|
$this->php = new Intelligence($index);
|
|
}
|
|
|
|
/** @return array{0: string, 1: int, 2: int} */
|
|
private function cursor(string $text): array
|
|
{
|
|
$offset = strpos($text, '|');
|
|
$before = substr($text, 0, $offset);
|
|
return [str_replace('|', '', $text), substr_count($before, "\n") + 1, strlen($before) - (strrpos($before, "\n") === false ? 0 : strrpos($before, "\n") + 1) + 1];
|
|
}
|
|
|
|
/** @return list<array<string, mixed>> */
|
|
private function items(string $text): array
|
|
{
|
|
[$text, $line, $col] = $this->cursor($text);
|
|
return $this->php->complete('src/Controller/HomeController.php', $text, $line, $col)['items'];
|
|
}
|
|
|
|
private const HEAD = "<?php\nnamespace App\\Controller;\n\nuse Symfony\\Component\\HttpFoundation\\Response;\n\nclass HomeController\n{\n";
|
|
|
|
public function testAParameterListBeingTypedProposesTheRepository(): void
|
|
{
|
|
$items = $this->items(self::HEAD . " public function index(XxxRep|\n}\n");
|
|
$labels = array_column($items, 'label');
|
|
self::assertContains('XxxRepository', $labels);
|
|
$item = $items[array_search('XxxRepository', $labels, true)];
|
|
self::assertStringContainsString('App\\Repository\\XxxRepository', $item['additionalTextEdits'][0]['text']);
|
|
}
|
|
|
|
public function testAParameterListWithATypedParameterBeforeStillCompletes(): void
|
|
{
|
|
$labels = array_column($this->items(self::HEAD . " public function index(Request \$r, XxxRep|): Response\n {\n return new Response();\n }\n}\n"), 'label');
|
|
self::assertContains('XxxRepository', $labels);
|
|
}
|
|
|
|
public function testAnEmptyWordInADiPositionListsProjectClassesFirst(): void
|
|
{
|
|
$labels = array_column($this->items(self::HEAD . " public function index(|\n}\n"), 'label');
|
|
self::assertContains('XxxRepository', $labels);
|
|
$first = array_slice($labels, 0, 12);
|
|
self::assertContains('XxxRepository', $first);
|
|
self::assertContains('ZooRepository', $first);
|
|
}
|
|
|
|
public function testTheUsualInjectedInterfacesComeFirstWhenTheyAreKnown(): void
|
|
{
|
|
$ws = new MemoryWorkspace([
|
|
'src/Aaa/Aaa.php' => "<?php\nnamespace App\\Aaa;\n\nclass Aaa {}\n",
|
|
'src/ORM/EntityManagerInterface.php' => "<?php\nnamespace Doctrine\\ORM;\n\ninterface EntityManagerInterface {}\n",
|
|
]);
|
|
$index = new Index($ws);
|
|
$index->refresh();
|
|
$php = new Intelligence($index);
|
|
[$text, $line, $col] = $this->cursor(self::HEAD . " public function index(|\n}\n");
|
|
$labels = array_column($php->complete('src/Controller/HomeController.php', $text, $line, $col)['items'], 'label');
|
|
self::assertLessThan(array_search('Aaa', $labels, true), array_search('EntityManagerInterface', $labels, true));
|
|
}
|
|
|
|
public function testAConstructorWithPromotedPropertiesCompletes(): void
|
|
{
|
|
$labels = array_column($this->items(self::HEAD . " public function __construct(private readonly XxxRep|\n ) {}\n}\n"), 'label');
|
|
self::assertContains('XxxRepository', $labels);
|
|
$labels = array_column($this->items(self::HEAD . " public function __construct(\n private readonly EntityManagerInterface \$em,\n private XxxRep|,\n ) {}\n}\n"), 'label');
|
|
self::assertContains('XxxRepository', $labels);
|
|
}
|
|
|
|
public function testReturnPropertyAndOtherTypePositions(): void
|
|
{
|
|
foreach ([
|
|
" public function index(): XxxRep|\n}\n",
|
|
" private XxxRep|\n}\n",
|
|
" public function f(): ?XxxRep|\n {\n }\n}\n",
|
|
" public function f() { if (\$a instanceof XxxRep|) {} }\n}\n",
|
|
" public function f() { try {} catch (XxxRep|) {} }\n}\n",
|
|
" #[XxxRep|]\n public function f() {}\n}\n",
|
|
" public function f(\$a, ?XxxRep|\n}\n",
|
|
" public function f() { \$g = fn (int \$a): XxxRep| => 1; }\n}\n",
|
|
" public function f() { return new XxxRep|; }\n}\n",
|
|
] as $code) {
|
|
self::assertContains('XxxRepository', array_column($this->items(self::HEAD . $code), 'label'), $code);
|
|
}
|
|
}
|
|
|
|
public function testAMethodSurroundedByCodeStillCompletesInItsParameters(): void
|
|
{
|
|
$code = self::HEAD . " public function a(): void { \$x = 1; }\n\n #[Route('/')]\n public function index(XxxRep|\n\n public function b(): void { \$y = 2; }\n}\n";
|
|
self::assertContains('XxxRepository', array_column($this->items($code), 'label'));
|
|
}
|
|
|
|
public function testNothingIsAddedForSameNamespaceOrAlreadyImported(): void
|
|
{
|
|
$items = $this->items("<?php\nnamespace App\\Repository;\n\nclass A { public function f(ZooRep|) {} }\n");
|
|
$zoo = $items[array_search('ZooRepository', array_column($items, 'label'), true)];
|
|
self::assertArrayNotHasKey('additionalTextEdits', $zoo);
|
|
$items = $this->items("<?php\nnamespace App\\Controller;\n\nuse App\\Repository\\ZooRepository;\n\nclass A { public function f(ZooRep|) {} }\n");
|
|
$zoo = $items[array_search('ZooRepository', array_column($items, 'label'), true)];
|
|
self::assertArrayNotHasKey('additionalTextEdits', $zoo);
|
|
}
|
|
|
|
public function testAnImportKeepsUnderTheNamespaceAndSortsWithTheOtherUses(): void
|
|
{
|
|
$text = "<?php\nnamespace App\\Controller;\n\nuse A\\First;\nuse Z\\Last;\n\nclass A { public function f(XxxRep|) {} }\n";
|
|
$items = $this->items($text);
|
|
$edit = $items[array_search('XxxRepository', array_column($items, 'label'), true)]['additionalTextEdits'][0];
|
|
// sorted: after "use A\First;" (line 4), so at the start of the line of "use Z\Last;" (line 5)
|
|
self::assertSame("use App\\Repository\\XxxRepository;\n", $edit['text']);
|
|
self::assertSame(5, $edit['range']['startLine']);
|
|
self::assertSame(1, $edit['range']['startColumn']);
|
|
}
|
|
|
|
public function testTheImportWorksWhenTheFileDoesNotParse(): void
|
|
{
|
|
$text = "<?php\nnamespace App\\Controller;\n\nuse A\\First;\nuse Z\\Last;\n\nclass A\n{\n public function f(XxxRep|\n";
|
|
$items = $this->items($text);
|
|
$edit = $items[array_search('XxxRepository', array_column($items, 'label'), true)]['additionalTextEdits'][0];
|
|
self::assertSame("use App\\Repository\\XxxRepository;\n", $edit['text']);
|
|
self::assertSame(5, $edit['range']['startLine']);
|
|
}
|
|
|
|
public function testAnImportGoesAfterTheLastUseWhenItSortsLast(): void
|
|
{
|
|
$items = $this->items("<?php\nnamespace App\\Controller;\n\nuse A\\First;\nuse Aa\\Second;\n\nclass A { public function f(XxxRep|) {} }\n");
|
|
$edit = $items[array_search('XxxRepository', array_column($items, 'label'), true)]['additionalTextEdits'][0];
|
|
self::assertSame("\nuse App\\Repository\\XxxRepository;", $edit['text']);
|
|
self::assertSame(5, $edit['range']['startLine']);
|
|
self::assertSame(15, $edit['range']['startColumn']);
|
|
}
|
|
|
|
public function testANameConflictGetsAnAlias(): void
|
|
{
|
|
$items = $this->items("<?php\nnamespace App\\Controller;\n\nuse App\\Entity\\Xxx;\n\nclass A { public function f(Xx|) {} }\n");
|
|
$service = null;
|
|
foreach ($items as $i) {
|
|
if (($i['detail'] ?? '') === 'App\\Service\\Xxx') {
|
|
$service = $i;
|
|
}
|
|
}
|
|
self::assertNotNull($service);
|
|
self::assertSame('ServiceXxx', $service['insertText']);
|
|
self::assertSame("\nuse App\\Service\\Xxx as ServiceXxx;", $service['additionalTextEdits'][0]['text']);
|
|
}
|
|
|
|
public function testTheQuickFixOffersTheClassesOfTheName(): void
|
|
{
|
|
$text = "<?php\nnamespace App\\Controller;\n\nclass A { public function f(XxxRepository \$r) {} }\n";
|
|
$col = strpos($text, 'XxxRepository') - strrpos(substr($text, 0, strpos($text, 'XxxRepository')), "\n");
|
|
$fixes = $this->php->importFixes('x.php', $text, 4, $col + 1);
|
|
self::assertCount(1, $fixes);
|
|
self::assertSame('App\\Repository\\XxxRepository', $fixes[0]['fqn']);
|
|
self::assertSame("\nuse App\\Repository\\XxxRepository;\n", $fixes[0]['edit']['text']);
|
|
self::assertSame(2, $fixes[0]['edit']['range']['startLine']);
|
|
|
|
$text = "<?php\nnamespace App\\Controller;\n\nclass A { public function f(Xxx \$r) {} }\n";
|
|
$col = strpos($text, 'Xxx') - strrpos(substr($text, 0, strpos($text, 'Xxx')), "\n");
|
|
$fixes = $this->php->importFixes('x.php', $text, 4, $col + 1);
|
|
self::assertSame(['App\\Entity\\Xxx', 'App\\Other\\Xxx', 'App\\Service\\Xxx'], array_column($fixes, 'fqn'));
|
|
}
|
|
|
|
public function testNoQuickFixForAKnownClass(): void
|
|
{
|
|
$text = "<?php\nnamespace App\\Controller;\n\nuse App\\Repository\\XxxRepository;\n\nclass A { public function f(XxxRepository \$r) {} }\n";
|
|
self::assertSame([], $this->php->importFixes('x.php', $text, 6, 35));
|
|
}
|
|
}
|