mirror of
https://github.com/MarijnDoeve/TijdVoorDeTest.git
synced 2026-07-04 22:50:15 +02:00
Add missing quiz/nav.html.twig and test for template reference integrity (#166)
Adds the previously uncommitted quiz nav partial that broke main, and introduces TemplateReferencesTest which scans all Twig templates for extends/include/embed references and asserts each target file exists — preventing this class of missing-template mistake from reaching CI undetected.
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
{% if is_granted('IS_AUTHENTICATED') or app.current_route() == 'tvdt_quiz_select_season' %}
|
||||
<div class="quiz-topbar">
|
||||
{% if is_granted('IS_AUTHENTICATED') %}
|
||||
<a href="{{ path('tvdt_backoffice_index') }}" class="btn btn-outline-secondary btn-sm">
|
||||
{{ 'Backoffice'|trans }}
|
||||
</a>
|
||||
<a href="{{ path('tvdt_login_logout') }}" class="btn btn-outline-secondary btn-sm">
|
||||
{{ 'Logout'|trans }}
|
||||
</a>
|
||||
{% else %}
|
||||
<a href="{{ path('tvdt_backoffice_index') }}" class="btn btn-outline-secondary btn-sm">
|
||||
{{ 'Manage Quiz'|trans }}
|
||||
</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Tests\Twig;
|
||||
|
||||
use PHPUnit\Framework\Assert;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use function Safe\file_get_contents;
|
||||
use function Safe\preg_match_all;
|
||||
|
||||
final class TemplateReferencesTest extends TestCase
|
||||
{
|
||||
private static string $templatesDir;
|
||||
|
||||
public static function setUpBeforeClass(): void
|
||||
{
|
||||
self::$templatesDir = \dirname(__DIR__, 2).'/templates';
|
||||
}
|
||||
|
||||
/** @return iterable<string, array{string, string}> */
|
||||
public static function templateReferenceProvider(): iterable
|
||||
{
|
||||
$templatesDir = \dirname(__DIR__, 2).'/templates';
|
||||
$iterator = new \RecursiveIteratorIterator(
|
||||
new \RecursiveDirectoryIterator($templatesDir, \RecursiveDirectoryIterator::SKIP_DOTS),
|
||||
);
|
||||
|
||||
foreach ($iterator as $file) {
|
||||
Assert::assertInstanceOf(\SplFileInfo::class, $file);
|
||||
if ('twig' !== $file->getExtension()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$content = file_get_contents($file->getPathname());
|
||||
$sourceFile = str_replace($templatesDir.'/', '', $file->getPathname());
|
||||
|
||||
// Match extends, include(), and embed tags — capture the quoted template name
|
||||
preg_match_all(
|
||||
'/(?:extends|include|embed)\s*\(?[\'"]([^\'"]+)[\'"]\)?/',
|
||||
$content,
|
||||
$matches,
|
||||
);
|
||||
|
||||
foreach ($matches[1] as $referencedTemplate) {
|
||||
yield \sprintf('%s → %s', $sourceFile, $referencedTemplate) => [$sourceFile, $referencedTemplate];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[DataProvider('templateReferenceProvider')]
|
||||
public function testReferencedTemplateExists(string $sourceFile, string $referencedTemplate): void
|
||||
{
|
||||
$absolutePath = self::$templatesDir.'/'.$referencedTemplate;
|
||||
|
||||
$this->assertFileExists(
|
||||
$absolutePath,
|
||||
\sprintf("Template '%s' references '%s' which does not exist at '%s'.", $sourceFile, $referencedTemplate, $absolutePath),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user