Skip to content
This repository was archived by the owner on Nov 5, 2025. It is now read-only.

Commit 5cac427

Browse files
Optionally render GraphViz/DOT markup to PDF, PNG, or SVG
1 parent c72a35d commit 5cac427

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<extensions>
6060
<bootstrap class="example\framework\event\test\extension\Extension">
6161
<parameter name="targetDirectory" value="build"/>
62+
<parameter name="format" value="pdf"/>
6263
</bootstrap>
6364
</extensions>
6465
</phpunit>

tests/src/extension/Extension.php

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@
66
use function array_is_list;
77
use function array_pop;
88
use function assert;
9+
use function exec;
910
use function explode;
1011
use function file_put_contents;
12+
use function in_array;
1113
use function is_array;
1214
use function is_dir;
1315
use function is_string;
1416
use function json_decode;
1517
use function mkdir;
1618
use function sprintf;
19+
use function sys_get_temp_dir;
20+
use function tempnam;
21+
use function unlink;
1722
use PHPUnit\Event\Test\AdditionalInformationProvided;
1823
use PHPUnit\Runner\Extension\Extension as ExtensionInterface;
1924
use PHPUnit\Runner\Extension\Facade as ExtensionFacade;
@@ -27,6 +32,11 @@ final class Extension implements ExtensionInterface
2732
*/
2833
private string $targetDirectory;
2934

35+
/**
36+
* @var 'dot'|'pdf'|'png'|'svg'
37+
*/
38+
private string $format;
39+
3040
public function bootstrap(Configuration $configuration, ExtensionFacade $facade, ParameterCollection $parameters): void
3141
{
3242
$targetDirectory = '/tmp';
@@ -41,6 +51,16 @@ public function bootstrap(Configuration $configuration, ExtensionFacade $facade,
4151

4252
$this->createDirectory($this->targetDirectory);
4353

54+
$format = 'dot';
55+
56+
if ($parameters->has('format')) {
57+
if (in_array($parameters->get('format'), ['dot', 'pdf', 'png', 'svg'], true)) {
58+
$format = $parameters->get('format');
59+
}
60+
}
61+
62+
$this->format = $format;
63+
4464
$facade->registerSubscriber(new AdditionalInformationProvidedSubscriber($this));
4565
}
4666

@@ -61,23 +81,44 @@ public function testProvidedAdditionalInformation(AdditionalInformationProvided
6181
$tmp = explode('\\', $event->test()->className());
6282
$className = array_pop($tmp);
6383

64-
file_put_contents(
84+
$dot = (new DotRenderer)->render(
85+
/** @phpstan-ignore argument.type */
86+
$data['given'],
87+
/** @phpstan-ignore argument.type */
88+
$data['when'],
89+
/** @phpstan-ignore argument.type */
90+
$data['then'],
91+
);
92+
93+
$target = sprintf(
94+
'%s%s%s_%s.%s',
95+
$this->targetDirectory,
96+
DIRECTORY_SEPARATOR,
97+
$className,
98+
$event->test()->methodName(),
99+
$this->format,
100+
);
101+
102+
if ($this->format === 'dot') {
103+
file_put_contents($target, $dot);
104+
105+
return;
106+
}
107+
108+
$tmpFile = tempnam(sys_get_temp_dir(), 'graphviz');
109+
110+
file_put_contents($tmpFile, $dot);
111+
112+
exec(
65113
sprintf(
66-
'%s%s%s_%s.dot',
67-
$this->targetDirectory,
68-
DIRECTORY_SEPARATOR,
69-
$className,
70-
$event->test()->methodName(),
71-
),
72-
(new DotRenderer)->render(
73-
/** @phpstan-ignore argument.type */
74-
$data['given'],
75-
/** @phpstan-ignore argument.type */
76-
$data['when'],
77-
/** @phpstan-ignore argument.type */
78-
$data['then'],
114+
'dot -T%s -o %s %s > /dev/null 2>&1',
115+
$this->format,
116+
$target,
117+
$tmpFile,
79118
),
80119
);
120+
121+
unlink($tmpFile);
81122
}
82123

83124
/**

0 commit comments

Comments
 (0)