|
10 | 10 | use PHPUnit\Framework\TestCase; |
11 | 11 | use T3Docs\Typo3DocsTheme\Renderer\DecoratingPlantumlBinaryRenderer; |
12 | 12 |
|
| 13 | +use function file_exists; |
13 | 14 | use function is_dir; |
14 | 15 | use function rmdir; |
| 16 | +use function set_error_handler; |
15 | 17 | use function sys_get_temp_dir; |
| 18 | +use function tempnam; |
| 19 | +use function unlink; |
16 | 20 |
|
17 | 21 | final class DecoratingPlantumlBinaryRendererTest extends TestCase |
18 | 22 | { |
19 | 23 | private const TEMP_SUBDIRECTORY = '/phpdocumentor'; |
20 | 24 |
|
| 25 | + /** |
| 26 | + * Canary test: Verifies that tempnam() still triggers E_NOTICE when directory doesn't exist. |
| 27 | + * |
| 28 | + * This test documents the upstream bug that DecoratingPlantumlBinaryRenderer works around. |
| 29 | + * When this test FAILS, the upstream library (phpDocumentor/guides-graphs) has likely |
| 30 | + * fixed the issue, and this decorator may no longer be needed. |
| 31 | + * |
| 32 | + * @see https://github.com/phpDocumentor/guides-graphs/issues/1 |
| 33 | + * @see https://github.com/TYPO3-Documentation/render-guides/pull/1099 |
| 34 | + */ |
| 35 | + #[Test] |
| 36 | + public function tempnamStillTriggersNoticeWhenDirectoryMissing(): void |
| 37 | + { |
| 38 | + // Use a unique non-existent directory to avoid interference |
| 39 | + $nonExistentDir = sys_get_temp_dir() . '/phpdocumentor_canary_' . uniqid(); |
| 40 | + |
| 41 | + $noticeTriggered = false; |
| 42 | + $previousHandler = set_error_handler(static function (int $errno, string $errstr) use (&$noticeTriggered): bool { |
| 43 | + if ($errno === E_NOTICE && str_contains($errstr, 'tempnam()')) { |
| 44 | + $noticeTriggered = true; |
| 45 | + } |
| 46 | + return false; // Let PHP handle it normally |
| 47 | + }); |
| 48 | + |
| 49 | + try { |
| 50 | + $tempFile = @tempnam($nonExistentDir, 'canary_'); |
| 51 | + |
| 52 | + // Clean up the temp file if it was created (in system temp dir) |
| 53 | + if ($tempFile !== false && file_exists($tempFile)) { |
| 54 | + unlink($tempFile); |
| 55 | + } |
| 56 | + } finally { |
| 57 | + set_error_handler($previousHandler); |
| 58 | + } |
| 59 | + |
| 60 | + self::assertTrue( |
| 61 | + $noticeTriggered, |
| 62 | + 'Expected tempnam() to trigger E_NOTICE when directory does not exist. ' |
| 63 | + . 'If this test fails, the upstream issue may be fixed and DecoratingPlantumlBinaryRenderer ' |
| 64 | + . 'might no longer be needed. Check: https://github.com/phpDocumentor/guides-graphs/issues/1' |
| 65 | + ); |
| 66 | + } |
| 67 | + |
21 | 68 | #[Test] |
22 | 69 | public function renderCreatesTempDirectoryWhenMissing(): void |
23 | 70 | { |
|
0 commit comments