[英]How to test dir creation errors in php (windows)
I have very short piece of code, but I cannot find the correct way how to test it using eg.我的代码很短,但我找不到正确的方法来测试它,例如。 PHPUnit.
PHPUnit。 Of course testing it in Linux is easier I think because of permissions over files but what I can do the test under Windows file system?
当然在 Linux 中测试它更容易我认为因为文件的权限但是我可以在 Windows 文件系统下进行测试吗? Here is the code:
这是代码:
private function createCacheDir(string $cacheDir): void
{
if (!is_dir($cacheDir)) {
if (!mkdir($cacheDir, 0755, true)) {
throw new \RuntimeException(sprintf('Dir %s cannot be created.', $cacheDir));
}
}
}
The problem is that such folder does not exists it is simply created and I do not know how to make second if falsy.问题是这样的文件夹不存在,它只是简单地创建,我不知道如何制作第二个如果是假的。 I will be happy for any working solution.
我将为任何可行的解决方案感到高兴。
As Alex Howansky said the most sensible answer is to use bovigo/vfsStream eg.:正如Alex Howansky所说,最明智的答案是使用bovigo/vfsStream例如:
public function setUp(): void
{
$this->baseCacheDir = vfsStream::setup('baseCacheDir');
}
public function testConstructorIfNotWritableDir(): void
{
$baseCacheDir = vfsStream::url('baseCacheDir');
$this->baseCacheDir->chmod(0444);
$cacheDir = $baseCacheDir.'/cacheDir';
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage(sprintf('Dir %s cannot be created.', $cacheDir));
$this->getObjectUnderTest($cacheDir);//Here is created Object under test and is executed this private method from the question.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.