我试图在unitTest的setUp方法中获取内核根目录:
private string $databaseDirPath;
protected function setUp(): void
{
parent::setUp();
self::bootKernel();
$container = static::getContainer();
$this->databaseDirPath = $container->getParameter('kernel.root_dir').'/resources/database/' ;
}
/**
* @dataProvider provideInexistentFile
*/
public function testConvertReturnNull(string $file):void
{
if (!file_exists($file) || !is_readable($file)) {
$response = null;
}
$this->assertNull($response);
}
public function provideInexistentFile(): array
{
return [
'inexistent file' => [
$this->databaseDirPath. 'comdfsasd.csv',
],
'inexistent file' => [
$this->databaseDirPath. 'gbfdgbzsa.csv',
],
];
}
这就是错误:
$databaseDirPath在初始化之前不得访问
$databaseDirPath
属性的类型提示不正确。
setUp
方法不是构造函数。
变量初始化为类型提示不允许的null
。
使用以下内容:
private ?string $dateBaseDirPath = null;
更多信息可以在这个答案中找到。
在执行任何挂钩(如setUp
或beforeClass
(之前,都会调用数据提供程序。因此,尽管@Nicolai在技术上可能是正确的,说你的类型提示不正确,但这并不是问题的根源。
但您可以简单地添加";数据获取";到您的数据提供商:
public function provideInexistentFile(): array
{
self::bootKernel();
$container = static::getContainer();
$databaseDirPath = $container->getParameter('kernel.root_dir') . '/resources/database/';
return [
'inexistent file' => [
$databaseDirPath. 'comdfsasd.csv',
],
'inexistent file' => [
$databaseDirPath. 'gbfdgbzsa.csv',
],
];
}