Phpunit: Cannot determine time at which the request started

Created on 27 Feb 2018  路  5Comments  路  Source: sebastianbergmann/phpunit

| Q | A
| --------------------| ---------------
| PHPUnit version | 7.0.2
| PHP version | 7.2.2
| Installation Method | Composer

In my tests, I override $_SERVER to test how my library parses the current request. Since I updated to PHPUnit 7, I got the following error:

PHP Fatal error: Uncaught SebastianBergmann\Timer\RuntimeException: Cannot determine time at which the request started in /mnt/hgfs/www/brick/http/vendor/phpunit/php-timer/src/Timer.php:64

This is because the implementation of Timer::timeSinceStartOfRequest() relies on the presence of $_SERVER['REQUEST_TIME_FLOAT'] or $_SERVER['REQUEST_TIME'].

I can of course wipe $_SERVER while keeping REQUEST_TIME_FLOAT in my tests, but this is a burden that could probably be avoided by PHPUnit.

Would it be possible to read this variable (or the return value of microtime(true), for that matter) before running the tests, to make Timer independent of $_SERVER?


Output of composer info | sort, as requested:

doctrine/instantiator              1.1.0   A small, lightweight utility to instantiate objects in PHP without invoking their constructors
guzzle/guzzle                      v3.9.3  PHP HTTP client. This library is deprecated in favor of https://packagist.org/packages/guzzlehttp/guzzle
myclabs/deep-copy                  1.7.0   Create deep copies (clones) of your objects
phar-io/manifest                   1.0.1   Component for reading phar.io manifest information from a PHP Archive (PHAR)
phar-io/version                    1.0.1   Library for handling version information and constraints
phpdocumentor/reflection-common    1.0.1   Common reflection classes used by phpdocumentor to reflect the code structure
phpdocumentor/reflection-docblock  4.3.0   With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a Doc...
phpdocumentor/type-resolver        0.4.0  
phpspec/prophecy                   1.7.5   Highly opinionated mocking framework for PHP 5.3+
phpunit/php-code-coverage          6.0.1   Library that provides collection, processing, and rendering functionality for PHP code coverage information.
phpunit/php-file-iterator          1.4.5   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.0.0   Utility class for timing
phpunit/php-token-stream           3.0.0   Wrapper around PHP's tokenizer extension.
phpunit/phpunit                    7.0.2   The PHP Unit Testing framework.
phpunit/phpunit-mock-objects       6.0.1   Mock Object library for PHPUnit
psr/log                            1.0.2   Common interface for logging libraries
satooshi/php-coveralls             v1.1.0  PHP client library for Coveralls API
sebastian/code-unit-reverse-lookup 1.0.1   Looks up which function or method a line of code belongs to
sebastian/comparator               2.1.3   Provides the functionality to compare PHP values for equality
sebastian/diff                     3.0.0   Diff implementation
sebastian/environment              3.1.0   Provides functionality to handle HHVM/PHP environments
sebastian/exporter                 3.1.0   Provides the functionality to export PHP variables for visualization
sebastian/global-state             2.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      1.0.0   Provides a list of PHP built-in functions that operate on resources
sebastian/version                  2.0.1   Library that helps with managing the version number of Git-hosted PHP projects
symfony/config                     v4.0.4  Symfony Config Component
symfony/console                    v4.0.4  Symfony Console Component
symfony/event-dispatcher           v2.8.34 Symfony EventDispatcher Component
symfony/filesystem                 v4.0.4  Symfony Filesystem Component
symfony/polyfill-mbstring          v1.7.0  Symfony polyfill for the Mbstring extension
symfony/stopwatch                  v4.0.4  Symfony Stopwatch Component
symfony/yaml                       v4.0.4  Symfony Yaml Component
theseer/tokenizer                  1.1.0   A small library for converting tokenized PHP source code into XML and potentially other formats
webmozart/assert                   1.3.0   Assertions to validate method input/output with nice error messages.

Most helpful comment

@karborator

As of version 6, PHPUnit does not perform this backup and restore operation for global and super-global variables by default anymore. It can be activated by using the --globals-backup option or setting backupGlobals="true" in the XML configuration file.

Source: https://phpunit.readthedocs.io/en/8.4/fixtures.html

All 5 comments

For anyone in the same situation, here is a temporary workaround:

class MyTest extends TestCase
{
    private $requestTimeFloat;

    public function setUp()
    {
        $this->requestTimeFloat = $_SERVER['REQUEST_TIME_FLOAT'];
    }

    public function tearDown()
    {
        $_SERVER['REQUEST_TIME_FLOAT'] = $this->requestTimeFloat;
    }
}

Simply enable the backup and restore of $GLOBALS and your problem should be solved.

It is indeed, thank you 馃憤

I can not find articles about how to "enable the backup and restore of $GLOBALS "

@karborator

As of version 6, PHPUnit does not perform this backup and restore operation for global and super-global variables by default anymore. It can be activated by using the --globals-backup option or setting backupGlobals="true" in the XML configuration file.

Source: https://phpunit.readthedocs.io/en/8.4/fixtures.html

Was this page helpful?
0 / 5 - 0 ratings