66
77use Doctrine \Common \Collections \ArrayCollection ;
88use Doctrine \Website \Assets \AssetIntegrityGenerator ;
9+ use Doctrine \Website \Docs \CodeBlockLanguageDetector ;
910use Doctrine \Website \Model \ProjectVersion ;
1011use Doctrine \Website \Tests \TestCase ;
1112use Doctrine \Website \Twig \MainExtension ;
1213use Parsedown ;
14+ use PHPUnit \Framework \Attributes \DataProvider ;
15+ use PHPUnit \Framework \MockObject \MockObject ;
1316
1417class 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