*/ public const VIEWPORTS = [ 'desktop' => ['desktop', 'desktop'], 'tablet' => ['iPadPro', 'tablet'], 'mobile' => ['iPhone15', 'mobile'], ]; /** * The engine this run is driving. * * Set by the test:visual composer script. Playwright::defaultBrowserType() * would report the same thing but is marked @internal. */ public static function browser(): string { return getenv('VANGUARD_BROWSER') ?: 'chrome'; } public static function dir(): string { return base_path('tests/Browser/Gallery'); } /** * The filename a capture is written under, before it is collected. */ public static function filename(string $page, string $viewport): string { return self::PREFIX.$page.'__'.$viewport; } /** * Move this run's captures into the gallery, then rebuild the index. */ public static function collect(): void { $target = self::dir().'/'.self::browser(); if (! is_dir($target)) { mkdir($target, 0755, true); } foreach (glob(base_path('tests/Browser/Screenshots/'.self::PREFIX.'*.png')) ?: [] as $capture) { rename($capture, $target.'/'.str_replace(self::PREFIX, '', basename($capture))); } self::buildIndex(); } /** * Write the browsable index over everything captured so far. * * Reads the directory rather than tracking state, so the index stays * complete when only one browser is re-run. */ public static function buildIndex(): void { $browsers = array_map('basename', array_filter(glob(self::dir().'/*') ?: [], 'is_dir')); sort($browsers); $pages = []; foreach ($browsers as $browser) { foreach (glob(self::dir().'/'.$browser.'/*.png') ?: [] as $image) { [$page, $viewport] = array_pad(explode('__', basename($image, '.png'), 2), 2, 'desktop'); $pages[$page][$viewport][$browser] = $browser.'/'.basename($image); } } ksort($pages); file_put_contents(self::dir().'/index.html', self::render($pages, $browsers)); } /** * @param array>> $pages * @param array $browsers */ private static function render(array $pages, array $browsers): string { $generated = date('Y-m-d H:i'); $engines = implode(', ', $browsers); $count = array_sum(array_map(fn (array $viewports): int => array_sum(array_map('count', $viewports)), $pages)); $body = ''; foreach ($pages as $page => $viewports) { $title = htmlspecialchars(str_replace('-', ' ', $page)); $body .= "

{$title}

"; foreach (array_keys(self::VIEWPORTS) as $viewport) { if (! isset($viewports[$viewport])) { continue; } $body .= '

'.htmlspecialchars($viewport).'

'; foreach ($browsers as $browser) { $src = $viewports[$viewport][$browser] ?? null; $body .= '
'.($src === null ? '
not captured
' : '' .'') .'
'.htmlspecialchars($browser).'
'; } $body .= '
'; } $body .= '
'; } return << Vanguard UI gallery

Vanguard UI gallery

{$count} captures · {$engines} · generated {$generated}

{$body} HTML; } }