winzik/engine/tests/Cryo/HotPatchTest.php

93 lines
4.1 KiB
PHP

<?php
namespace JumPHP\Tests\Cryo;
use JumPHP\Cryo\HotPatch;
use PHPUnit\Framework\TestCase;
/** a class of "the game" whose code is patched while it runs */
final class PatchedHero
{
public const SPEED = 2;
public int $x = 0;
public function step(): int
{
return $this->x += 1;
}
}
/** Hot patches with Cryo\Engine (the IDE's Cryo window): a method and a constant changed in the running program, undone. */
final class HotPatchTest extends TestCase
{
protected function setUp(): void
{
if (($why = HotPatch::unsupported()) !== null) {
self::markTestSkipped($why);
}
}
protected function tearDown(): void
{
if (HotPatch::unsupported() === null) {
HotPatch::handle(['action' => 'revert']);
}
}
public function testAMethodIsReplacedWhileTheObjectKeepsItsStateThenRestored(): void
{
$hero = new PatchedHero();
$hero->step();
$answer = HotPatch::handle(['id' => 'a', 'action' => 'method', 'class' => PatchedHero::class, 'method' => 'step',
'code' => 'function (): int { return $this->x += 10; }']);
self::assertTrue($answer['ok'], $answer['error'] ?? '');
self::assertSame(11, $hero->step(), 'the same object, its state kept, the new code');
self::assertNotEmpty($answer['journal']);
self::assertTrue(HotPatch::handle(['action' => 'revert', 'change' => $answer['change']])['ok']);
self::assertSame(12, $hero->step(), 'the original code is back');
}
public function testAConstantIsChanged(): void
{
$answer = HotPatch::handle(['action' => 'constant', 'constant' => PatchedHero::class . '::SPEED', 'value' => 5]);
self::assertTrue($answer['ok'], $answer['error'] ?? '');
self::assertSame(5, constant(PatchedHero::class . '::SPEED'));
}
public function testBadPatchesAreRefusedWithAReason(): void
{
$syntax = HotPatch::handle(['action' => 'method', 'class' => PatchedHero::class, 'method' => 'step', 'code' => 'function () { return $this->x +; }']);
self::assertFalse($syntax['ok']);
self::assertStringContainsString('syntax error', $syntax['error']);
$missing = HotPatch::handle(['action' => 'method', 'class' => PatchedHero::class, 'method' => 'fly', 'code' => 'function () {}']);
self::assertStringContainsString('does not exist', $missing['error']);
self::assertSame(1, (new PatchedHero())->step(), 'nothing changed');
}
public function testTheConsoleRunsPhpInTheProgramAndKeepsItsVariables(): void
{
$first = HotPatch::handle(['action' => 'eval', 'code' => '$hero = new \JumPHP\Tests\Cryo\PatchedHero(); $hero->step();']);
self::assertTrue($first['ok'], $first['error'] ?? '');
$second = HotPatch::handle(['action' => 'eval', 'code' => '$hero->x + 41']);
self::assertSame('42', $second['result']);
self::assertSame('10', HotPatch::handle(['action' => 'eval', 'code' => '$n = 5; $n * 2'])['result'], 'the last expression is shown');
self::assertSame('', HotPatch::handle(['action' => 'eval', 'code' => 'if (true) { $m = 1; }'])['result']);
$echo = HotPatch::handle(['action' => 'eval', 'code' => 'echo "hi"']);
self::assertSame('hi', $echo['output']);
$error = HotPatch::handle(['action' => 'eval', 'code' => 'nope_nope()']);
self::assertFalse($error['ok']);
self::assertStringContainsString('nope_nope', $error['error']);
}
public function testCompletionsComeFromTheRealObjects(): void
{
HotPatch::handle(['action' => 'eval', 'code' => '$hero = new \JumPHP\Tests\Cryo\PatchedHero()']);
$labels = fn (string $text) => array_column(HotPatch::handle(['action' => 'complete', 'text' => $text])['items'], 'label');
self::assertContains('step', $labels('$hero->st'));
self::assertContains('x', $labels('$hero->'));
self::assertContains('$hero', $labels('$he'));
self::assertContains('SPEED', $labels('\JumPHP\Tests\Cryo\PatchedHero::SP'));
self::assertContains('str_replace', $labels('str_rep'));
}
}