|
2 | 2 |
|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
| 5 | +use League\CommonMark\Extension\DescriptionList\DescriptionListExtension; |
| 6 | + |
5 | 7 | it('uses commonmark extensions and environment config in cli builds', function (): void { |
6 | 8 | $projectPath = sys_get_temp_dir() . '/docsmith-commonmark-cli-' . uniqid(); |
7 | 9 | $sourcePath = $projectPath . '/md'; |
|
31 | 33 | ]; |
32 | 34 | PHP); |
33 | 35 |
|
34 | | - $command = [ |
35 | | - PHP_BINARY, |
36 | | - dirname(__DIR__, 2) . '/bin/docsmith', |
| 36 | + $result = docsmithCliProcess([ |
37 | 37 | 'build', |
38 | 38 | '--source=' . $sourcePath, |
39 | 39 | '--output=' . $outputPath, |
40 | 40 | '--commonmark-config=' . $commonMarkConfigPath, |
| 41 | + ]); |
| 42 | + |
| 43 | + expect($result['exitCode'])->toBe(0, $result['stderr']) |
| 44 | + ->and($result['stdout'])->toContain('Built docs'); |
| 45 | + |
| 46 | + $html = (string) file_get_contents($outputPath . '/index.html'); |
| 47 | + |
| 48 | + expect($html)->toContain('<dl>') |
| 49 | + ->toContain('<dt>Term</dt>') |
| 50 | + ->toContain('<dd>Definition</dd>') |
| 51 | + ->and(str_contains($html, '<div>'))->toBeFalse() |
| 52 | + ->and(str_contains($html, 'removed'))->toBeFalse(); |
| 53 | +}); |
| 54 | + |
| 55 | +it('fails clearly when the commonmark config file does not exist', function (): void { |
| 56 | + $projectPath = sys_get_temp_dir() . '/docsmith-commonmark-cli-missing-' . uniqid(); |
| 57 | + $sourcePath = $projectPath . '/md'; |
| 58 | + mkdir($sourcePath, 0777, true); |
| 59 | + file_put_contents($sourcePath . '/index.md', '# Hello'); |
| 60 | + |
| 61 | + $result = docsmithCliProcess([ |
| 62 | + 'build', |
| 63 | + '--source=' . $sourcePath, |
| 64 | + '--output=' . $projectPath . '/docs', |
| 65 | + '--commonmark-config=' . $projectPath . '/missing.php', |
| 66 | + ]); |
| 67 | + |
| 68 | + expect($result['exitCode'])->toBe(1) |
| 69 | + ->and($result['stderr'])->toContain('[Docsmith] Invalid CommonMark config') |
| 70 | + ->and($result['stderr'])->toContain('File does not exist'); |
| 71 | +}); |
| 72 | + |
| 73 | +it('fails clearly when the commonmark config does not return an array', function (): void { |
| 74 | + $projectPath = sys_get_temp_dir() . '/docsmith-commonmark-cli-nonarray-' . uniqid(); |
| 75 | + $sourcePath = $projectPath . '/md'; |
| 76 | + mkdir($sourcePath, 0777, true); |
| 77 | + file_put_contents($sourcePath . '/index.md', '# Hello'); |
| 78 | + |
| 79 | + $configPath = $projectPath . '/commonmark.php'; |
| 80 | + file_put_contents($configPath, "<?php\n\nreturn 'not-an-array';\n"); |
| 81 | + |
| 82 | + $result = docsmithCliProcess([ |
| 83 | + 'build', |
| 84 | + '--source=' . $sourcePath, |
| 85 | + '--output=' . $projectPath . '/docs', |
| 86 | + '--commonmark-config=' . $configPath, |
| 87 | + ]); |
| 88 | + |
| 89 | + expect($result['exitCode'])->toBe(1) |
| 90 | + ->and($result['stderr'])->toContain('The file must return an array.'); |
| 91 | +}); |
| 92 | + |
| 93 | +it('fails clearly when the commonmark config has invalid extensions', function (): void { |
| 94 | + $projectPath = sys_get_temp_dir() . '/docsmith-commonmark-cli-badext-' . uniqid(); |
| 95 | + $sourcePath = $projectPath . '/md'; |
| 96 | + mkdir($sourcePath, 0777, true); |
| 97 | + file_put_contents($sourcePath . '/index.md', '# Hello'); |
| 98 | + |
| 99 | + $configPath = $projectPath . '/commonmark.php'; |
| 100 | + file_put_contents($configPath, <<<'PHP' |
| 101 | + <?php |
| 102 | +
|
| 103 | + return [ |
| 104 | + 'extensions' => [\League\CommonMark\Extension\DescriptionList\DescriptionListExtension::class], |
| 105 | + ]; |
| 106 | + PHP); |
| 107 | + |
| 108 | + $result = docsmithCliProcess([ |
| 109 | + 'build', |
| 110 | + '--source=' . $sourcePath, |
| 111 | + '--output=' . $projectPath . '/docs', |
| 112 | + '--commonmark-config=' . $configPath, |
| 113 | + ]); |
| 114 | + |
| 115 | + expect($result['exitCode'])->toBe(1) |
| 116 | + ->and($result['stderr'])->toContain(DescriptionListExtension::class) |
| 117 | + ->and($result['stderr'])->toContain('must implement'); |
| 118 | +}); |
| 119 | + |
| 120 | +/** |
| 121 | + * Run the Docsmith binary in a subprocess and capture its output. |
| 122 | + * |
| 123 | + * @param list<string> $arguments |
| 124 | + * |
| 125 | + * @return array{exitCode: int, stdout: string, stderr: string} |
| 126 | + */ |
| 127 | +function docsmithCliProcess(array $arguments): array |
| 128 | +{ |
| 129 | + $command = [ |
| 130 | + PHP_BINARY, |
| 131 | + dirname(__DIR__, 2) . '/bin/docsmith', |
| 132 | + ...$arguments, |
41 | 133 | ]; |
42 | 134 | $pipes = []; |
43 | 135 | $process = proc_open($command, [ |
|
55 | 147 | fclose($pipes[2]); |
56 | 148 | $exitCode = proc_close($process); |
57 | 149 |
|
58 | | - expect($exitCode)->toBe(0, $stderr) |
59 | | - ->and($stdout)->toContain('Built docs'); |
60 | | - |
61 | | - $html = (string) file_get_contents($outputPath . '/index.html'); |
62 | | - |
63 | | - expect($html)->toContain('<dl>') |
64 | | - ->toContain('<dt>Term</dt>') |
65 | | - ->toContain('<dd>Definition</dd>') |
66 | | - ->and(str_contains($html, '<div>'))->toBeFalse() |
67 | | - ->and(str_contains($html, 'removed'))->toBeFalse(); |
68 | | -}); |
| 150 | + return [ |
| 151 | + 'exitCode' => $exitCode, |
| 152 | + 'stdout' => (string) $stdout, |
| 153 | + 'stderr' => (string) $stderr, |
| 154 | + ]; |
| 155 | +} |
0 commit comments