您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit断言-assertFileIsReadable
发布时间:2021-10-02 20:50:04编辑:雪饮阅读()
assertFileIsReadable断言用于断言一个文件是文件并且可读。
FileIsReadableTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class FileIsReadableTest extends TestCase
{
public function testFailure(): void
{
$this->assertFileIsReadable('/path/to/file');
}
public function testNoReadable(): void
{
$this->assertFileIsReadable('/usr/local/no_read');
}
public function testSuccess(): void
{
$this->assertFileIsReadable('/usr/local/read');
}
}
use PHPUnit\Framework\TestCase;
final class FileIsReadableTest extends TestCase
{
public function testFailure(): void
{
$this->assertFileIsReadable('/path/to/file');
}
public function testNoReadable(): void
{
$this->assertFileIsReadable('/usr/local/no_read');
}
public function testSuccess(): void
{
$this->assertFileIsReadable('/usr/local/read');
}
}
当时这个测试结果就有点不太符合了:
[root@localhost ~]# ls -l /usr/local/read /usr/local/no_read
--wx-wx-wx 1 root root 0 Oct 2 08:39 /usr/local/no_read
-rwxrwxrwx 1 root root 0 Oct 2 08:42 /usr/local/read
[root@localhost ~]# /usr/local/php734/bin/php /usr/local/phpunit-9.5.8.phar /usr/local/organizing/tests/FileIsReadableTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
F.. 3 / 3 (100%)
Time: 00:00, Memory: 18.00 MB
There was 1 failure:
1) FileIsReadableTest::testFailure
Failed asserting that file "/path/to/file" exists.
/usr/local/organizing/tests/FileIsReadableTest.php:8
FAILURES!
Tests: 3, Assertions: 5, Failures: 1.
[root@localhost ~]# ls /path/to/file
ls: cannot access /path/to/file: No such file or directory
可以看到/path/to/file文件确实不存在的,所以对于其的断言自然是错误的。
但是对于/usr/local/no_read文件我给的权限是333,不可读,但是竟然断言成功?
最后就是对于/usr/local/read文件我给的是777自然可读,那么断言没有问题也就是必然的了。
关键字词:phpunit,assertFileIsReadable