@@ -36,6 +36,41 @@ public static function dataFiltersInput(): iterable
3636 yield 'resource(in-memory stream) ' => [$ inMemoryStream , $ helloWorldPhpStdOut ];
3737 }
3838
39+ #[DataProvider('dataWritesToStreamsAndReturnsExpectedValue ' )]
40+ public function testWritesToStreamsAndReturnsExpectedValue (bool $ useOutput , bool $ useError ): void
41+ {
42+ $ outputStream = fopen ('php://memory ' , 'w+ ' );
43+ $ errorStream = fopen ('php://memory ' , 'w+ ' );
44+
45+ $ returned = (new Filter ('php ' ))->filter (
46+ input: '<?php fwrite(fopen("php://stdout", "w"), "output"); fwrite(fopen("php://stderr", "w"), "error"); ' ,
47+ output: $ useOutput ? $ outputStream : null ,
48+ error: $ useError ? $ errorStream : null ,
49+ );
50+ rewind ($ outputStream );
51+ rewind ($ errorStream );
52+
53+ self ::assertSame ([
54+ 'returned ' => $ useOutput ? null : 'output ' ,
55+ 'output ' => $ useOutput ? 'output ' : '' ,
56+ 'error ' => $ useError ? 'error ' : '' ,
57+ ], [
58+ 'returned ' => $ returned ,
59+ 'output ' => stream_get_contents ($ outputStream ),
60+ 'error ' => stream_get_contents ($ errorStream ),
61+ ]);
62+ }
63+
64+ public static function dataWritesToStreamsAndReturnsExpectedValue (): array
65+ {
66+ return [
67+ 'no stream ' => [false , false ],
68+ 'output stream ' => [true , false ],
69+ 'error stream ' => [false , true ],
70+ 'both streams ' => [true , true ],
71+ ];
72+ }
73+
3974 public function testBuildsAndExecutesPipeline (): void
4075 {
4176 $ pipeline = (new Filter ('gzip ' ))->pipe (new Filter ('base64 ' ))->pipe (new Filter ('base64 ' , ['--decode ' ]));
@@ -48,20 +83,27 @@ public function testBuildsAndExecutesPipeline(): void
4883 }
4984
5085 #[DataProvider('dataThrows ' )]
51- public function testThrows (string $ command , array $ options , mixed $ input ): void
86+ public function testThrows (string $ command , array $ options , mixed $ input, mixed $ output , mixed $ error ): void
5287 {
5388 self ::expectException (Exception \FilterException::class);
5489
55- (new Filter ($ command , $ options ))->filter ($ input );
90+ (new Filter ($ command , $ options ))->filter ($ input, $ output , $ error );
5691 }
5792
5893 public static function dataThrows (): array
5994 {
95+ $ closedStream = fopen ('php://memory ' , 'w ' );
96+ fclose ($ closedStream );
6097 return [
61- 'unknown command ' => ['unknown ' , [], '' ],
62- 'unknown option ' => ['php ' , ['--unknown ' ], '' ],
63- 'wrong data ' => ['php ' , [], '<?php wrong data ' ],
64- 'unsupported input ' => ['php ' , [], new stdClass ()],
98+ 'unknown command ' => ['unknown ' , [], '' , null , null ],
99+ 'unknown option ' => ['php ' , ['--unknown ' ], '' , null , null ],
100+ 'wrong data ' => ['php ' , [], '<?php wrong data ' , null , null ],
101+ 'unsupported input ' => ['php ' , [], new stdClass (), null , null ],
102+ 'unsupported output ' => ['php ' , [], '' , new stdClass (), null ],
103+ 'unsupported error ' => ['php ' , [], '' , null , new stdClass ()],
104+ 'closed input ' => ['php ' , [], $ closedStream , null , null ],
105+ 'closed output ' => ['php ' , [], '' , $ closedStream , null ],
106+ 'closed error ' => ['php ' , [], '' , null , $ closedStream ],
65107 ];
66108 }
67109}
0 commit comments