Phpunit: Process isolation under phpdbg throws exceptions

Created on 30 Nov 2019  ·  3Comments  ·  Source: sebastianbergmann/phpunit

| Q | A
| --------------------| ---------------
| PHPUnit version | 8.4.3
| PHP version | 7.4.0 (phpdbg SAPI)
| Installation Method | Composer

Summary

Use of process isolation throws exceptions under phpdbg. At first I thought this was a regression of #3772, but it seems to be a separate cause.

I think was able to track the bug down to this line in TestCaseMethod.tpl (there's also similar code in TestCaseClass.tpl). Both these lines attempt to read from STDOUT, without first checking if it is a readable stream.

If I put in a debug statement using stream_get_meta_data(STDOUT), you can see that STDOUT is not readable:

array(9) {
  ["timed_out"]=>
  bool(false)
  ["blocked"]=>
  bool(true)
  ["eof"]=>
  bool(false)
  ["wrapper_type"]=>
  string(3) "PHP"
  ["stream_type"]=>
  string(5) "STDIO"
  ["mode"]=>
  string(2) "wb"
  ["unread_bytes"]=>
  int(0)
  ["seekable"]=>
  bool(false)
  ["uri"]=>
  string(12) "php://stdout"
}

See also:

Current behavior

Any test that uses process isolation under phpdbg does not run, and instead throws an exception similar to: PHPUnit\Framework\Exception: Notice: stream_get_contents(): read of 8192 bytes failed with errno=9 Bad file descriptor in Standard input code on line 336.

How to reproduce

Run this test under phpdbg:

<?php

use PHPUnit\Framework\TestCase;

class ReproTest extends TestCase
{
    /**
     * @doesNotPerformAssertions
     * @runInSeparateProcess
     */
    public function testProcessIsolation()
    {
    }
}

And see this output:

$ phpdbg -qrr vendor/bin/phpunit
PHPUnit 8.4.3 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 121 ms, Memory: 6.00 MB

There was 1 error:

1) ReproTest::testProcessIsolation
PHPUnit\Framework\Exception: Notice: stream_get_contents(): read of 8192 bytes failed with errno=9 Bad file descriptor in Standard input code on line 336

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

Expected behavior

Process isolation should work for phpdbg in the same way as it does for other PHP SAPIs.

Output of composer info | sort

doctrine/instantiator              1.3.0   A small, lightweight utility to instantiate objects in PHP without invoking their constructors
myclabs/deep-copy                  1.9.3   Create deep copies (clones) of your objects
phar-io/manifest                   1.0.3   Component for reading phar.io manifest information from a PHP Archive (PHAR)
phar-io/version                    2.0.1   Library for handling version information and constraints
phpdocumentor/reflection-common    2.0.0   Common reflection classes used by phpdocumentor to reflect the code structure
phpdocumentor/reflection-docblock  4.3.2   With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.
phpdocumentor/type-resolver        1.0.1   A PSR-5 based resolver of Class names, Types and Structural Element Names
phpspec/prophecy                   1.9.0   Highly opinionated mocking framework for PHP 5.3+
phpunit/php-code-coverage          7.0.10  Library that provides collection, processing, and rendering functionality for PHP code coverage information.
phpunit/php-file-iterator          2.0.2   FilterIterator implementation that filters files based on a list of suffixes.
phpunit/php-text-template          1.2.1   Simple template engine.
phpunit/php-timer                  2.1.2   Utility class for timing
phpunit/php-token-stream           3.1.1   Wrapper around PHP's tokenizer extension.
phpunit/phpunit                    8.4.3   The PHP Unit Testing framework.
sebastian/code-unit-reverse-lookup 1.0.1   Looks up which function or method a line of code belongs to
sebastian/comparator               3.0.2   Provides the functionality to compare PHP values for equality
sebastian/diff                     3.0.2   Diff implementation
sebastian/environment              4.2.3   Provides functionality to handle HHVM/PHP environments
sebastian/exporter                 3.1.2   Provides the functionality to export PHP variables for visualization
sebastian/global-state             3.0.0   Snapshotting of global state
sebastian/object-enumerator        3.0.3   Traverses array structures and object graphs to enumerate all referenced objects
sebastian/object-reflector         1.1.1   Allows reflection of object attributes, including inherited and non-public ones
sebastian/recursion-context        3.0.0   Provides functionality to recursively process PHP variables
sebastian/resource-operations      2.0.1   Provides a list of PHP built-in functions that operate on resources
sebastian/type                     1.1.3   Collection of value objects that represent the types of the PHP type system
sebastian/version                  2.0.1   Library that helps with managing the version number of Git-hosted PHP projects
symfony/polyfill-ctype             v1.13.0 Symfony polyfill for ctype functions
theseer/tokenizer                  1.1.3   A small library for converting tokenized PHP source code into XML and potentially other formats
webmozart/assert                   1.6.0   Assertions to validate method input/output with nice error messages.
featurprocess-isolation typbug

All 3 comments

I confirm.

I will not invest time in debugging an issue that only affects PHPDBG. To be honest, I am thinking about removing support for it from php-code-coverage as well as PHPUnit itself.

Just for the sake of an example, when using regular PHP it will also report the stream as non-readable:

php -r 'echo stream_get_meta_data(STDOUT)["mode"], "\n";' | cat
// Outputs: wb

This problem first appeared in PHP 7.4.0. This might be a PHP bug, but I don't know better.

Was this page helpful?
0 / 5 - 0 ratings