191 lines
11 KiB
PHP
191 lines
11 KiB
PHP
<?php
|
|
|
|
namespace JumPHP\Ide\Tests;
|
|
|
|
use JumPHP\Ide\Async\Deferred;
|
|
use JumPHP\Ide\Links\ImportMap;
|
|
use JumPHP\Ide\Links\JsLibraries;
|
|
use JumPHP\Ide\Links\ProjectPaths;
|
|
use JumPHP\Ide\Links\RemoteImage;
|
|
use JumPHP\Ide\Workspace\LocalWorkspace;
|
|
use JumPHP\Ide\Workspace\MemoryWorkspace;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class LinksTest extends TestCase
|
|
{
|
|
private const IMPORTMAP = <<<'PHP'
|
|
<?php
|
|
|
|
/**
|
|
* Returns the importmap for this application.
|
|
*/
|
|
return [
|
|
'app' => [
|
|
'path' => './assets/app.js',
|
|
'entrypoint' => true,
|
|
],
|
|
'@hotwired/stimulus' => [
|
|
'version' => '3.2.2',
|
|
],
|
|
'@symfony/stimulus-bundle' => [
|
|
'path' => './vendor/symfony/stimulus-bundle/assets/dist/loader.js',
|
|
],
|
|
'bootstrap/dist/css/bootstrap.min.css' => [
|
|
'version' => '5.3.3',
|
|
'type' => 'css',
|
|
],
|
|
];
|
|
PHP;
|
|
|
|
private function project(): MemoryWorkspace
|
|
{
|
|
return new MemoryWorkspace([
|
|
'importmap.php' => self::IMPORTMAP,
|
|
'assets/app.js' => "import './bootstrap.js';\nimport { Controller } from '@hotwired/stimulus';\n",
|
|
'assets/bootstrap.js' => "import { startStimulusApp } from '@symfony/stimulus-bundle';\nexport const app = 1;\n",
|
|
'assets/styles/app.css' => 'body {}',
|
|
'assets/images/logo.png' => 'PNG',
|
|
'assets/controllers/hello_controller.js' => "import { Controller } from '@hotwired/stimulus';\n",
|
|
'assets/vendor/@hotwired/stimulus/stimulus.index.js' => 'export class Controller {}',
|
|
'assets/vendor/bootstrap/dist/css/bootstrap.min.css' => '',
|
|
'templates/base.html.twig' => '<html>',
|
|
'templates/home/index.html.twig' => "{% extends 'base.html.twig' %}",
|
|
'templates/bundles/TwigBundle/Exception/error.html.twig' => 'x',
|
|
'public/images/hero.jpg' => 'JPG',
|
|
'package.json' => '{"dependencies": {"lodash": "^4.17.0"}, "devDependencies": {"chart.js": "^4"}}',
|
|
'node_modules/lodash/package.json' => '{"main": "lodash.js"}',
|
|
'node_modules/lodash/lodash.js' => 'module.exports = {};',
|
|
'node_modules/@types/lodash/index.d.ts' => "export function chunk<T>(a: T[], n: number): T[][];\n",
|
|
'node_modules/chart.js/package.json' => '{"types": "dist/types.d.ts", "module": "dist/chart.js"}',
|
|
'node_modules/chart.js/dist/types.d.ts' => "export * from './types/index';\n",
|
|
'node_modules/chart.js/dist/types/index.d.ts' => "export declare class Chart {}\n",
|
|
]);
|
|
}
|
|
|
|
public function testImportmapPhpIsReadWithoutRunningIt(): void
|
|
{
|
|
$map = ImportMap::parse(self::IMPORTMAP);
|
|
self::assertSame(['app', '@hotwired/stimulus', '@symfony/stimulus-bundle', 'bootstrap/dist/css/bootstrap.min.css'], array_keys($map));
|
|
self::assertSame('./assets/app.js', $map['app']['path']);
|
|
self::assertTrue($map['app']['entrypoint']);
|
|
self::assertSame('3.2.2', $map['@hotwired/stimulus']['version']);
|
|
self::assertNull($map['@hotwired/stimulus']['path']);
|
|
self::assertSame('css', $map['bootstrap/dist/css/bootstrap.min.css']['type']);
|
|
self::assertSame([], ImportMap::parse('<?php return [];'));
|
|
self::assertSame(['x'], array_keys(ImportMap::parse("<?php return array('x' => array('path' => 'a.js'));")));
|
|
}
|
|
|
|
public function testTwigTemplateNamesPointToTemplates(): void
|
|
{
|
|
$paths = new ProjectPaths($this->project());
|
|
self::assertSame('templates/base.html.twig', $paths->resolve('templates/home/index.html.twig', 'base.html.twig', 'template'));
|
|
self::assertSame('templates/home/index.html.twig', $paths->resolve('templates/x.html.twig', 'home/index.html.twig', 'template'));
|
|
self::assertSame('templates/bundles/TwigBundle/Exception/error.html.twig', $paths->resolve('templates/x.html.twig', '@Twig/Exception/error.html.twig', 'template'));
|
|
self::assertSame('templates/base.html.twig', $paths->resolve('templates/x.html.twig', '@App/base.html.twig', 'template'));
|
|
self::assertNull($paths->resolve('templates/x.html.twig', 'missing.html.twig', 'template'));
|
|
}
|
|
|
|
public function testRelativeWebAssetAndModulePaths(): void
|
|
{
|
|
$paths = new ProjectPaths($this->project());
|
|
self::assertSame('assets/bootstrap.js', $paths->resolve('assets/app.js', './bootstrap.js', 'module'));
|
|
self::assertSame('assets/styles/app.css', $paths->resolve('assets/app.js', './styles/app.css?v=3', 'relative'));
|
|
self::assertSame('assets/images/logo.png', $paths->resolve('assets/styles/app.css', '../images/logo.png', 'relative'));
|
|
self::assertSame('public/images/hero.jpg', $paths->resolve('templates/base.html.twig', '/images/hero.jpg', 'relative'));
|
|
self::assertSame('assets/images/logo.png', $paths->resolve('templates/base.html.twig', 'images/logo.png', 'asset'));
|
|
self::assertSame('public/images/hero.jpg', $paths->resolve('templates/base.html.twig', 'images/hero.jpg', 'asset'));
|
|
self::assertSame('assets/vendor/@hotwired/stimulus/stimulus.index.js', $paths->resolve('assets/app.js', '@hotwired/stimulus', 'module'));
|
|
self::assertSame('assets/vendor/bootstrap/dist/css/bootstrap.min.css', $paths->resolve('assets/app.js', 'bootstrap/dist/css/bootstrap.min.css', 'module'));
|
|
self::assertSame('node_modules/lodash/lodash.js', $paths->resolve('assets/app.js', 'lodash', 'module'));
|
|
self::assertNull($paths->resolve('assets/app.js', '../../../etc/passwd', 'relative'), 'never out of the project');
|
|
self::assertNull($paths->resolve('assets/app.js', 'https://example.com/x.png', 'relative'), 'a URL is not a project file');
|
|
}
|
|
|
|
public function testCompletionListsFoldersFirstThenFiles(): void
|
|
{
|
|
$paths = new ProjectPaths($this->project());
|
|
$r = $paths->complete('assets/app.js', './', 'module');
|
|
$labels = array_column($r['items'], 'label');
|
|
self::assertSame('', $r['typed']);
|
|
self::assertSame(['controllers/', 'images/', 'styles/', 'vendor/', 'app.js', 'bootstrap.js'], $labels);
|
|
$r = $paths->complete('assets/app.js', './styles/ap', 'module');
|
|
self::assertSame('ap', $r['typed']);
|
|
self::assertSame(['app.css'], array_column($r['items'], 'label'));
|
|
$r = $paths->complete('templates/home/index.html.twig', 'ho', 'template');
|
|
self::assertContains('home/', array_column($r['items'], 'label'));
|
|
self::assertContains('base.html.twig', array_column($r['items'], 'label'));
|
|
$r = $paths->complete('templates/base.html.twig', '/images/', 'relative');
|
|
self::assertSame(['hero.jpg'], array_column($r['items'], 'label'));
|
|
}
|
|
|
|
public function testBareSpecifiersProposeTheImportmapAndNodePackages(): void
|
|
{
|
|
$paths = new ProjectPaths($this->project());
|
|
$r = $paths->complete('assets/app.js', '@hot', 'module');
|
|
$labels = array_column($r['items'], 'label');
|
|
self::assertSame('@hot', $r['typed']);
|
|
self::assertSame(['@hotwired/stimulus', '@symfony/stimulus-bundle', 'app', 'bootstrap/dist/css/bootstrap.min.css', 'chart.js', 'lodash'], $labels, 'importmap.php first, then package.json');
|
|
self::assertContains('@hotwired/stimulus', $labels);
|
|
self::assertContains('lodash', $labels);
|
|
self::assertContains('chart.js', $labels);
|
|
self::assertStringStartsWith('importmap.php · 3.2.2', $r['items'][array_search('@hotwired/stimulus', $labels, true)]['detail']);
|
|
}
|
|
|
|
public function testTheJavaScriptWorkerGetsTheImportedModulesAndPackages(): void
|
|
{
|
|
$ws = $this->project();
|
|
$ws->write('assets/charts.js', "import { chunk } from 'lodash';\nimport { Chart } from 'chart.js';\n");
|
|
$libs = (new JsLibraries($ws, new ProjectPaths($ws)))->collect('assets/app.js', "import './bootstrap.js';\nimport { Controller } from '@hotwired/stimulus';\nimport './charts.js';\n");
|
|
$files = array_column($libs['libs'], 'path');
|
|
self::assertContains('assets/bootstrap.js', $files);
|
|
self::assertContains('assets/vendor/@hotwired/stimulus/stimulus.index.js', $files);
|
|
self::assertContains('node_modules/@types/lodash/index.d.ts', $files, '@types/ when the package has no types');
|
|
self::assertContains('node_modules/chart.js/dist/types.d.ts', $files, 'package.json "types"');
|
|
self::assertContains('node_modules/chart.js/dist/types/index.d.ts', $files, 'what the types import');
|
|
self::assertSame(['assets/vendor/@hotwired/stimulus/stimulus.index.js'], $libs['paths']['@hotwired/stimulus']);
|
|
self::assertFalse($libs['truncated']);
|
|
self::assertSame(['./a', 'e', 'b', 'c', 'd'], JsLibraries::imports("import x from './a';\nimport 'b';\nconst c = await import('c');\nrequire(\"d\");\nexport * from 'e';"));
|
|
}
|
|
|
|
public function testTheListingOfADiskFolderHidesVarAndHiddenFolders(): void
|
|
{
|
|
$dir = sys_get_temp_dir() . '/links-' . uniqid();
|
|
mkdir("$dir/assets/js", 0777, true);
|
|
mkdir("$dir/var/cache", 0777, true);
|
|
mkdir("$dir/.git", 0777, true);
|
|
touch("$dir/assets/app.js");
|
|
try {
|
|
$paths = new ProjectPaths(new LocalWorkspace($dir));
|
|
self::assertSame(['assets/'], array_column($paths->complete('index.html', '', 'relative')['items'], 'label'));
|
|
self::assertSame(['js/', 'app.js'], array_column($paths->complete('index.html', 'assets/', 'relative')['items'], 'label'));
|
|
} finally {
|
|
exec('rm -rf ' . escapeshellarg($dir));
|
|
}
|
|
}
|
|
|
|
public function testRemoteImagesOnlyFromPublicAddressesAndCached(): void
|
|
{
|
|
$calls = [];
|
|
$cache = sys_get_temp_dir() . '/links-img-' . uniqid();
|
|
$fetch = function (string $url) use (&$calls): Deferred {
|
|
$calls[] = $url;
|
|
return Deferred::resolved(['status' => 200, 'body' => "\x89PNG....", 'type' => 'application/octet-stream']);
|
|
};
|
|
$lookup = fn (string $host): array => ['example.com' => ['93.184.215.14'], 'intranet.example' => ['192.168.1.10']][$host] ?? [];
|
|
$images = new RemoteImage($fetch, $lookup, $cache);
|
|
$got = null;
|
|
$images->fetch('https://example.com/logo.png')->then(function ($r) use (&$got) { $got = $r; });
|
|
self::assertSame('data:image/png;base64,' . base64_encode("\x89PNG...."), $got['image']);
|
|
self::assertTrue($got['readOnly']);
|
|
$images->fetch('https://example.com/logo.png');
|
|
self::assertCount(1, $calls, 'the second time from the cache');
|
|
foreach (['http://127.0.0.1/x.png', 'http://localhost/x.png', 'https://intranet.example/x.png', 'http://[::1]/x.png', 'ftp://example.com/x.png',
|
|
'http://169.254.169.254/latest', 'http://10.0.0.1/x.png', 'file:///etc/passwd', 'https://user:[email protected]/x.png'] as $bad) {
|
|
$error = null;
|
|
$images->fetch($bad)->then(null, function ($e) use (&$error) { $error = $e; });
|
|
self::assertInstanceOf(\InvalidArgumentException::class, $error, $bad);
|
|
}
|
|
self::assertCount(1, $calls);
|
|
exec('rm -rf ' . escapeshellarg($cache));
|
|
}
|
|
}
|