rob-revisited/ide/tests/LibraryRefreshTest.php

131 lines
5.8 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Ide;
use JumPHP\Ide\Workspace\LocalWorkspace;
use PHPUnit\Framework\TestCase;
/** vendor/ changed by a composer require / remove (outside the IDE, or from it): the classes and the diagnostics follow. */
final class LibraryRefreshTest extends TestCase
{
private string $dir;
/** @var list<array{0: string, 1: array}> */
private array $events = [];
protected function setUp(): void
{
$this->dir = sys_get_temp_dir() . '/jumphp-ide-lib-' . bin2hex(random_bytes(4));
mkdir($this->dir);
}
protected function tearDown(): void
{
exec('rm -rf ' . escapeshellarg($this->dir));
}
private const TEXT = "<?php\nnamespace App;\n\nuse ApiPlatform\\Metadata\\ApiResource;\n\n#[ApiResource]\nfinal class Book\n{\n}\n";
private function messages(Ide $ide): array
{
return array_column($ide->handle('php.diagnostics', ['path' => 'src/Book.php', 'text' => self::TEXT]), 'message');
}
private function settle(Ide $ide): void
{
for ($i = 0; $i < 200; $i++) {
$ide->tick(0.05);
}
}
private function install(LocalWorkspace $ws, bool $withApiPlatform, int $second): void
{
$psr4 = "<?php\n\$vendorDir = dirname(__DIR__);\n\$baseDir = dirname(\$vendorDir);\nreturn array(\n";
$installed = [];
if ($withApiPlatform) {
$psr4 .= " 'ApiPlatform\\\\Metadata\\\\' => array(\$vendorDir . '/api-platform/metadata'),\n";
$installed[] = ['name' => 'api-platform/metadata', 'version' => '4.0.0', 'install-path' => '../api-platform/metadata'];
$ws->write('vendor/api-platform/metadata/ApiResource.php', "<?php\nnamespace ApiPlatform\\Metadata;\n\n#[\\Attribute]\nclass ApiResource\n{\n}\n");
} else {
$ws->delete('vendor/api-platform/metadata/ApiResource.php');
}
$psr4 .= " 'App\\\\' => array(\$baseDir . '/src'),\n);\n";
$ws->write('vendor/composer/autoload_psr4.php', $psr4);
$ws->write('vendor/composer/installed.json', json_encode(['packages' => $installed]));
// the tests write within one second: the mtimes must differ like they do between two real composer runs
foreach (['vendor/composer/autoload_psr4.php', 'vendor/composer/installed.json'] as $file) {
touch("$this->dir/$file", 1_700_000_000 + $second);
}
}
public function testAComposerRequireDoneOutsideTheIdeResolvesTheClassAndClearsTheDiagnostic(): void
{
$ws = new LocalWorkspace($this->dir);
$ws->write('composer.json', '{"autoload": {"psr-4": {"App\\\\": "src/"}}}');
$ws->write('src/Book.php', self::TEXT);
$this->install($ws, false, 0);
$ide = new Ide($ws, $this->dir . '/cache', function (string $event, array $payload): void {
$this->events[] = [$event, $payload];
});
$this->settle($ide);
self::assertContains("Undefined class 'ApiPlatform\\Metadata\\ApiResource'", $this->messages($ide));
// composer require api-platform: nobody tells the IDE, the next ticks find it
$this->install($ws, true, 10);
$this->events = [];
$ide->emit('run.exit', ['id' => 1, 'code' => 0]); // the explicit trigger (a Composer window run); the 2 s poll finds it too
$ide->tick(0.05);
self::assertContains('index.libraries', array_column($this->events, 0), 'the page is told the libraries are indexed again');
// between the detection and the end of the indexing no class of the libraries is "undefined" for a moment
self::assertNotContains("Undefined class 'ApiPlatform\\Metadata\\ApiResource'", $this->messages($ide));
$this->settle($ide);
self::assertContains('index.changed', array_column($this->events, 0), 'the open files get their diagnostics again');
self::assertSame([], $this->messages($ide));
self::assertContains('ApiPlatform\\Metadata\\ApiResource', array_column($ide->handle('php.classes', ['query' => 'ApiResource']), 'name'));
// composer remove: the class goes, the diagnostic comes back
$this->install($ws, false, 20);
$ide->emit('run.exit', ['id' => 2, 'code' => 0]);
$this->settle($ide);
self::assertNull($this->findClass($ide));
self::assertContains("Undefined class 'ApiPlatform\\Metadata\\ApiResource'", $this->messages($ide));
}
public function testTheEndOfACommandLooksAtVendorAtOnce(): void
{
$ws = new LocalWorkspace($this->dir);
$ws->write('composer.json', '{}');
$this->install($ws, false, 0);
$emit = null;
$ide = new Ide($ws, null, function (string $event, array $payload) use (&$emit): void {
$this->events[] = [$event, $payload];
});
$this->settle($ide);
$this->install($ws, true, 5);
$this->events = [];
$ide->tick(0.01); // inside the 2 seconds of the last look: nothing yet
self::assertNotContains('index.libraries', array_column($this->events, 0));
$ide->emit('run.exit', ['id' => 1, 'code' => 0]);
$ide->tick(0.01);
self::assertContains('index.libraries', array_column($this->events, 0));
}
public function testRebuildingTheIndexAlsoListsTheLibrariesAgain(): void
{
$ws = new LocalWorkspace($this->dir);
$ws->write('composer.json', '{}');
$this->install($ws, true, 0);
$ide = new Ide($ws, $this->dir . '/cache', fn () => null);
$this->settle($ide);
self::assertNotNull($this->findClass($ide));
$ide->handle('index.rebuild', []);
$this->settle($ide);
self::assertNotNull($this->findClass($ide));
}
private function findClass(Ide $ide): ?array
{
return $ide->index->findClass('ApiPlatform\\Metadata\\ApiResource');
}
}