66use function array_is_list ;
77use function array_pop ;
88use function assert ;
9+ use function exec ;
910use function explode ;
1011use function file_put_contents ;
12+ use function in_array ;
1113use function is_array ;
1214use function is_dir ;
1315use function is_string ;
1416use function json_decode ;
1517use function mkdir ;
1618use function sprintf ;
19+ use function sys_get_temp_dir ;
20+ use function tempnam ;
21+ use function unlink ;
1722use PHPUnit \Event \Test \AdditionalInformationProvided ;
1823use PHPUnit \Runner \Extension \Extension as ExtensionInterface ;
1924use 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