Skip to content

Commit 1d44823

Browse files
committed
[TASK] Add canary test to detect upstream fix
Add a test that expects tempnam() to trigger E_NOTICE when the directory doesn't exist. When this test FAILS, it indicates that PHP's behavior has changed or upstream has fixed the issue, and the DecoratingPlantumlBinaryRenderer workaround may no longer be needed. This helps track when we can safely remove this workaround. Relates: TYPO3-Documentation#1099
1 parent b916a0c commit 1d44823

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

packages/typo3-docs-theme/tests/unit/Renderer/DecoratingPlantumlBinaryRendererTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,61 @@
1010
use PHPUnit\Framework\TestCase;
1111
use T3Docs\Typo3DocsTheme\Renderer\DecoratingPlantumlBinaryRenderer;
1212

13+
use function file_exists;
1314
use function is_dir;
1415
use function rmdir;
16+
use function set_error_handler;
1517
use function sys_get_temp_dir;
18+
use function tempnam;
19+
use function unlink;
1620

1721
final class DecoratingPlantumlBinaryRendererTest extends TestCase
1822
{
1923
private const TEMP_SUBDIRECTORY = '/phpdocumentor';
2024

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+
2168
#[Test]
2269
public function renderCreatesTempDirectoryWhenMissing(): void
2370
{

0 commit comments

Comments
 (0)