160 lines
9.1 KiB
PHP
160 lines
9.1 KiB
PHP
<?php
|
|
|
|
namespace JumPHP\Ide\Tests;
|
|
|
|
use JumPHP\Ide\Composer\ComposerProject;
|
|
use JumPHP\Ide\Web\PhpVersions;
|
|
use JumPHP\Ide\Web\WebCommands;
|
|
use JumPHP\Ide\Web\WebProject;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/** A web site's PHP version (PhpVersions, tools/web-php.php), its .jumphp/web.json (WebProject) and the changes (WebCommands). */
|
|
final class WebPhpTest extends TestCase
|
|
{
|
|
private string $dir;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->dir = sys_get_temp_dir() . '/jumphp-webphp-' . bin2hex(random_bytes(4));
|
|
mkdir($this->dir, 0777, true);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
exec('rm -rf ' . escapeshellarg($this->dir));
|
|
}
|
|
|
|
/** @return list<string> */
|
|
private static function allowed(string $constraint): array
|
|
{
|
|
return array_values(array_filter(PhpVersions::AVAILABLE, fn ($v) => PhpVersions::allows($constraint, $v)));
|
|
}
|
|
|
|
public function testComposerConstraintsAllowTheRightPhpVersions(): void
|
|
{
|
|
self::assertSame(['8.5', '8.4', '8.3', '8.2'], self::allowed('^8.2'));
|
|
self::assertSame(['8.3', '8.2'], self::allowed('>=8.1 <8.4'));
|
|
self::assertSame(['8.3', '8.2'], self::allowed('>= 8.1.0, < 8.4'));
|
|
self::assertSame(['8.3'], self::allowed('~8.3.0'));
|
|
self::assertSame(['8.5', '8.4', '8.3', '8.2'], self::allowed('~8.1'));
|
|
self::assertSame(['8.4'], self::allowed('8.4.*'));
|
|
self::assertSame(['8.3', '8.2'], self::allowed('8.2 || 8.3'));
|
|
self::assertSame(['8.5', '8.4', '8.3', '8.2'], self::allowed('^7.4|^8.0'));
|
|
self::assertSame(['8.5'], self::allowed('>=8.5.12'));
|
|
self::assertSame(['8.3', '8.2'], self::allowed('8.2 - 8.3'));
|
|
self::assertSame(['8.3', '8.2'], self::allowed('<=8.3'));
|
|
self::assertSame([], self::allowed('^7.4'));
|
|
self::assertSame(['8.5', '8.4', '8.3', '8.2'], self::allowed('*'));
|
|
}
|
|
|
|
public function testTheVersionIsTheNewestTheLockedPackagesAllow(): void
|
|
{
|
|
$r = PhpVersions::resolve(['require' => ['php' => '>=8.2']], ['packages' => [
|
|
['name' => 'a/old', 'require' => ['php' => '>=8.1 <8.4']],
|
|
['name' => 'b/new', 'require' => ['php' => '>=8.2']],
|
|
]]);
|
|
self::assertSame('8.3', $r['version']);
|
|
self::assertSame('composer', $r['source']);
|
|
self::assertStringContainsString('a/old php >=8.1 <8.4', $r['detail']);
|
|
// a package that needs the newest PHP (what made testSF's console fail on 8.4)
|
|
$r = PhpVersions::resolve(['require' => ['php' => '>=8.4']], ['packages-dev' => [['name' => 'x/y', 'require' => ['php' => '>=8.5']]]]);
|
|
self::assertSame('8.5', $r['version']);
|
|
}
|
|
|
|
public function testThePlatformOfComposerConfigWins(): void
|
|
{
|
|
$r = PhpVersions::resolve(['require' => ['php' => '>=8.1'], 'config' => ['platform' => ['php' => '8.2.15']]], null);
|
|
self::assertSame(['8.2', 'platform'], [$r['version'], $r['source']]);
|
|
$r = PhpVersions::resolve(null, ['platform-overrides' => ['php' => '8.3.0']]);
|
|
self::assertSame('8.3', $r['version']);
|
|
}
|
|
|
|
public function testNothingSaysThenTheDefaultAndAConflictIsReported(): void
|
|
{
|
|
self::assertSame(['8.4', 'default'], array_values(array_intersect_key(PhpVersions::resolve(null, null), ['version' => 1, 'source' => 1])));
|
|
self::assertSame(PhpVersions::DEFAULT, PhpVersions::resolve(['require' => ['ext-gd' => '*']], null)['version']);
|
|
$r = PhpVersions::resolve(['require' => ['php' => '>=8.5']], ['packages' => [['name' => 'a/b', 'require' => ['php' => '<8.3']]]]);
|
|
self::assertTrue($r['conflict']);
|
|
self::assertStringContainsString('a/b', $r['detail']);
|
|
}
|
|
|
|
public function testTheResolverReadsAFolder(): void
|
|
{
|
|
file_put_contents("$this->dir/composer.json", '{"require": {"php": "^8.2"}}');
|
|
file_put_contents("$this->dir/composer.lock", '{"packages": [{"name": "s/s", "require": {"php": ">=8.2 <8.5"}}]}');
|
|
self::assertSame('8.4', PhpVersions::resolveFolder($this->dir)['version']);
|
|
// the script tools/web.sh runs in a throwaway container prints the same
|
|
exec('php ' . escapeshellarg(dirname(__DIR__, 2) . '/tools/web-php.php') . ' ' . escapeshellarg($this->dir), $out);
|
|
self::assertStringStartsWith("8.4\tcomposer: ", $out[0] ?? '');
|
|
}
|
|
|
|
public function testWebJsonKeepsTheOtherKeysAndStaysReadableByWebSh(): void
|
|
{
|
|
file_put_contents("$this->dir/x", '');
|
|
mkdir("$this->dir/.jumphp");
|
|
file_put_contents("$this->dir/.jumphp/web.json", "{\n \"kind\": \"symfony\",\n \"db\": \"mariadb\"\n}\n");
|
|
WebProject::set($this->dir, ['php' => '8.5', 'runner' => 'local', 'local_php' => '/opt/php/bin/php']);
|
|
self::assertSame('{"kind": "symfony", "db": "mariadb", "php": "8.5", "runner": "local", "local_php": "/opt/php/bin/php"}' . "\n", file_get_contents("$this->dir/.jumphp/web.json"));
|
|
self::assertSame('local', WebProject::runner($this->dir));
|
|
WebProject::set($this->dir, ['runner' => null, 'php' => '']);
|
|
self::assertSame(['kind' => 'symfony', 'db' => 'mariadb', 'local_php' => '/opt/php/bin/php'], WebProject::read($this->dir));
|
|
self::assertSame('docker', WebProject::runner($this->dir));
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
WebProject::set($this->dir, ['php' => 'a"b']);
|
|
}
|
|
|
|
public function testTheSetupAndThePhpOfASite(): void
|
|
{
|
|
file_put_contents("$this->dir/composer.json", '{"require": {"php": ">=8.4"}}');
|
|
self::assertSame('jumphp', WebProject::setup($this->dir));
|
|
self::assertSame(['8.5', 'composer'], [WebProject::php($this->dir)['version'], WebProject::php($this->dir)['source']]);
|
|
// symfony-docker's files: its Dockerfile's PHP
|
|
mkdir("$this->dir/frankenphp");
|
|
file_put_contents("$this->dir/compose.yaml", "services:\n php:\n build: .\n database:\n image: postgres:16\n");
|
|
file_put_contents("$this->dir/Dockerfile", "#syntax=docker/dockerfile:1\nFROM dunglas/frankenphp:1-php8.3 AS frankenphp_upstream\n");
|
|
self::assertSame('dunglas', WebProject::setup($this->dir));
|
|
self::assertSame(['8.3', 'Dockerfile'], [WebProject::php($this->dir)['version'], WebProject::php($this->dir)['source']]);
|
|
// web.json says otherwise
|
|
WebProject::set($this->dir, ['docker' => 'compose', 'service' => 'php']);
|
|
self::assertSame('compose', WebProject::setup($this->dir));
|
|
self::assertNull(WebProject::php($this->dir)['version']);
|
|
WebProject::set($this->dir, ['php' => '8.4']);
|
|
self::assertSame(['8.4', 'web.json'], [WebProject::php($this->dir)['version'], WebProject::php($this->dir)['source']]);
|
|
$s = WebCommands::settings($this->dir);
|
|
self::assertSame(['php', 'database'], $s['services']);
|
|
self::assertSame(['compose', 'compose.yaml', '8.5'], [$s['setup'], $s['composeFile'], $s['auto']]);
|
|
}
|
|
|
|
public function testComposeServicesAreTheKeysUnderServices(): void
|
|
{
|
|
$yaml = "name: x\nservices:\n # the site\n app:\n image: php:8.3-fpm\n environment:\n A: b\n db:\n image: mariadb\nvolumes:\n data:\n";
|
|
self::assertSame(['app', 'db'], WebProject::composeServices($yaml));
|
|
}
|
|
|
|
public function testChangesRunWebPhpAndWebDockerOfTheToolkit(): void
|
|
{
|
|
self::assertSame(['@jumphp', 'web:php', '/s', '8.5'], WebCommands::config('/s', 'php', ['version' => '8.5'])['command']);
|
|
self::assertSame(['@jumphp', 'web:php', '/s', 'auto'], WebCommands::config('/s', 'php', ['version' => 'auto'])['command']);
|
|
self::assertTrue(WebCommands::config('/s', 'php', ['version' => '8.5'])['toolkit']);
|
|
self::assertSame(['@jumphp', 'web:docker', '/s', 'dunglas'], WebCommands::config('/s', 'docker', ['setup' => 'dunglas', 'service' => 'x'])['command']);
|
|
self::assertSame(['@jumphp', 'web:docker', '/s', 'compose', 'php'], WebCommands::config('/s', 'docker', ['setup' => 'compose', 'service' => 'php'])['command']);
|
|
self::assertSame(['@jumphp', 'web:docker', '/s', 'compose', 'app', 'docker/compose.yml'],
|
|
WebCommands::config('/s', 'docker', ['setup' => 'compose', 'service' => 'app', 'file' => 'docker/compose.yml'])['command']);
|
|
foreach ([['php', ['version' => '7.4']], ['docker', ['setup' => 'k8s']], ['docker', ['setup' => 'compose', 'service' => 'a;rm']]] as [$what, $p]) {
|
|
try {
|
|
WebCommands::config('/s', $what, $p);
|
|
self::fail("accepted $what " . json_encode($p));
|
|
} catch (\InvalidArgumentException) {
|
|
self::assertTrue(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function testComposerUsesTheLocalPhpWhenTheSiteRunsLocally(): void
|
|
{
|
|
file_put_contents("$this->dir/composer.json", '{"require": {}}');
|
|
$p = new ComposerProject($this->dir, [], 'native', ['/opt/php/bin/php', '/usr/local/bin/composer']);
|
|
self::assertSame(['/opt/php/bin/php', '/usr/local/bin/composer', 'install', '--no-interaction'], $p->config('', ['install', '--no-interaction'])['command']);
|
|
self::assertSame(['composer', 'install'], (new ComposerProject($this->dir, [], 'native'))->config('', ['install'])['command']);
|
|
}
|
|
}
|