rob-revisited/ide/tests/ChromeExtensionTest.php

59 lines
2.6 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Project\ProjectInfo;
use JumPHP\Ide\Workspace\LocalWorkspace;
use PHPUnit\Framework\TestCase;
/** A Chrome extension (manifest.json, Manifest V3, at the root): found, and its Run menu through the IDE's copy of JumPHP (tools/chrome.sh). */
final class ChromeExtensionTest extends TestCase
{
private string $dir;
protected function setUp(): void
{
$this->dir = sys_get_temp_dir() . '/jumphp-chrome-' . bin2hex(random_bytes(4));
mkdir($this->dir);
}
protected function tearDown(): void
{
exec('rm -rf ' . escapeshellarg($this->dir));
}
private function info(): ProjectInfo
{
return new ProjectInfo(new LocalWorkspace($this->dir));
}
public function testAManifestV3FolderIsAChromeExtensionWithItsRunConfigurations(): void
{
file_put_contents("$this->dir/manifest.json", "{\n \"manifest_version\": 3,\n \"name\": \"X\",\n \"version\": \"1.0\"\n}\n");
file_put_contents("$this->dir/popup.html", '<p>x</p>');
self::assertTrue($this->info()->isChromeExtension());
$describe = $this->info()->describe();
self::assertSame('chrome', $describe['kind']);
$run = array_column($describe['run'], null, 'id');
foreach (['chrome:check' => 'tool', 'chrome:run' => 'shot', 'chrome:test' => 'test', 'chrome:pack' => 'build', 'chrome:bump:minor' => 'tool'] as $id => $kind) {
self::assertArrayHasKey($id, $run);
self::assertSame($kind, $run[$id]['kind']);
self::assertTrue($run[$id]['toolkit']);
}
self::assertSame(['@jumphp', 'chrome:run', $this->dir], $run['chrome:run']['command']);
self::assertSame(['@jumphp', 'chrome:bump', $this->dir, 'minor'], $run['chrome:bump:minor']['command']);
// not a web site too, even with an index.php beside it
file_put_contents("$this->dir/index.php", '<?php');
self::assertArrayNotHasKey('web:serve', array_column($this->info()->describe()['run'], null, 'id'));
}
public function testAManifestV2OrAnotherManifestIsNot(): void
{
file_put_contents("$this->dir/manifest.json", '{"manifest_version": 2, "name": "old"}');
self::assertFalse($this->info()->isChromeExtension());
file_put_contents("$this->dir/manifest.json", '{"name": "a web app manifest", "start_url": "/"}');
self::assertFalse($this->info()->isChromeExtension());
self::assertSame('php', $this->info()->describe()['kind']);
self::assertArrayNotHasKey('chrome:check', array_column($this->info()->describe()['run'], null, 'id'));
}
}