rob-revisited/ide/tests/WebProjectTest.php

63 lines
2.4 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Project\ProjectInfo;
use JumPHP\Ide\Run\Runner;
use JumPHP\Ide\Workspace\LocalWorkspace;
use PHPUnit\Framework\TestCase;
/** A PHP site that is not a JumPHP project: its kind found, "Serve the site" through the IDE's copy of JumPHP (tools/web.sh). */
final class WebProjectTest extends TestCase
{
private string $dir;
protected function setUp(): void
{
$this->dir = sys_get_temp_dir() . '/jumphp-web-' . 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 testAWordPressSiteIsServedWithTheToolkit(): void
{
file_put_contents("$this->dir/wp-load.php", '<?php');
self::assertSame('wordpress', $this->info()->webKind());
$run = $this->info()->describe()['run'];
$serve = array_values(array_filter($run, fn ($c) => $c['id'] === 'web:serve'))[0] ?? null;
self::assertNotNull($serve);
self::assertSame(['@jumphp', 'web:serve', $this->dir], $serve['command']);
self::assertTrue($serve['toolkit']);
}
public function testSymfonyAndPlainPhpAndNothing(): void
{
mkdir("$this->dir/bin");
touch("$this->dir/bin/console");
file_put_contents("$this->dir/composer.json", '{"require": {"symfony/framework-bundle": "^7"}}');
self::assertSame('symfony', $this->info()->webKind());
exec('rm -rf ' . escapeshellarg($this->dir) . '/*');
file_put_contents("$this->dir/index.php", '<?php echo 1;');
self::assertSame('php', $this->info()->webKind());
unlink("$this->dir/index.php");
self::assertNull($this->info()->webKind());
}
public function testTheToolkitCommandFetchesJumphpAndRunsWebSh(): void
{
$command = Runner::toolkitCommandFor('/home/me/.jumphp/toolkit', 'https://git.jumphp.net/jumphp/jumPhp.git', ['@jumphp', 'web:serve', '/srv/site']);
self::assertSame(['sh', '-c'], array_slice($command, 0, 2));
self::assertStringContainsString('git clone --depth 1', $command[2]);
self::assertStringContainsString('tools/web.sh', $command[2]);
self::assertStringContainsString("exec \"\$tk/bin/jumphp\" 'web:serve' '/srv/site'", $command[2]);
}
}