PHPunit 出错并出现异常:'SplFileInfo' 的序列化是不允许的,即使它不应该
PHPunit is erroring out with Exception: Serialization of 'SplFileInfo' is not allowed even though it shouldn't
我有一个奇怪的错误。尝试在遗留代码库中实现 PHPunit。在代码库中,有一些关于设置包含路径的逻辑,这弄乱了 phpunit,我看不出如何。
代码:
$includePath = (realpath((__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'php')) . PATH_SEPARATOR);
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__));
foreach ($iterator as $fileInfo) {
if ('.' == $fileInfo->getFilename()) {
$includePath .= ($fileInfo->getPath() . PATH_SEPARATOR);
}
}
set_include_path($includePath);
如果我对 $includePath 进行硬编码,那么它就可以正常工作。但是,在我的 bootstrap 文件中使用此代码时出现错误:
Exception: Serialization of 'SplFileInfo' is not allowed
在每次测试中。这对我来说没有意义,因为我没有尝试在测试中的任何地方使用 RecursiveDirectoryIterator,也没有尝试序列化任何 RecursiveDirectoryIterator 文件。很奇怪。
我的单元测试看起来像:
class ReportTest extends PHPUnit_Framework_TestCase {
public function testAssertTrue() {
$this->assertTrue(true);
}
}
我 运行 phpunit 喜欢:
phpunit --bootstrap tests/bootstrap.php tests
bootstrap 在顶部有这些行(包含路径设置),然后包含其他内容,如我们的数据库连接等。
错误看起来像:
$ phpunit --bootstrap tests/test_bootstrap.php tests
PHPUnit 4.8.6 by Sebastian Bergmann and contributors.
EEEEEEEEE
Time: 340 ms, Memory: 19.00Mb
There were 9 errors:
1) ReportTest::testAssertTrue
Exception: Serialization of 'SplFileInfo' is not allowed
.... etc
RecursiveDirectoryIterator
正在为 foreach ($iterator as $fileInfo)
返回 SplFileInfo
。
该代码是否在全局名称space 中,在function/class 之外? PHPunit 将备份然后恢复全局变量 - 在 phpunit.xml 文件中用 backupGlobals
设置。 code that performs that backup 至少在最近的版本中,从该代码看来是为了执行 SplFileInfo
class 不喜欢的 unserialize(serialize($value));
。
如果您可以将该设置放入 class,并在全局 space 之外,它就不会尝试 'back it up'。
或者可能只是 unset($iterator);
完成后?
我有一个奇怪的错误。尝试在遗留代码库中实现 PHPunit。在代码库中,有一些关于设置包含路径的逻辑,这弄乱了 phpunit,我看不出如何。
代码:
$includePath = (realpath((__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'php')) . PATH_SEPARATOR);
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__));
foreach ($iterator as $fileInfo) {
if ('.' == $fileInfo->getFilename()) {
$includePath .= ($fileInfo->getPath() . PATH_SEPARATOR);
}
}
set_include_path($includePath);
如果我对 $includePath 进行硬编码,那么它就可以正常工作。但是,在我的 bootstrap 文件中使用此代码时出现错误:
Exception: Serialization of 'SplFileInfo' is not allowed
在每次测试中。这对我来说没有意义,因为我没有尝试在测试中的任何地方使用 RecursiveDirectoryIterator,也没有尝试序列化任何 RecursiveDirectoryIterator 文件。很奇怪。
我的单元测试看起来像:
class ReportTest extends PHPUnit_Framework_TestCase {
public function testAssertTrue() {
$this->assertTrue(true);
}
}
我 运行 phpunit 喜欢:
phpunit --bootstrap tests/bootstrap.php tests
bootstrap 在顶部有这些行(包含路径设置),然后包含其他内容,如我们的数据库连接等。
错误看起来像:
$ phpunit --bootstrap tests/test_bootstrap.php tests
PHPUnit 4.8.6 by Sebastian Bergmann and contributors.
EEEEEEEEE
Time: 340 ms, Memory: 19.00Mb
There were 9 errors:
1) ReportTest::testAssertTrue
Exception: Serialization of 'SplFileInfo' is not allowed
.... etc
RecursiveDirectoryIterator
正在为 foreach ($iterator as $fileInfo)
返回 SplFileInfo
。
该代码是否在全局名称space 中,在function/class 之外? PHPunit 将备份然后恢复全局变量 - 在 phpunit.xml 文件中用 backupGlobals
设置。 code that performs that backup 至少在最近的版本中,从该代码看来是为了执行 SplFileInfo
class 不喜欢的 unserialize(serialize($value));
。
如果您可以将该设置放入 class,并在全局 space 之外,它就不会尝试 'back it up'。
或者可能只是 unset($iterator);
完成后?