Skip to content

Commit b083aeb

Browse files
committed
More Windows test suite improvements
Cleans up some formatting, uses a more precise check for execution duration, and applies more reliable escaping of Windows command-line parameters.
1 parent 2267d1d commit b083aeb

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

tests/AbstractProcessTest.php

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,10 @@ public function testProcessWithDefaultCwdAndEnv()
104104
public function testProcessWithCwd()
105105
{
106106
$cmd = $this->getPhpCommandLine('echo getcwd(), PHP_EOL;');
107-
108-
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
109-
$testCwd = 'C:\\';
110-
} else {
111-
$testCwd = '/';
112-
}
107+
$cwd = defined('PHP_WINDOWS_VERSION_BUILD') ? 'C:\\' : '/';
113108

114109
$loop = $this->createLoop();
115-
$process = new Process($cmd, $testCwd);
110+
$process = new Process($cmd, $cwd);
116111

117112
$output = '';
118113

@@ -125,7 +120,7 @@ public function testProcessWithCwd()
125120

126121
$loop->run();
127122

128-
$this->assertSame($testCwd . PHP_EOL, $output);
123+
$this->assertSame($cwd . PHP_EOL, $output);
129124
}
130125

131126
public function testProcessWithEnv()
@@ -193,7 +188,7 @@ public function testStartAndAllowProcessToExitSuccessfullyUsingEventLoop()
193188
public function testStartInvalidProcess()
194189
{
195190
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
196-
$this->markTestSkipped('Windows does not have an executable flag. This test does not make sense on Windows.');
191+
$this->markTestSkipped('Windows does not have an executable flag.');
197192
}
198193

199194
$cmd = tempnam(sys_get_temp_dir(), 'react');
@@ -321,26 +316,16 @@ public function testTerminateWithStopAndContinueSignalsUsingEventLoop()
321316
$this->assertFalse($process->isTerminated());
322317
}
323318

324-
public function outputSizeProvider() {
325-
return [ [1000, 5], [10000, 5], [100000, 5] ];
326-
}
327-
328319
/**
329-
* @dataProvider outputSizeProvider
320+
* @dataProvider provideOutputSizeAndExpectedMaxDuration
330321
*/
331-
public function testProcessOutputOfSize($size, $expectedMaxDuration = 5)
322+
public function testProcessWithFixedOutputSize($size, $expectedMaxDuration = 5)
332323
{
333-
// Note: very strange behaviour of Windows (PHP 5.5.6):
334-
// on a 1000 long string, Windows succeeds.
335-
// on a 10000 long string, Windows fails to output anything.
336-
// On a 100000 long string, it takes a lot of time but succeeds.
337-
$cmd = $this->getPhpBinary() . ' -r ' . escapeshellarg('echo str_repeat(\'o\', '.$size.'), PHP_EOL;');
338-
339-
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
340-
// Windows madness! for some obscure reason, the whole command lines needs to be
341-
// wrapped in quotes (?!?)
342-
$cmd = '"'.$cmd.'"';
343-
}
324+
// Note: very strange behaviour of Windows (PHP 5.5.6):
325+
// on a 1000 long string, Windows succeeds.
326+
// on a 10000 long string, Windows fails to output anything.
327+
// On a 100000 long string, it takes a lot of time but succeeds.
328+
$cmd = $this->getPhpCommandLine(sprintf('echo str_repeat("o", %d), PHP_EOL;', $size));
344329

345330
$loop = $this->createLoop();
346331
$process = new Process($cmd);
@@ -354,17 +339,25 @@ public function testProcessOutputOfSize($size, $expectedMaxDuration = 5)
354339
});
355340
});
356341

357-
$startTime = time();
358-
342+
$startTime = microtime(true);
359343
$loop->run();
344+
$endTime = microtime(true);
360345

361-
$endTime = time();
346+
$expectedOutput = str_repeat('o', $size) . PHP_EOL;
362347

363-
$this->assertEquals($size + strlen(PHP_EOL), strlen($output));
364-
$this->assertSame(str_repeat('o', $size) . PHP_EOL, $output);
348+
$this->assertEquals(strlen($expectedOutput), strlen($output));
349+
$this->assertSame($expectedOutput, $output);
365350
$this->assertLessThanOrEqual($expectedMaxDuration, $endTime - $startTime, "Process took longer than expected.");
366351
}
367352

353+
public function provideOutputSizeAndExpectedMaxDuration()
354+
{
355+
return [
356+
[1000, 5],
357+
[10000, 5],
358+
[100000, 5],
359+
];
360+
}
368361

369362
/**
370363
* Execute a callback at regular intervals until it returns successfully or
@@ -404,6 +397,16 @@ private function getPhpBinary()
404397

405398
private function getPhpCommandLine($phpCode)
406399
{
407-
return $this->getPhpBinary() . ' -r ' . escapeshellarg($phpCode);
400+
/* The following is a suitable workaround for Windows given some
401+
* escapeshellarg() incompatibilies in older PHP versions and knowledge
402+
* that we're only escaping echo statements destined for "php -r".
403+
*
404+
* See: http://php.net/manual/en/function.escapeshellarg.php#114873
405+
*/
406+
$phpCode = defined('PHP_WINDOWS_VERSION_BUILD')
407+
? '"' . addcslashes($phpCode, '\\"') . '"'
408+
: escapeshellarg($phpCode);
409+
410+
return $this->getPhpBinary() . ' -r ' . $phpCode;
408411
}
409412
}

0 commit comments

Comments
 (0)