rob-revisited/ide/tests/LiveReloadTest.php

138 lines
6.3 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Web\LiveReload;
use JumPHP\Ide\Web\WebProject;
use PHPUnit\Framework\TestCase;
/** The live reload of a served web site: the IDE tells the reload container each saved file and reads how many tabs follow. */
final class LiveReloadTest extends TestCase
{
private string $dir;
/** @var list<array{string, string, ?string}> */
private array $calls = [];
private float $now = 100.0;
protected function setUp(): void
{
$this->dir = sys_get_temp_dir() . '/jumphp-live-' . bin2hex(random_bytes(4));
mkdir($this->dir);
WebProject::set($this->dir, ['kind' => 'symfony', 'live_port' => '8931']);
}
protected function tearDown(): void
{
exec('rm -rf ' . escapeshellarg($this->dir));
}
/** @param (callable(string, string, ?string): ?string)|null $answer */
private function live(?callable $answer = null): LiveReload
{
$answer ??= static fn (): string => '{"ok":true}';
return new LiveReload($this->dir, function (string $m, string $u, ?string $b) use ($answer): ?string {
$this->calls[] = [$m, $u, $b];
return $answer($m, $u, $b);
}, fn (): float => $this->now);
}
public function testASaveIsToldToTheReloadContainerOnTheLiveReloadPort(): void
{
self::assertTrue($this->live()->filesChanged(['templates/base.html.twig']));
self::assertSame(['POST', 'http://127.0.0.1:8931/__jumphp/notify', '{"files":["templates/base.html.twig"]}'], $this->calls[0]);
}
public function testFilesThatChangeNothingOnThePageAreNotTold(): void
{
$live = $this->live();
self::assertFalse($live->filesChanged(['vendor/symfony/x.php', 'var/cache/dev/a.php', 'node_modules/a.js', '.git/config', 'README.md', '.jumphp/live.js']));
self::assertSame([], $this->calls);
self::assertTrue(LiveReload::relevant('assets/app.scss'));
self::assertTrue(LiveReload::relevant('.env.local'));
self::assertTrue(LiveReload::relevant('config\\services.yaml'));
self::assertTrue(LiveReload::relevant('translations/messages.fr.xlf'));
}
public function testASiteThatIsNotRunningIsNotWaitedForAtEachSave(): void
{
$live = $this->live(static fn (): ?string => null);
self::assertFalse($live->filesChanged(['a.php']));
self::assertFalse($live->filesChanged(['b.php']));
self::assertCount(1, $this->calls, 'the second save does not try again within 5 seconds');
$this->now += 6;
$live->filesChanged(['c.php']);
self::assertCount(2, $this->calls);
}
public function testOffSendsNothingAndTheSettingIsKeptInWebJson(): void
{
$live = $this->live();
self::assertTrue($live->enabled());
$live->setEnabled(false);
self::assertSame('off', WebProject::get($this->dir, 'live'));
self::assertSame('symfony', WebProject::get($this->dir, 'kind'), 'the other keys stay');
self::assertFalse($live->filesChanged(['a.php']));
self::assertSame([], $this->calls);
self::assertSame(['enabled' => false, 'running' => false, 'clients' => 0], array_intersect_key($live->status(), ['enabled' => 1, 'running' => 1, 'clients' => 1]));
$live->setEnabled(true);
self::assertNull(WebProject::get($this->dir, 'live'));
self::assertTrue($live->filesChanged(['a.php']));
}
public function testTheStatusCountsTheBrowserTabsAndGivesTheLiveReloadAddress(): void
{
$live = $this->live(static fn (string $m, string $u): string => '{"ok":true,"clients":3,"reloads":5,"injects":2,"last":1.5,"poll":true}');
$s = $live->status();
self::assertTrue($s['running']);
self::assertSame(3, $s['clients']);
self::assertSame('http://localhost:8931', $s['url'], 'the live-reload address, never the raw PHP port 8932');
self::assertTrue($s['poll']);
self::assertSame(['GET', 'http://127.0.0.1:8931/__jumphp/status', null], $this->calls[0]);
}
public function testTheStatusOfASiteThatIsDownOrAnswersNonsense(): void
{
self::assertFalse($this->live(static fn (): ?string => null)->status()['running']);
self::assertFalse($this->live(static fn (): string => '<html>')->status()['running']);
}
public function testNoPortNoNotice(): void
{
WebProject::set($this->dir, ['live_port' => null]);
$live = $this->live();
self::assertNull($live->url());
self::assertFalse($live->filesChanged(['a.php']));
self::assertFalse($live->status()['running']);
}
public function testTheRegisteredCommandsAnswerAndToggle(): void
{
$handlers = [];
LiveReload::register(function (string $n, callable $h) use (&$handlers): void { $handlers[$n] = $h; }, $this->live(static fn (): string => '{"ok":true,"clients":1}'));
self::assertSame(1, $handlers['web.live']()['clients']);
self::assertFalse($handlers['web.live.set'](['enabled' => false])['enabled']);
self::assertTrue($handlers['web.live.set'](['enabled' => true])['enabled']);
}
public function testARealNoticeReachesARealServer(): void
{
// the PHP stream client the IDE uses, against a PHP server standing in for live.js
$router = $this->dir . '/router.php';
file_put_contents($router, '<?php if ($_SERVER["REQUEST_URI"] === "/__jumphp/status") { echo json_encode(["ok" => true, "clients" => 2]); return; }'
. ' file_put_contents(__DIR__ . "/got.json", file_get_contents("php://input")); echo "{\"ok\":true}";');
$port = random_int(20000, 40000);
$proc = proc_open([PHP_BINARY, '-S', "127.0.0.1:$port", $router], [1 => ['file', '/dev/null', 'w'], 2 => ['file', '/dev/null', 'w']], $pipes);
try {
for ($i = 0; $i < 30 && !@fsockopen('127.0.0.1', $port); $i++) {
usleep(100000);
}
WebProject::set($this->dir, ['live_port' => (string)$port]);
$live = new LiveReload($this->dir);
self::assertSame(2, $live->status()['clients']);
self::assertTrue($live->filesChanged(['src/Controller/HomeController.php']));
self::assertSame('{"files":["src/Controller/HomeController.php"]}', file_get_contents($this->dir . '/got.json'));
} finally {
proc_terminate($proc);
}
}
}