phpphpunitwarnings

PHPUnit throws "Warning: date(): It is not safe..."


When running phpunit --coverage-html I get the well-known warning about timezones.

PHP Warning: date(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.

Everything works as expected, but it becomes really annoying.

Of course, I could solve this by changing my php.ini, but I'd prefer to avoid it, if possible, in order to keep some server-agnosticism. In addition, I don't want to prevent this warning to appear if triggered by my testable code.

Is there a way to define the default timezone only for internal PHPUnit operations?


Solution

  • I set the TimeZone in the bootstrap.php file.

    <?php
    // PHP and Web server settings
    error_reporting(E_ALL | E_STRICT);
    date_default_timezone_set("America/Toronto");       // Set the default timezone
    $_SERVER['SERVER_NAME'] = 'http://myserver';       // Set Web Server name
    
    // Process the Include Path to allow the additional application to be set.
    $IncludePaths = explode( PATH_SEPARATOR, get_include_path() );
    $NewIncludePaths = array_merge( $IncludePaths, array(dirname(__FILE__) ));
    set_include_path( implode( PATH_SEPARATOR, array_unique($NewIncludePaths)));    // Update Include Path
    
    //define('PHPUNIT_RUNNING', 1); // Indicate to the code that Automated Testing is running.
    ?>