22
33namespace React \Tests \ChildProcess ;
44
5- use React \ChildProcess \Process ;
6- use React \EventLoop \Timer \Timer ;
75use PHPUnit \Framework \ExpectationFailedException ;
86use PHPUnit \Framework \TestCase ;
7+ use React \ChildProcess \Process ;
98use SebastianBergmann \Environment \Runtime ;
109
1110abstract class AbstractProcessTest extends TestCase
@@ -152,15 +151,13 @@ public function testProcessWithDefaultCwdAndEnv()
152151 $ cmd = $ this ->getPhpBinary () . ' -r ' . escapeshellarg ('echo getcwd(), PHP_EOL, count($_SERVER), PHP_EOL; ' );
153152
154153 $ loop = $ this ->createLoop ();
154+
155155 $ process = new Process ($ cmd );
156+ $ process ->start ($ loop );
156157
157158 $ output = '' ;
158-
159- $ loop ->addTimer (0.001 , function (Timer $ timer ) use ($ process , &$ output ) {
160- $ process ->start ($ timer ->getLoop ());
161- $ process ->stdout ->on ('data ' , function () use (&$ output ) {
162- $ output .= func_get_arg (0 );
163- });
159+ $ process ->stdout ->on ('data ' , function () use (&$ output ) {
160+ $ output .= func_get_arg (0 );
164161 });
165162
166163 $ loop ->run ();
@@ -180,15 +177,13 @@ public function testProcessWithCwd()
180177 $ cmd = $ this ->getPhpBinary () . ' -r ' . escapeshellarg ('echo getcwd(), PHP_EOL; ' );
181178
182179 $ loop = $ this ->createLoop ();
180+
183181 $ process = new Process ($ cmd , '/ ' );
182+ $ process ->start ($ loop );
184183
185184 $ output = '' ;
186-
187- $ loop ->addTimer (0.001 , function (Timer $ timer ) use ($ process , &$ output ) {
188- $ process ->start ($ timer ->getLoop ());
189- $ process ->stdout ->on ('data ' , function () use (&$ output ) {
190- $ output .= func_get_arg (0 );
191- });
185+ $ process ->stdout ->on ('data ' , function () use (&$ output ) {
186+ $ output .= func_get_arg (0 );
192187 });
193188
194189 $ loop ->run ();
@@ -205,15 +200,13 @@ public function testProcessWithEnv()
205200 $ cmd = $ this ->getPhpBinary () . ' -r ' . escapeshellarg ('echo getenv("foo"), PHP_EOL; ' );
206201
207202 $ loop = $ this ->createLoop ();
203+
208204 $ process = new Process ($ cmd , null , array ('foo ' => 'bar ' ));
205+ $ process ->start ($ loop );
209206
210207 $ output = '' ;
211-
212- $ loop ->addTimer (0.001 , function (Timer $ timer ) use ($ process , &$ output ) {
213- $ process ->start ($ timer ->getLoop ());
214- $ process ->stdout ->on ('data ' , function () use (&$ output ) {
215- $ output .= func_get_arg (0 );
216- });
208+ $ process ->stdout ->on ('data ' , function () use (&$ output ) {
209+ $ output .= func_get_arg (0 );
217210 });
218211
219212 $ loop ->run ();
@@ -236,9 +229,7 @@ public function testStartAndAllowProcessToExitSuccessfullyUsingEventLoop()
236229 $ termSignal = func_get_arg (1 );
237230 });
238231
239- $ loop ->addTimer (0.001 , function (Timer $ timer ) use ($ process ) {
240- $ process ->start ($ timer ->getLoop ());
241- });
232+ $ process ->start ($ loop );
242233
243234 $ loop ->run ();
244235
@@ -257,15 +248,13 @@ public function testStartInvalidProcess()
257248 $ cmd = tempnam (sys_get_temp_dir (), 'react ' );
258249
259250 $ loop = $ this ->createLoop ();
251+
260252 $ process = new Process ($ cmd );
253+ $ process ->start ($ loop );
261254
262255 $ output = '' ;
263-
264- $ loop ->addTimer (0.001 , function (Timer $ timer ) use ($ process , &$ output ) {
265- $ process ->start ($ timer ->getLoop ());
266- $ process ->stderr ->on ('data ' , function () use (&$ output ) {
267- $ output .= func_get_arg (0 );
268- });
256+ $ process ->stderr ->on ('data ' , function () use (&$ output ) {
257+ $ output .= func_get_arg (0 );
269258 });
270259
271260 $ loop ->run ();
@@ -338,10 +327,8 @@ public function testTerminateWithDefaultTermSignalUsingEventLoop()
338327 $ termSignal = func_get_arg (1 );
339328 });
340329
341- $ loop ->addTimer (0.001 , function (Timer $ timer ) use ($ process ) {
342- $ process ->start ($ timer ->getLoop ());
343- $ process ->terminate ();
344- });
330+ $ process ->start ($ loop );
331+ $ process ->terminate ();
345332
346333 $ loop ->run ();
347334
@@ -379,22 +366,20 @@ public function testTerminateWithStopAndContinueSignalsUsingEventLoop()
379366 });
380367
381368 $ that = $ this ;
382- $ loop ->addTimer (0.001 , function (Timer $ timer ) use ($ process , $ that ) {
383- $ process ->start ($ timer ->getLoop ());
384- $ process ->terminate (SIGSTOP );
385-
386- $ that ->assertSoon (function () use ($ process , $ that ) {
387- $ that ->assertTrue ($ process ->isStopped ());
388- $ that ->assertTrue ($ process ->isRunning ());
389- $ that ->assertEquals (SIGSTOP , $ process ->getStopSignal ());
390- });
391-
392- $ process ->terminate (SIGCONT );
393-
394- $ that ->assertSoon (function () use ($ process , $ that ) {
395- $ that ->assertFalse ($ process ->isStopped ());
396- $ that ->assertEquals (SIGSTOP , $ process ->getStopSignal ());
397- });
369+ $ process ->start ($ loop );
370+ $ process ->terminate (SIGSTOP );
371+
372+ $ that ->assertSoon (function () use ($ process , $ that ) {
373+ $ that ->assertTrue ($ process ->isStopped ());
374+ $ that ->assertTrue ($ process ->isRunning ());
375+ $ that ->assertEquals (SIGSTOP , $ process ->getStopSignal ());
376+ });
377+
378+ $ process ->terminate (SIGCONT );
379+
380+ $ that ->assertSoon (function () use ($ process , $ that ) {
381+ $ that ->assertFalse ($ process ->isStopped ());
382+ $ that ->assertEquals (SIGSTOP , $ process ->getStopSignal ());
398383 });
399384
400385 $ loop ->run ();
@@ -444,7 +429,12 @@ function ($output) use (&$stdErr) {
444429 }
445430 );
446431
447- $ loop ->tick ();
432+ // tick loop once
433+ $ loop ->addTimer (0 , function () use ($ loop ) {
434+ $ loop ->stop ();
435+ });
436+ $ loop ->run ();
437+
448438 sleep (1 ); // comment this line out and it works fine
449439
450440 $ loop ->run ();
0 commit comments