Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect STDERR to STDOUT when running PHPT tests #1550

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Extensions/PhptTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ public function run(PHPUnit_Framework_TestResult $result = null)

$result->startTest($this);

// Redirects STDERR to STDOUT
$php->setUseStderrRedirection(true);

if (isset($sections['SKIPIF'])) {
$jobResult = $php->runJob($sections['SKIPIF'], $this->settings);

Expand Down
66 changes: 66 additions & 0 deletions src/Util/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* file that was distributed with this source code.
*/

use SebastianBergmann\Environment\Runtime;

/**
* Utility methods for PHP sub-processes.
*
Expand All @@ -21,6 +23,53 @@
*/
abstract class PHPUnit_Util_PHP
{
/**
* @var Runtime
*/
protected $runtime;

/**
* @var bool
*/
protected $stderrRedirection = false;

/**
* Creates internal Runtime instance.
*/
public function __construct()
{
$this->runtime = new Runtime();
}

/**
* Defines if should use STDERR redirection or not.
*
* Then $stderrRedirection is TRUE, STDERR is redirected to STDOUT.
*
* @throws PHPUnit_Framework_Exception
* @param boolean $stderrRedirection
*
* @return null
*/
public function setUseStderrRedirection($stderrRedirection)
{
if (!is_bool($stderrRedirection)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
}

$this->stderrRedirection = $stderrRedirection;
}

/**
* Returns TRUE if uses STDERR redirection or FALSE if not.
*
* @return boolean
*/
public function useStderrRedirection()
{
return $this->stderrRedirection;
}

/**
* @return PHPUnit_Util_PHP
* @since Method available since Release 3.5.12
Expand Down Expand Up @@ -53,6 +102,23 @@ public function runTestJob($job, PHPUnit_Framework_Test $test, PHPUnit_Framework
);
}

/**
* Returns the command based into the configurations.
*
* @return string
*/
public function getCommand(array $settings)
{
$command = $this->runtime->getBinary();
$command .= $this->settingsToParameters($settings);

if (true === $this->stderrRedirection) {
$command .= ' 2>&1';
}

return $command;
}

/**
* Runs a single job (PHP code) using a separate PHP process.
*
Expand Down
6 changes: 1 addition & 5 deletions src/Util/PHP/Default.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
* file that was distributed with this source code.
*/

use SebastianBergmann\Environment\Runtime;

/**
* Default utility for PHP sub-processes.
*
Expand All @@ -33,10 +31,8 @@ class PHPUnit_Util_PHP_Default extends PHPUnit_Util_PHP
*/
public function runJob($job, array $settings = array())
{
$runtime = new Runtime;

$process = proc_open(
$runtime->getBinary() . $this->settingsToParameters($settings),
$this->getCommand($settings),
array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
Expand Down
6 changes: 1 addition & 5 deletions src/Util/PHP/Windows.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
* file that was distributed with this source code.
*/

use SebastianBergmann\Environment\Runtime;

/**
* Windows utility for PHP sub-processes.
*
Expand Down Expand Up @@ -38,16 +36,14 @@ class PHPUnit_Util_PHP_Windows extends PHPUnit_Util_PHP_Default
*/
public function runJob($job, array $settings = array())
{
$runtime = new Runtime;

if (false === $stdout_handle = tmpfile()) {
throw new PHPUnit_Framework_Exception(
'A temporary file could not be created; verify that your TEMP environment variable is writable'
);
}

$process = proc_open(
$runtime->getBinary() . $this->settingsToParameters($settings),
$this->getCommand($settings),
array(
0 => array('pipe', 'r'),
1 => $stdout_handle,
Expand Down
9 changes: 9 additions & 0 deletions tests/TextUI/phpt-stderr.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--TEST--
GH-1169: PHPT runner doesn't look at STDERR.
--FILE--
<?php
$stderr = fopen('php://stderr', 'w');
fwrite($stderr, 'PHPUnit must look at STDERR when running PHPT tests.'.PHP_EOL);
?>
--EXPECTF--
PHPUnit must look at STDERR when running PHPT tests.
80 changes: 80 additions & 0 deletions tests/Util/PHPTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* @package PHPUnit
* @author Henrique Moody <henriquemoody@gmail.com>
* @copyright Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @link http://www.phpunit.de/
* @covers PHPUnit_Util_PHP
*/
class PHPUnit_Util_PHPTest extends PHPUnit_Framework_TestCase
{
public function testShouldNotUseStderrRedirectionByDefault()
{
$phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');

$this->assertFalse($phpMock->useStderrRedirection());
}

public function testShouldDefinedIfUseStderrRedirection()
{
$phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
$phpMock->setUseStderrRedirection(true);

$this->assertTrue($phpMock->useStderrRedirection());
}

public function testShouldDefinedIfDoNotUseStderrRedirection()
{
$phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
$phpMock->setUseStderrRedirection(false);

$this->assertFalse($phpMock->useStderrRedirection());
}

/**
* @expectedException PHPUnit_Framework_Exception
* @expectedExceptionMessage Argument #1 (No Value) of PHPUnit_Util_PHP::setUseStderrRedirection() must be a boolean
*/
public function testShouldThrowsExceptionWhenStderrRedirectionVariableIsNotABoolean()
{
$phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
$phpMock->setUseStderrRedirection(null);
}

public function testShouldUseGivenSettingsToCreateCommand()
{
$phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');

$settings = array(
'allow_url_fopen=1',
'auto_append_file=',
'display_errors=1',
);

$expectedCommandFormat = '%s -d allow_url_fopen=1 -d auto_append_file= -d display_errors=1';
$actualCommand = $phpMock->getCommand($settings);

$this->assertStringMatchesFormat($expectedCommandFormat, $actualCommand);
}

public function testShouldRedirectStderrToStdoutWhenDefined()
{
$phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
$phpMock->setUseStderrRedirection(true);

$expectedCommandFormat = '%s 2>&1';
$actualCommand = $phpMock->getCommand(array());

$this->assertStringMatchesFormat($expectedCommandFormat, $actualCommand);
}
}