Skip to content

Commit 8131dd4

Browse files
committed
Added extra tests
1 parent e4ba8d4 commit 8131dd4

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

lib/Docs/CodeBlockLanguageDetector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
use function sprintf;
1010
use function trim;
1111

12-
final readonly class CodeBlockLanguageDetector
12+
/** @final */
13+
readonly class CodeBlockLanguageDetector
1314
{
1415
/**
1516
* We use some language aliases not supported by our highlighter library

lib/Twig/MainExtension.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Twig\TwigTest;
1919

2020
use function assert;
21+
use function explode;
2122
use function file_get_contents;
2223
use function is_int;
2324
use function is_string;
@@ -36,7 +37,7 @@ public function __construct(
3637
private readonly AssetIntegrityGenerator $assetIntegrityGenerator,
3738
private readonly string $sourceDir,
3839
private readonly string $webpackBuildDir,
39-
private readonly CodeBlockLanguageDetector $codeBlockLanguageDetector
40+
private readonly CodeBlockLanguageDetector $codeBlockLanguageDetector,
4041
) {
4142
}
4243

@@ -49,7 +50,7 @@ public function getFunctions(): array
4950
new TwigFunction('get_webpack_asset_url', [$this, 'getWebpackAssetUrl']),
5051
new TwigFunction('get_asset_integrity', [$this->assetIntegrityGenerator, 'getAssetIntegrity']),
5152
new TwigFunction('get_webpack_asset_integrity', [$this->assetIntegrityGenerator, 'getWebpackAssetIntegrity']),
52-
new TwigFunction('code_block_language', fn (string|null $language, string|null $code) => $this->codeBlockLanguageDetector->detectLanguage($language ?? "php", $code !== null ? explode("\n", $code) : [])),
53+
new TwigFunction('code_block_language', [$this, 'detectCodeBlockLanguage']),
5354
];
5455
}
5556

@@ -136,4 +137,12 @@ public function isTocNode(Node $node): bool
136137
{
137138
return $node instanceof TocNode;
138139
}
140+
141+
public function detectCodeBlockLanguage(string|null $language, string|null $code): string
142+
{
143+
return $this->codeBlockLanguageDetector->detectLanguage(
144+
$language ?? 'php',
145+
$code !== null ? explode("\n", $code) : [],
146+
);
147+
}
139148
}

tests/Twig/MainExtensionTest.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66

77
use Doctrine\Common\Collections\ArrayCollection;
88
use Doctrine\Website\Assets\AssetIntegrityGenerator;
9+
use Doctrine\Website\Docs\CodeBlockLanguageDetector;
910
use Doctrine\Website\Model\ProjectVersion;
1011
use Doctrine\Website\Tests\TestCase;
1112
use Doctrine\Website\Twig\MainExtension;
1213
use Parsedown;
14+
use PHPUnit\Framework\Attributes\DataProvider;
15+
use PHPUnit\Framework\MockObject\MockObject;
1316

1417
class MainExtensionTest extends TestCase
1518
{
@@ -22,19 +25,22 @@ class MainExtensionTest extends TestCase
2225
private string $webpackBuildDir;
2326

2427
private MainExtension $mainExtension;
28+
private CodeBlockLanguageDetector&MockObject $codeblockLanguageDetector;
2529

2630
protected function setUp(): void
2731
{
28-
$this->parsedown = $this->createMock(Parsedown::class);
29-
$this->assetIntegrityGenerator = $this->createMock(AssetIntegrityGenerator::class);
30-
$this->sourceDir = __DIR__ . '/../../source';
31-
$this->webpackBuildDir = __DIR__ . '/../../.webpack-build';
32+
$this->parsedown = $this->createMock(Parsedown::class);
33+
$this->assetIntegrityGenerator = $this->createMock(AssetIntegrityGenerator::class);
34+
$this->codeblockLanguageDetector = $this->createMock(CodeBlockLanguageDetector::class);
35+
$this->sourceDir = __DIR__ . '/../../source';
36+
$this->webpackBuildDir = __DIR__ . '/../../.webpack-build';
3237

3338
$this->mainExtension = new MainExtension(
3439
$this->parsedown,
3540
$this->assetIntegrityGenerator,
3641
$this->sourceDir,
3742
$this->webpackBuildDir,
43+
$this->codeblockLanguageDetector,
3844
);
3945
}
4046

@@ -76,4 +82,40 @@ public function testGetAssetUrl(): void
7682

7783
self::assertMatchesRegularExpression('#^http://lcl.doctrine-project.org/js/main.js\?[a-z0-9+]{6}$#', $url);
7884
}
85+
86+
/** @param string[] $lines */
87+
#[DataProvider('codeDetectionDataProvider')]
88+
public function testDetectCodeBlockLanguage(string|null $language, string|null $code, array $lines): void
89+
{
90+
$this->codeblockLanguageDetector->expects(self::once())
91+
->method('detectLanguage')
92+
->with($language ?? 'php', $lines)
93+
->willReturn('php');
94+
95+
$language = $this->mainExtension->detectCodeBlockLanguage($language, $code);
96+
97+
self::assertSame('php', $language);
98+
}
99+
100+
/** @return array<array{string|null, string|null, array<string>}> */
101+
public static function codeDetectionDataProvider(): array
102+
{
103+
return [
104+
[
105+
'php',
106+
'code',
107+
['code'],
108+
],
109+
[
110+
null,
111+
'code',
112+
['code'],
113+
],
114+
[
115+
'sql',
116+
null,
117+
[],
118+
],
119+
];
120+
}
79121
}

0 commit comments

Comments
 (0)