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

Test swallows output buffer when run in a separate process #1149

Closed
decadent opened this issue Feb 19, 2014 · 9 comments
Closed

Test swallows output buffer when run in a separate process #1149

decadent opened this issue Feb 19, 2014 · 9 comments

Comments

@decadent
Copy link

<?php

class A extends \PHPUnit_Framework_TestCase {

    public function testOne() {
        print '1';
        file_put_contents('aaa1', ob_get_level());
    }

    /**
     * @runInSeparateProcess
     */
    public function testTwo() {
        print '2';
        file_put_contents('aaa2', ob_get_level());
    }

}
$ phpunit -v a.php
PHPUnit 3.7.31 by Sebastian Bergmann.

.1.

Time: 155 ms, Memory: 33.75Mb

OK (2 tests, 0 assertions)
$ cat aaa1
1
$ cat aaa2
2
@decadent
Copy link
Author

I know that both output buffering and running tests in separate processes were sources of annoying issues for phpunit, and both are not regular use cases, so consider this a low priority issue or something.
Only I suppose that the behavior should be consistent whether running in one process or in separate processes. So maybe the separate-process case should execute a course completely independent of the same-process case in order to not inherit any state and not cause any more trouble at all?

@donquixote
Copy link

I use process isolation a lot. It is important for testing stuff like a class loader.
And now I can't figure out how to print anything while "@runTestsInSeparateProcesses" and "@preserveGlobalState disabled" are on. It would be great to see this fixed.

@decadent
Copy link
Author

decadent commented May 1, 2014

@donquixote You can actually open php://stdout directly and write to it. The stuff printed will cause an error in deserialization of the test results and will be output as an Exception.

I guess this indicates that what's missing is transmission of the output from the test in the serialized data.

@sun
Copy link
Contributor

sun commented Jul 22, 2014

I can confirm that the output is not re-printed to STDOUT.

what's missing is transmission of the output from the test in the serialized data

The process isolation script template TestCaseMethod.tpl.dist does capture the output already:

    ob_start();
    $test->run($result);
    $output = ob_get_clean();
    print serialize(
      array(
        'testResult'    => $test->getResult(),
        'numAssertions' => $test->getNumAssertions(),
        'result'        => $result,
        'output'        => $output
      )
    );

If I get you right, then you'd like the subprocess output to get forwarded to STDOUT of the parent/main process?

PHPUnit_Util_PHP::processChildResult() immediately forwards STDERR already:

        if (!empty($stderr)) {
            $result->addError(
              $test,
              new PHPUnit_Framework_Exception(trim($stderr)), $time
            );

It seemingly forwards STDOUT, too:

            if ($childResult !== false) {
                if (!empty($childResult['output'])) {
                    print $childResult['output'];
                }

The $stdout variable itself is not consumed, and actually replaced with unserialize($stdout):

                if (strpos($stdout, "#!/usr/bin/env php\n") === 0) {
                    $stdout = substr($stdout, 19);
                }
                $childResult = unserialize(str_replace("#!/usr/bin/env php\n", '', $stdout));

So theoretically, output of the child process should be forwarded to the parent process already. Perhaps there's a bug in this code though. Can you debug?

Alternatively, the execution of tests running in isolation may be wrapped into another output buffer somewhere else.

@whatthejeff
Copy link
Contributor

This should be fixed in the next stable release. Thanks!

@sun
Copy link
Contributor

sun commented Jul 22, 2014

@whatthejeff Awesome! Question:

Is a technical analysis like mine above helpful? Or useless noise?

I'm just starting to dive deep, so I don't consider myself to be in a position to fix issues like this directly (just yet), but thought it might be helpful to share insights gathered while debugging other stuff ;)

Just curious whether comments like the above are a waste of everyone's time :)

@whatthejeff
Copy link
Contributor

@sun It's actually very helpful and very much appreciated!

@Flimm
Copy link

Flimm commented Jul 25, 2016

What version was this released in?

To answer my own question, it was released in v4.1.5.

$ git describe --contains 8337d8f93fc451252455120a8e0bd0cc7d5659cf
4.1.5~9

Which is odd, because I am running v4.7.6, and I am still experiencing this issue of PHPUnit swallowing output when run in separate processes.

@DavidSoussan
Copy link

I am experiencing the same issue: PHPUnit swallowing output when run in separate processes. using version 5.6.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants