34 lines
1.8 KiB
PHP
34 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace JumPHP\Ide\Tests;
|
|
|
|
use JumPHP\Ide\Ide;
|
|
use JumPHP\Ide\Workspace\MemoryWorkspace;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/** The Calls window: who calls a method, grouped by the method they are in, and from there who calls the caller. */
|
|
final class CallsTest extends TestCase
|
|
{
|
|
public function testTheCallersOfAMethodAndTheirOwnCallers(): void
|
|
{
|
|
$files = [
|
|
'src/Score.php' => "<?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",
|
|
'src/Game.php' => "<?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",
|
|
];
|
|
$ide = new Ide(new MemoryWorkspace($files));
|
|
for ($i = 0; $i < 500; $i++) {
|
|
$ide->tick(0.05);
|
|
}
|
|
// the name of Score::add (line 6, column of "add")
|
|
$calls = $ide->handle('php.calls', ['path' => 'src/Score.php', 'line' => 6, 'column' => 21]);
|
|
self::assertSame('Score::add()', $calls['target']);
|
|
self::assertSame(3, $calls['count']);
|
|
$byCaller = array_column($calls['callers'], null, 'caller');
|
|
self::assertCount(2, $byCaller['Score::bonus()']['calls'], 'two calls in bonus()');
|
|
self::assertCount(1, $byCaller['Game::hit()']['calls']);
|
|
// one level up: who calls Score::bonus()
|
|
$bonus = $byCaller['Score::bonus()'];
|
|
$up = $ide->handle('php.calls', ['path' => $bonus['path'], 'line' => $bonus['line'], 'column' => $bonus['col']]);
|
|
self::assertSame(['Game::hit()'], array_column($up['callers'], 'caller'));
|
|
}
|
|
}
|