Added extra tests

This commit is contained in:
Jaapio
2025-06-19 00:06:01 +02:00
parent bfdbbaa035
commit d99cec1d16
3 changed files with 59 additions and 7 deletions

View File

@@ -9,7 +9,8 @@ use Highlight\Highlighter;
use function sprintf;
use function trim;
final readonly class CodeBlockLanguageDetector
/** @final */
readonly class CodeBlockLanguageDetector
{
/**
* We use some language aliases not supported by our highlighter library

View File

@@ -18,6 +18,7 @@ use Twig\TwigFunction;
use Twig\TwigTest;
use function assert;
use function explode;
use function file_get_contents;
use function is_int;
use function is_string;
@@ -36,7 +37,7 @@ final class MainExtension extends AbstractExtension
private readonly AssetIntegrityGenerator $assetIntegrityGenerator,
private readonly string $sourceDir,
private readonly string $webpackBuildDir,
private readonly CodeBlockLanguageDetector $codeBlockLanguageDetector
private readonly CodeBlockLanguageDetector $codeBlockLanguageDetector,
) {
}
@@ -49,7 +50,7 @@ final class MainExtension extends AbstractExtension
new TwigFunction('get_webpack_asset_url', [$this, 'getWebpackAssetUrl']),
new TwigFunction('get_asset_integrity', [$this->assetIntegrityGenerator, 'getAssetIntegrity']),
new TwigFunction('get_webpack_asset_integrity', [$this->assetIntegrityGenerator, 'getWebpackAssetIntegrity']),
new TwigFunction('code_block_language', fn (string|null $language, string|null $code) => $this->codeBlockLanguageDetector->detectLanguage($language ?? "php", $code !== null ? explode("\n", $code) : [])),
new TwigFunction('code_block_language', [$this, 'detectCodeBlockLanguage']),
];
}
@@ -136,4 +137,12 @@ final class MainExtension extends AbstractExtension
{
return $node instanceof TocNode;
}
public function detectCodeBlockLanguage(string|null $language, string|null $code): string
{
return $this->codeBlockLanguageDetector->detectLanguage(
$language ?? 'php',
$code !== null ? explode("\n", $code) : [],
);
}
}

View File

@@ -6,10 +6,13 @@ namespace Doctrine\Website\Tests\Twig;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Website\Assets\AssetIntegrityGenerator;
use Doctrine\Website\Docs\CodeBlockLanguageDetector;
use Doctrine\Website\Model\ProjectVersion;
use Doctrine\Website\Tests\TestCase;
use Doctrine\Website\Twig\MainExtension;
use Parsedown;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
class MainExtensionTest extends TestCase
{
@@ -22,19 +25,22 @@ class MainExtensionTest extends TestCase
private string $webpackBuildDir;
private MainExtension $mainExtension;
private CodeBlockLanguageDetector&MockObject $codeblockLanguageDetector;
protected function setUp(): void
{
$this->parsedown = $this->createMock(Parsedown::class);
$this->assetIntegrityGenerator = $this->createMock(AssetIntegrityGenerator::class);
$this->sourceDir = __DIR__ . '/../../source';
$this->webpackBuildDir = __DIR__ . '/../../.webpack-build';
$this->parsedown = $this->createMock(Parsedown::class);
$this->assetIntegrityGenerator = $this->createMock(AssetIntegrityGenerator::class);
$this->codeblockLanguageDetector = $this->createMock(CodeBlockLanguageDetector::class);
$this->sourceDir = __DIR__ . '/../../source';
$this->webpackBuildDir = __DIR__ . '/../../.webpack-build';
$this->mainExtension = new MainExtension(
$this->parsedown,
$this->assetIntegrityGenerator,
$this->sourceDir,
$this->webpackBuildDir,
$this->codeblockLanguageDetector,
);
}
@@ -76,4 +82,40 @@ class MainExtensionTest extends TestCase
self::assertMatchesRegularExpression('#^http://lcl.doctrine-project.org/js/main.js\?[a-z0-9+]{6}$#', $url);
}
/** @param string[] $lines */
#[DataProvider('codeDetectionDataProvider')]
public function testDetectCodeBlockLanguage(string|null $language, string|null $code, array $lines): void
{
$this->codeblockLanguageDetector->expects(self::once())
->method('detectLanguage')
->with($language ?? 'php', $lines)
->willReturn('php');
$language = $this->mainExtension->detectCodeBlockLanguage($language, $code);
self::assertSame('php', $language);
}
/** @return array<array{string|null, string|null, array<string>}> */
public static function codeDetectionDataProvider(): array
{
return [
[
'php',
'code',
['code'],
],
[
null,
'code',
['code'],
],
[
'sql',
null,
[],
],
];
}
}