rob-revisited/ide/tests/YamlNavigationTest.php

233 lines
15 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Ide;
use JumPHP\Ide\Php\Index;
use JumPHP\Ide\Workspace\MemoryWorkspace;
use JumPHP\Ide\Yaml\YamlNavigation;
use JumPHP\Ide\Yaml\YamlTokens;
use PHPUnit\Framework\TestCase;
/** Ctrl+click, usages, hover, links and rename in YAML files (JumPHP\Ide\Yaml\YamlNavigation). */
final class YamlNavigationTest extends TestCase
{
private MemoryWorkspace $ws;
private YamlNavigation $nav;
private const SERVICES = <<<'YAML'
imports:
- { resource: 'packages/*.yaml' }
parameters:
app.locale: fr
services:
_defaults:
autowire: true
App\:
resource: '../src/'
exclude: '../src/{Kernel.php,Entity}'
App\Mailer\Mailer:
arguments: ['%app.locale%', '%env(MAILER_DSN)%']
calls:
- method: setLogger
arguments: ['@logger.main']
tags: [app.handler]
logger.main:
class: App\Log\Logger
factory: ['@log.alias', 'create']
log.alias: '@App\Log\Logger'
App\Handlers:
arguments: [!tagged_iterator app.handler, !php/const App\Mailer\Mailer::LIMIT]
YAML;
protected function setUp(): void
{
$this->ws = new MemoryWorkspace([
'site/composer.json' => '{}',
'site/.env' => "APP_ENV=dev\nMAILER_DSN=smtp://localhost\n",
'site/config/services.yaml' => self::SERVICES,
'site/config/packages/twig.yaml' => "twig:\n default_path: '%kernel.project_dir%/templates'\n",
'site/config/routes.yaml' => "home:\n path: /\n controller: App\\Controller\\HomeController::index\ncontrollers:\n resource: ../src/Controller/\n type: attribute\n",
'site/config/packages/security.yaml' => "security:\n providers:\n users:\n entity: {class: App\\Entity\\User}\n firewalls:\n main:\n provider: users\n",
'site/src/Kernel.php' => "<?php\nnamespace App;\nclass Kernel {}\n",
'site/src/Entity/User.php' => "<?php\nnamespace App\\Entity;\nclass User {}\n",
'site/src/Mailer/Mailer.php' => "<?php\nnamespace App\\Mailer;\n\nfinal class Mailer\n{\n public const LIMIT = 5;\n public function setLogger(\$l): void {}\n}\n",
'site/src/Log/Logger.php' => "<?php\nnamespace App\\Log;\n\nfinal class Logger\n{\n public function create(): self { return \$this; }\n}\n",
'site/src/Controller/HomeController.php' => "<?php\nnamespace App\\Controller;\n\nfinal class HomeController\n{\n public function index(): string { return \$this->trans('home.title'); }\n}\n",
'site/templates/base.html.twig' => "<h1>{{ 'home.title'|trans }}</h1>\n",
'site/translations/messages.fr.yaml' => "home:\n title: Accueil\n",
'site/translations/messages.en.yaml' => "home.title: Home\n",
'stack/compose.yaml' => "x-base: &base\n restart: always\n env_file: .env\nservices:\n web:\n <<: *base\n build:\n context: ./docker\n dockerfile: web.Dockerfile\n depends_on: [db]\n volumes:\n - data:/var/lib/data\n - ./app:/app\n networks:\n - front\n db:\n image: postgres\n worker:\n extends:\n service: web\nnetworks:\n front: {}\nvolumes:\n data:\n",
'stack/.env' => "A=1\n",
'stack/docker/web.Dockerfile' => "FROM php\n",
'stack/app/index.php' => "<?php\n",
'games/g/config/project.yaml' => "project:\n title: G\n start_scene: App\\Scene\\TitleScene\n smoke: {scene: App\\Scene\\PlayScene}\n icon: assets/icon.png\n",
'games/g/assets/icon.png' => 'png',
'games/g/config/input.yaml' => "actions:\n jump: [Space, pad0:a]\n",
'games/g/config/lang.yaml' => "fr:\n PLAY: JOUER\nde:\n PLAY: SPIELEN\n",
'games/g/src/Scene/TitleScene.php' => "<?php\nnamespace App\\Scene;\n\nfinal class TitleScene\n{\n public function items(): array { return [['PLAY', null]]; }\n public function t(): string { return Lang::t('PLAY'); }\n}\n",
'games/g/src/Scene/PlayScene.php' => "<?php\nnamespace App\\Scene;\n\nfinal class PlayScene\n{\n public function update(): void { if (\$this->actions()->isPressed('jump')) {} }\n}\n",
'games/g/forms/MainForm.form.yaml' => "form:\n children:\n - {type: button, id: save, text: PLAY, events: {click: onSaveClick}}\n",
'games/g/src/Form/MainForm.php' => "<?php\nnamespace App\\Form;\n\nfinal class MainForm\n{\n public function onSaveClick(): void {}\n}\n",
'.github/workflows/ci.yml' => "jobs:\n a:\n steps:\n - uses: ./.github/actions/setup\n",
'.github/actions/setup/action.yml' => "name: setup\n",
]);
$index = new Index($this->ws);
$index->refresh();
$this->nav = new YamlNavigation($this->ws, $index);
}
/** [line, column] of the first "|" in a marked text, and the text without it. @return array{0: string, 1: int, 2: int} */
private static function at(string $marked): array
{
$offset = strpos($marked, '|');
self::assertIsInt($offset);
$before = substr($marked, 0, $offset);
$line = substr_count($before, "\n") + 1;
$col = strlen($before) - (int)(strrpos("\n" . $before, "\n")) + 1;
return [substr($marked, 0, $offset) . substr($marked, $offset + 1), $line, $col];
}
/** Where Ctrl+click on the marked place of a file goes: "path:line". @return list<string> */
private function go(string $path, string $needle, int $shift = 1): array
{
$text = $this->ws->read($path);
$offset = strpos($text, $needle);
self::assertIsInt($offset, $needle);
[$text, $line, $col] = self::at(substr_replace($text, '|', $offset + $shift, 0));
return array_map(fn ($l) => $l['path'] . ':' . $l['line'], $this->nav->definition($path, $text, $line, $col));
}
/** @return list<string> "path:line" of the usages */
private function uses(string $path, string $needle, int $shift = 1, bool $declarations = true): array
{
$text = $this->ws->read($path);
$offset = strpos($text, $needle);
self::assertIsInt($offset, $needle);
[$text, $line, $col] = self::at(substr_replace($text, '|', $offset + $shift, 0));
return array_map(fn ($l) => $l['path'] . ':' . $l['line'], $this->nav->references($path, $text, $line, $col, $declarations));
}
public function testTheTokensOfALineKnowKeysFlowsTagsAnchorsAndAliases(): void
{
$tokens = YamlTokens::scan(" arguments: [!tagged_iterator app.handler, '@x', {a: *ref}] # note");
self::assertSame(['arguments', 'app.handler', '@x', 'a', 'ref'], array_column($tokens, 'text'));
self::assertTrue($tokens[0]['isKey']);
self::assertSame('tagged_iterator', $tokens[1]['tag']);
self::assertSame(17 + strlen('!tagged_iterator '), $tokens[1]['col']);
self::assertTrue($tokens[4]['alias']);
self::assertSame('a', $tokens[4]['keys'][2]);
}
public function testAnAliasLeadsToItsAnchorAndBack(): void
{
self::assertSame(['stack/compose.yaml:1'], $this->go('stack/compose.yaml', '*base'));
self::assertSame(['stack/compose.yaml:1', 'stack/compose.yaml:6'], $this->uses('stack/compose.yaml', '&base'));
// the merged keys show in the hover
[$text, $line, $col] = self::at(str_replace('<<: *base', '<<: *ba|se', $this->ws->read('stack/compose.yaml')));
$hover = $this->nav->hover('stack/compose.yaml', $text, $line, $col);
self::assertNotNull($hover);
self::assertStringContainsString('restart: always', $hover['contents']);
// anchors of another document are not the same
$multi = "a: &x 1\nb: *x\n---\nc: &x 2\nd: *x\n";
[$text, $line, $col] = self::at(str_replace("d: *x", "d: *|x", $multi));
self::assertSame(4, $this->nav->definition('m.yaml', $text, $line, $col)[0]['line']);
}
public function testRenamingAnAnchorRenamesItsAliases(): void
{
[$text, $line, $col] = self::at(str_replace('&base', '&|base', $this->ws->read('stack/compose.yaml')));
self::assertSame('base', $this->nav->renameTarget('stack/compose.yaml', $text, $line, $col)['text'] ?? null);
$edits = $this->nav->rename('stack/compose.yaml', $text, $line, $col, 'common')['edits'];
self::assertSame([[1, 10], [6, 10]], array_map(fn ($e) => [$e['line'], $e['col']], $edits['stack/compose.yaml']));
}
public function testSymfonyImportsResourcesAndExcludesLeadToFiles(): void
{
self::assertSame(['site/config/packages/security.yaml:1', 'site/config/packages/twig.yaml:1'], $this->go('site/config/services.yaml', 'packages/'));
self::assertContains('site/src/Kernel.php:1', $this->go('site/config/services.yaml', "'../src/'", 2));
self::assertSame(['site/src/Kernel.php:1', 'site/src/Entity/User.php:1'], $this->go('site/config/services.yaml', 'Kernel.php', 0));
self::assertSame(['site/src/Controller/HomeController.php:1'], $this->go('site/config/routes.yaml', '../src/Controller/'));
$links = $this->nav->links('site/config/services.yaml', $this->ws->read('site/config/services.yaml'));
$targets = array_column($links, 'path');
self::assertContains('site/src/Mailer/Mailer.php', $targets, 'a class is a link');
self::assertNotContains(2, array_column($links, 'line'), 'a glob of several files is not a link');
self::assertContains('site/src/Log/Logger.php', $targets, 'a class: class:');
self::assertNotContains(9, array_column($links, 'line'), 'a folder is not a link');
}
public function testClassesMethodsAndConstants(): void
{
self::assertSame(['site/src/Mailer/Mailer.php:4'], $this->go('site/config/services.yaml', 'App\\Mailer\\Mailer:'));
self::assertSame(['site/src/Log/Logger.php:4'], $this->go('site/config/services.yaml', 'App\\Log\\Logger'));
self::assertSame(['site/src/Controller/HomeController.php:6'], $this->go('site/config/routes.yaml', '::index', 3));
self::assertSame(['site/src/Mailer/Mailer.php:7'], $this->go('site/config/services.yaml', 'setLogger'));
self::assertSame(['site/src/Log/Logger.php:6'], $this->go('site/config/services.yaml', "'create'"), 'factory: [@service, method]');
self::assertSame(['site/src/Mailer/Mailer.php:6'], $this->go('site/config/services.yaml', '::LIMIT', 3));
}
public function testServicesParametersEnvironmentAndTags(): void
{
self::assertSame(['site/config/services.yaml:17'], $this->go('site/config/services.yaml', '@logger.main', 2));
self::assertSame(['site/config/services.yaml:4'], $this->go('site/config/services.yaml', '%app.locale%', 2));
self::assertSame(['site/.env:2'], $this->go('site/config/services.yaml', 'MAILER_DSN', 1));
self::assertSame(['site/config/services.yaml:16'], $this->go('site/config/services.yaml', 'app.handler,', 1));
self::assertSame(['site/config/packages/security.yaml:3'], $this->go('site/config/packages/security.yaml', 'provider: users', 11));
self::assertSame(['site/config/services.yaml:4', 'site/config/services.yaml:12'], $this->uses('site/config/services.yaml', 'app.locale:', 1));
self::assertContains('site/config/services.yaml:15', $this->uses('site/config/services.yaml', 'logger.main:', 1));
self::assertSame(['site/templates/base.html.twig:1'], $this->go('site/config/packages/twig.yaml', 'templates', 0), 'a folder: its files');
}
public function testTranslationKeysBothWays(): void
{
self::assertSame(['site/translations/messages.en.yaml:1', 'site/translations/messages.fr.yaml:2'],
$this->go('site/templates/base.html.twig', 'home.title'));
self::assertSame(['site/translations/messages.en.yaml:1', 'site/translations/messages.fr.yaml:2'],
$this->go('site/src/Controller/HomeController.php', 'home.title'));
$uses = $this->uses('site/translations/messages.fr.yaml', 'title', 1, false);
self::assertSame(['site/templates/base.html.twig:1', 'site/src/Controller/HomeController.php:6'], $uses);
[$text, $line, $col] = self::at("<h1>{{ 'home.ti|tle'|trans }}</h1>\n");
self::assertStringContainsString('Accueil', $this->nav->hover('site/templates/base.html.twig', $text, $line, $col)['contents'] ?? '');
}
public function testComposeNamesAndPaths(): void
{
$f = 'stack/compose.yaml';
self::assertSame(['stack/compose.yaml:16'], $this->go($f, '[db]'));
self::assertSame(['stack/compose.yaml:5'], $this->go($f, 'service: web', 10));
self::assertSame(['stack/compose.yaml:24'], $this->go($f, '- data:', 3));
self::assertSame(['stack/compose.yaml:22'], $this->go($f, '- front', 3));
self::assertSame(['stack/.env:1'], $this->go($f, 'env_file: .env', 11));
self::assertSame(['stack/docker/web.Dockerfile:1'], $this->go($f, 'web.Dockerfile'));
self::assertSame(['stack/app/index.php:1'], $this->go($f, './app'));
self::assertSame(['stack/compose.yaml:10', 'stack/compose.yaml:16'], $this->uses($f, ' db:', 3));
$edits = $this->nav->rename($f, ...[...array_slice(self::at(str_replace(' db:', ' d|b:', $this->ws->read($f))), 0, 3), 'database'])['edits'][$f];
self::assertCount(2, $edits);
}
public function testJumphpProjectInputLangAndForms(): void
{
self::assertSame(['games/g/src/Scene/TitleScene.php:4'], $this->go('games/g/config/project.yaml', 'App\\Scene\\TitleScene'));
self::assertSame(['games/g/src/Scene/PlayScene.php:4'], $this->go('games/g/config/project.yaml', 'App\\Scene\\PlayScene'));
self::assertSame(['games/g/assets/icon.png:1'], $this->go('games/g/config/project.yaml', 'assets/icon'));
self::assertSame(['games/g/src/Scene/PlayScene.php:6'], $this->uses('games/g/config/input.yaml', 'jump', 1, false));
self::assertSame(['games/g/config/input.yaml:2'], $this->go('games/g/src/Scene/PlayScene.php', "'jump'"));
self::assertSame(['games/g/config/lang.yaml:2', 'games/g/config/lang.yaml:4'], $this->go('games/g/src/Scene/TitleScene.php', "Lang::t('PLAY", 8));
self::assertSame(['games/g/config/lang.yaml:2', 'games/g/config/lang.yaml:4'], $this->go('games/g/src/Scene/TitleScene.php', "['PLAY'", 3), 'a menu label');
$uses = $this->uses('games/g/config/lang.yaml', 'PLAY', 1, false);
self::assertContains('games/g/forms/MainForm.form.yaml:3', $uses);
self::assertContains('games/g/src/Scene/TitleScene.php:7', $uses);
self::assertSame(['games/g/src/Form/MainForm.php:6'], $this->go('games/g/forms/MainForm.form.yaml', 'onSaveClick'));
self::assertSame(['.github/actions/setup/action.yml:1'], $this->go('.github/workflows/ci.yml', './.github', 2));
}
public function testTheIdeCommands(): void
{
$ide = new Ide($this->ws);
foreach (['yaml.definition', 'yaml.references', 'yaml.hover', 'yaml.links', 'yaml.rename'] as $command) {
self::assertTrue($ide->has($command), $command);
}
$text = $this->ws->read('stack/compose.yaml');
self::assertSame(1, $ide->handle('yaml.definition', ['path' => 'stack/compose.yaml', 'text' => $text, 'line' => 6, 'column' => 11])[0]['line']);
}
}