|
14 | 14 | use Symfony\Component\Process\Exception\LogicException; |
15 | 15 | use Symfony\Component\Process\Exception\ProcessTimedOutException; |
16 | 16 | use Symfony\Component\Process\Exception\RuntimeException; |
| 17 | +use Symfony\Component\Process\InputStream; |
17 | 18 | use Symfony\Component\Process\PhpExecutableFinder; |
18 | 19 | use Symfony\Component\Process\Pipes\PipesInterface; |
19 | 20 | use Symfony\Component\Process\Process; |
@@ -1176,29 +1177,72 @@ public function provideVariousIncrementals() { |
1176 | 1177 |
|
1177 | 1178 | public function testIteratorInput() |
1178 | 1179 | { |
1179 | | - $nextData = 'ping'; |
1180 | | - $input = function () use (&$nextData) { |
1181 | | - while (false !== $nextData) { |
1182 | | - yield $nextData; |
1183 | | - yield $nextData = ''; |
1184 | | - } |
| 1180 | + $input = function () { |
| 1181 | + yield 'ping'; |
| 1182 | + yield 'pong'; |
1185 | 1183 | }; |
1186 | | - $input = $input(); |
1187 | 1184 |
|
1188 | | - $process = new Process(self::$phpBin.' -r '.escapeshellarg('stream_copy_to_stream(STDIN, STDOUT);')); |
| 1185 | + $process = new Process(self::$phpBin.' -r '.escapeshellarg('stream_copy_to_stream(STDIN, STDOUT);'), null, null, $input()); |
| 1186 | + $process->run(); |
| 1187 | + $this->assertSame('pingpong', $process->getOutput()); |
| 1188 | + } |
| 1189 | + |
| 1190 | + public function testSimpleInputStream() |
| 1191 | + { |
| 1192 | + $input = new InputStream(); |
| 1193 | + |
| 1194 | + $process = new Process(self::$phpBin.' -r '.escapeshellarg('echo \'ping\'; stream_copy_to_stream(STDIN, STDOUT);')); |
1189 | 1195 | $process->setInput($input); |
1190 | | - $process->start(function ($type, $data) use ($input, &$nextData) { |
| 1196 | + |
| 1197 | + $process->start(function ($type, $data) use ($input) { |
1191 | 1198 | if ('ping' === $data) { |
1192 | | - $h = fopen('php://memory', 'r+'); |
1193 | | - fwrite($h, 'pong'); |
1194 | | - rewind($h); |
1195 | | - $nextData = $h; |
1196 | | - $input->next(); |
1197 | | - } else { |
1198 | | - $nextData = false; |
| 1199 | + $input->write('pang'); |
| 1200 | + } elseif (!$input->isClosed()) { |
| 1201 | + $input->write('pong'); |
| 1202 | + $input->close(); |
1199 | 1203 | } |
1200 | 1204 | }); |
1201 | 1205 |
|
| 1206 | + $process->wait(); |
| 1207 | + $this->assertSame('pingpangpong', $process->getOutput()); |
| 1208 | + } |
| 1209 | + |
| 1210 | + public function testInputStreamWithCallable() |
| 1211 | + { |
| 1212 | + $i = 0; |
| 1213 | + $stream = fopen('php://memory', 'w+'); |
| 1214 | + $stream = function () use ($stream, &$i) { |
| 1215 | + rewind($stream); |
| 1216 | + fwrite($stream, ++$i); |
| 1217 | + rewind($stream); |
| 1218 | + |
| 1219 | + return $stream; |
| 1220 | + }; |
| 1221 | + |
| 1222 | + $input = new InputStream($stream); |
| 1223 | + $input->write($stream()); |
| 1224 | + |
| 1225 | + $process = new Process(self::$phpBin.' -r '.escapeshellarg('stream_copy_to_stream(STDIN, STDOUT);')); |
| 1226 | + $process->setInput($input); |
| 1227 | + $process->start(function ($type, $data) use ($input) { |
| 1228 | + $input->close(); |
| 1229 | + }); |
| 1230 | + |
| 1231 | + $process->wait(); |
| 1232 | + $this->assertStringStartsWith('123', $process->getOutput()); |
| 1233 | + } |
| 1234 | + |
| 1235 | + public function testInputStreamWithGenerator() |
| 1236 | + { |
| 1237 | + $input = new InputStream(function ($input) { |
| 1238 | + yield 'pong'; |
| 1239 | + $input->close(); |
| 1240 | + }); |
| 1241 | + |
| 1242 | + $process = new Process(self::$phpBin.' -r '.escapeshellarg('stream_copy_to_stream(STDIN, STDOUT);')); |
| 1243 | + $process->setInput($input); |
| 1244 | + $process->start(); |
| 1245 | + $input->write('ping'); |
1202 | 1246 | $process->wait(); |
1203 | 1247 | $this->assertSame('pingpong', $process->getOutput()); |
1204 | 1248 | } |
|
0 commit comments