|
13 | 13 |
|
14 | 14 | namespace phpDocumentor\Guides\Graphs\Renderer; |
15 | 15 |
|
| 16 | +use FilesystemIterator; |
16 | 17 | use phpDocumentor\Guides\RenderContext; |
17 | 18 | use PHPUnit\Framework\TestCase; |
18 | 19 | use Psr\Log\NullLogger; |
| 20 | +use RecursiveDirectoryIterator; |
| 21 | +use RecursiveIteratorIterator; |
| 22 | +use SplFileInfo; |
| 23 | +use Throwable; |
19 | 24 |
|
| 25 | +use function assert; |
| 26 | +use function basename; |
| 27 | +use function is_dir; |
| 28 | +use function realpath; |
20 | 29 | use function rmdir; |
21 | 30 | use function sys_get_temp_dir; |
22 | 31 | use function uniqid; |
| 32 | +use function unlink; |
| 33 | + |
| 34 | +use const DIRECTORY_SEPARATOR; |
23 | 35 |
|
24 | 36 | final class PlantumlRendererTest extends TestCase |
25 | 37 | { |
| 38 | + private const TEMP_DIR_PREFIX = 'plantuml-test-'; |
| 39 | + |
26 | 40 | public function testRenderCreatesTempDirectoryWhenMissing(): void |
27 | 41 | { |
28 | | - $tempDir = sys_get_temp_dir() . '/plantuml-test-' . uniqid(); |
| 42 | + $tempDir = sys_get_temp_dir() . '/' . self::TEMP_DIR_PREFIX . uniqid('', true); |
29 | 43 |
|
30 | | - // Ensure the directory does not exist |
31 | 44 | self::assertDirectoryDoesNotExist($tempDir); |
32 | 45 |
|
33 | | - // Use a non-existent binary path - the render will fail but directory should be created first |
34 | 46 | $renderer = new PlantumlRenderer(new NullLogger(), '/non/existent/plantuml', $tempDir); |
35 | 47 |
|
36 | 48 | $renderContext = $this->createMock(RenderContext::class); |
37 | 49 | $renderContext->method('getLoggerInformation')->willReturn([]); |
38 | 50 |
|
39 | | - // The render will fail due to missing binary, but the temp directory should be created |
40 | | - $renderer->render($renderContext, 'A -> B'); |
| 51 | + try { |
| 52 | + // Render will fail (returns null) or throw due to non-existent plantuml binary. |
| 53 | + // We only care about verifying that the temp directory was created. |
| 54 | + $renderer->render($renderContext, 'A -> B'); |
| 55 | + } catch (Throwable) { |
| 56 | + // Expected: plantuml binary doesn't exist |
| 57 | + } |
| 58 | + |
| 59 | + self::assertDirectoryExists($tempDir, 'Temp directory should have been created'); |
| 60 | + |
| 61 | + $this->removeTempDirSafely($tempDir); |
| 62 | + } |
| 63 | + |
| 64 | + private function removeTempDirSafely(string $dir): void |
| 65 | + { |
| 66 | + if ($dir === '' || !is_dir($dir)) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + $realDir = realpath($dir); |
| 71 | + if ($realDir === false) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + $realSysTmp = realpath(sys_get_temp_dir()); |
| 76 | + if ($realSysTmp === false) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + // Safety: must be under system temp and have our prefix |
| 81 | + self::assertStringStartsWith( |
| 82 | + $realSysTmp . DIRECTORY_SEPARATOR, |
| 83 | + $realDir . DIRECTORY_SEPARATOR, |
| 84 | + 'Refusing to delete directory outside system temp', |
| 85 | + ); |
| 86 | + self::assertStringContainsString( |
| 87 | + self::TEMP_DIR_PREFIX, |
| 88 | + basename($realDir), |
| 89 | + 'Refusing to delete directory without expected prefix', |
| 90 | + ); |
| 91 | + |
| 92 | + /** @var RecursiveIteratorIterator<RecursiveDirectoryIterator> $iterator */ |
| 93 | + $iterator = new RecursiveIteratorIterator( |
| 94 | + new RecursiveDirectoryIterator($realDir, FilesystemIterator::SKIP_DOTS), |
| 95 | + RecursiveIteratorIterator::CHILD_FIRST, |
| 96 | + ); |
41 | 97 |
|
42 | | - self::assertDirectoryExists($tempDir); |
| 98 | + foreach ($iterator as $file) { |
| 99 | + assert($file instanceof SplFileInfo); |
| 100 | + $file->isDir() ? @rmdir($file->getPathname()) : @unlink($file->getPathname()); |
| 101 | + } |
43 | 102 |
|
44 | | - // Clean up |
45 | | - @rmdir($tempDir); |
| 103 | + @rmdir($realDir); |
46 | 104 | } |
47 | 105 | } |
0 commit comments