我正在尝试模拟或存根“资源”类型。此代码确认 PHPUnit 可以使用“资源”类型,但当我尝试模拟它时,它会抛出错误。错误:无法存根或模拟不存在的类或接口“资源”。
我错过了什么?
<?php
use PHPUnit\Framework\TestCase;
class TestDataAccess extends TestCase
{
public function testGetDataVersion() : void
{
$file_pointer = fopen("../data/users.txt", "r");
self::assertTrue(is_resource($file_pointer)); //verifies PHPUnit can use "resource" type
// all of these die on error:
// Cannot stub or mock class or interface "resource" which does not exist
//$resourceMock = $this->createStub(gettype($file_pointer));
$resourceMock = $this->createStub(resource::class);
//$resourceMock = $this->createMock(resource::class);
/* what I'm actually trying to test
$data_format = "1.0";
$resourceMock
->method('getLine')
->will($this->onConsecutiveCalls("#version:".$data_format, false));
$result = my_method_under_test($resourceMock);
self::assertSame($data_format, $result);
*/
fclose($file_pointer);
}
}
?>
答案在注释中:“资源”实际上并不是一个类。可以包装文件指针以启用一些测试,或者可以使用另一个插件进行模拟。