Skip to content

Commit a40aec7

Browse files
committed
refactor: Add dependency injection for temp directory in PlantumlRenderer
Allow temp directory to be injected via constructor for better testability and flexibility. Falls back to system temp directory with /phpdocumentor subdirectory if not provided.
1 parent 061a397 commit a40aec7

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

packages/guides-graphs/src/Graphs/Renderer/PlantumlRenderer.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@
2828

2929
final class PlantumlRenderer implements DiagramRenderer
3030
{
31-
public const TEMP_SUBDIRECTORY = '/phpdocumentor';
31+
private readonly string $tempDirectory;
3232

33-
public function __construct(private readonly LoggerInterface $logger, private readonly string $plantUmlBinaryPath)
34-
{
33+
public function __construct(
34+
private readonly LoggerInterface $logger,
35+
private readonly string $plantUmlBinaryPath,
36+
string|null $tempDirectory = null,
37+
) {
38+
$this->tempDirectory = $tempDirectory ?? sys_get_temp_dir() . '/phpdocumentor';
3539
}
3640

3741
public function render(RenderContext $renderContext, string $diagram): string|null
@@ -49,12 +53,11 @@ public function render(RenderContext $renderContext, string $diagram): string|nu
4953
@enduml
5054
PUML;
5155

52-
$tempDir = sys_get_temp_dir() . self::TEMP_SUBDIRECTORY;
53-
if (!is_dir($tempDir)) {
54-
mkdir($tempDir, 0o755, true);
56+
if (!is_dir($this->tempDirectory)) {
57+
mkdir($this->tempDirectory, 0o755, true);
5558
}
5659

57-
$pumlFileLocation = tempnam($tempDir, 'pu_');
60+
$pumlFileLocation = tempnam($this->tempDirectory, 'pu_');
5861
file_put_contents($pumlFileLocation, $output);
5962
try {
6063
$process = new Process([$this->plantUmlBinaryPath, '-tsvg', $pumlFileLocation], __DIR__, null, null, 600.0);

packages/guides-graphs/tests/unit/Renderer/PlantumlRendererTest.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,21 @@
1717
use PHPUnit\Framework\TestCase;
1818
use Psr\Log\NullLogger;
1919

20-
use function is_dir;
2120
use function rmdir;
2221
use function sys_get_temp_dir;
22+
use function uniqid;
2323

2424
final class PlantumlRendererTest extends TestCase
2525
{
26-
/** @runInSeparateProcess */
2726
public function testRenderCreatesTempDirectoryWhenMissing(): void
2827
{
29-
$tempDir = sys_get_temp_dir() . PlantumlRenderer::TEMP_SUBDIRECTORY;
28+
$tempDir = sys_get_temp_dir() . '/plantuml-test-' . uniqid();
3029

31-
// Remove the directory if it exists to test creation
32-
if (is_dir($tempDir)) {
33-
@rmdir($tempDir);
34-
}
35-
36-
// Skip if we can't remove it (contains files from other processes)
37-
if (is_dir($tempDir)) {
38-
self::markTestSkipped('Cannot remove temp directory - it contains files from other processes');
39-
}
30+
// Ensure the directory does not exist
31+
self::assertDirectoryDoesNotExist($tempDir);
4032

4133
// Use a non-existent binary path - the render will fail but directory should be created first
42-
$renderer = new PlantumlRenderer(new NullLogger(), '/non/existent/plantuml');
34+
$renderer = new PlantumlRenderer(new NullLogger(), '/non/existent/plantuml', $tempDir);
4335

4436
$renderContext = $this->createMock(RenderContext::class);
4537
$renderContext->method('getLoggerInformation')->willReturn([]);

0 commit comments

Comments
 (0)