PHPUnit 抛出“警告:date():它不安全...”

问题描述 投票:0回答:1

运行时

phpunit --coverage-html
我收到了有关时区的众所周知的警告。

PHP警告:date():依赖系统时区是不安全的 设置。您需要使用 date.timezone 设置或 date_default_timezone_set() 函数。如果您使用过其中任何一个 方法,并且您仍然收到此警告,您很可能 时区标识符拼写错误。我们选择时区“UTC” 现在,但请设置 date.timezone 以选择您的时区。

一切都按预期进行,但它变得非常烦人。

当然,我可以通过更改我的

php.ini
来解决这个问题,但如果可能的话,我宁愿避免它,以保持一些服务器不可知论。此外,如果由我的可测试代码触发,我不想阻止出现此警告。

有没有办法仅为内部 PHPUnit 操作定义默认时区?

php phpunit warnings
1个回答
12
投票

我在 bootstrap.php 文件中设置了时区。

<?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.
?>
© www.soinside.com 2019 - 2024. All rights reserved.