95 lines
4.7 KiB
PHP
95 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace JumPHP\Ide\Tests;
|
|
|
|
use JumPHP\Ide\Ide;
|
|
use JumPHP\Ide\Php\Analyzer;
|
|
use JumPHP\Ide\Php\Index;
|
|
use JumPHP\Ide\Workspace\LocalWorkspace;
|
|
use JumPHP\Ide\Workspace\MemoryWorkspace;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/** What the editor asks at every key, made cheap (docs/ide.md "Performance"): the same answers as before. */
|
|
final class EditorSpeedTest extends TestCase
|
|
{
|
|
private const SCORE = "<?php\nnamespace App;\n\nfinal class Score\n{\n public function add(int \$n): void\n {\n }\n\n public function bonus(): void\n {\n \$this->add(10);\n \$this->add(20);\n }\n}\n";
|
|
private const GAME = "<?php\nnamespace App;\n\nfinal class Game\n{\n public function __construct(private Score \$score)\n {\n }\n\n public function hit(): void\n {\n \$this->score->add(1);\n \$this->score->bonus();\n }\n}\n";
|
|
|
|
private function ide(): Ide
|
|
{
|
|
$ide = new Ide(new MemoryWorkspace(['src/Score.php' => self::SCORE, 'src/Game.php' => self::GAME]));
|
|
for ($i = 0; $i < 500 && $ide->busy(); $i++) {
|
|
$ide->tick(0.05);
|
|
}
|
|
return $ide;
|
|
}
|
|
|
|
public function testTheLensesOfAFileAreCountedInOneQuestion(): void
|
|
{
|
|
$ide = $this->ide();
|
|
$counts = $ide->handle('php.callCounts', ['path' => 'src/Score.php', 'text' => self::SCORE,
|
|
'places' => [['line' => 6, 'column' => 21], ['line' => 10, 'column' => 21], ['line' => 3, 'column' => 1]]]);
|
|
self::assertSame([3, 1, 0], $counts);
|
|
// the same as one php.calls per lens
|
|
self::assertSame(3, $ide->handle('php.calls', ['path' => 'src/Score.php', 'line' => 6, 'column' => 21])['count']);
|
|
}
|
|
|
|
public function testTheHighlightsStayInTheFile(): void
|
|
{
|
|
$ide = $this->ide();
|
|
$places = $ide->handle('php.highlights', ['path' => 'src/Score.php', 'text' => self::SCORE, 'line' => 12, 'column' => 17]);
|
|
self::assertSame(['src/Score.php'], array_values(array_unique(array_column($places, 'path'))));
|
|
self::assertSame([6, 12, 13], array_column($places, 'line'));
|
|
self::assertTrue($places[0]['declaration']);
|
|
// a change typed in the editor (not saved): its new uses count at once
|
|
$typed = str_replace(" \$this->add(20);\n", " \$this->add(20);\n \$this->add(30);\n", self::SCORE);
|
|
$places = $ide->handle('php.highlights', ['path' => 'src/Score.php', 'text' => $typed, 'line' => 12, 'column' => 17]);
|
|
self::assertSame([6, 12, 13, 14], array_column($places, 'line'));
|
|
// and the other files' counts follow (Index::filesReferencing kept up to date)
|
|
self::assertSame([4], $ide->handle('php.callCounts', ['path' => 'src/Score.php', 'text' => $typed, 'places' => [['line' => 6, 'column' => 21]]]));
|
|
$ide->workspace->write('src/Game.php', str_replace('$this->score->add(1);', '', self::GAME));
|
|
$ide->filesChanged();
|
|
for ($i = 0; $i < 50 && $ide->busy(); $i++) {
|
|
$ide->tick(0.05);
|
|
}
|
|
self::assertSame([3], $ide->handle('php.callCounts', ['path' => 'src/Score.php', 'text' => $typed, 'places' => [['line' => 6, 'column' => 21]]]));
|
|
}
|
|
|
|
public function testTheSameTextIsParsedOnce(): void
|
|
{
|
|
$text = self::GAME . str_repeat("// padding\n", 300);
|
|
Analyzer::forgetMemo();
|
|
[$a] = Analyzer::parse($text);
|
|
[$b] = Analyzer::parse($text);
|
|
self::assertSame($a, $b, 'the tree of the same text is shared');
|
|
self::assertSame(Analyzer::analyzeNow($text), Analyzer::analyze($text));
|
|
[$c] = Analyzer::parse($text . ' ');
|
|
self::assertNotSame($a, $c);
|
|
}
|
|
|
|
public function testAnIndexCachedOnDiskIsNotMadeAgainAtTheNextStart(): void
|
|
{
|
|
$dir = sys_get_temp_dir() . '/jph-index-' . getmypid();
|
|
@mkdir("$dir/src", 0777, true);
|
|
file_put_contents("$dir/src/Score.php", self::SCORE);
|
|
file_put_contents("$dir/src/Game.php", self::GAME);
|
|
touch("$dir/src/Score.php", 1731700000);
|
|
touch("$dir/src/Game.php", 1731700000);
|
|
$cache = "$dir.cache.json";
|
|
try {
|
|
$index = new Index(new LocalWorkspace($dir), $cache);
|
|
self::assertTrue($index->refresh());
|
|
$index->save();
|
|
// the mtime came back from JSON as an int: the file was analyzed again at every start
|
|
$again = new Index(new LocalWorkspace($dir), $cache);
|
|
$again->refresh(0.0);
|
|
self::assertSame(['done' => 2, 'total' => 2], $again->progress(), 'nothing left to analyze');
|
|
} finally {
|
|
@unlink("$dir/src/Score.php");
|
|
@unlink("$dir/src/Game.php");
|
|
@rmdir("$dir/src");
|
|
@rmdir($dir);
|
|
@unlink($cache);
|
|
}
|
|
}
|
|
}
|