您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit中NOTICE、WARNING、ERROR的断言支持
发布时间:2021-09-15 21:21:38编辑:雪饮阅读()
前面有了解到phpunit对DEPRECATED)错误的断言有expectDeprecation、expectDeprecationMessage、expectDeprecationMessageMatches这三种,特别是expectDeprecation是expectDeprecationMessage、expectDeprecationMessageMatche这两个所依赖的。
那么对于NOTICE、WARNING、ERROR类的错误同样也有上面类似的三种断言。
ErrorTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class ErrorTest extends TestCase
{
public function testNoticeCanBeExpected(): void
{
$this->expectNotice();
// (可选)测试讯息和某个字符串相等
$this->expectNoticeMessage('foo');
// 或者(可选)测试讯息和某个正则表达式匹配
$this->expectNoticeMessageMatches('/foo/');
\trigger_error('foo', \E_USER_NOTICE);
}
public function testWarningCanBeExpected(): void
{
$this->expectWarning();
// (可选)测试讯息和某个字符串相等
$this->expectWarningMessage('foo');
// 或者(可选)测试讯息和某个正则表达式匹配
$this->expectWarningMessageMatches('/foo/');
\trigger_error('foo', \E_USER_WARNING);
}
public function testErrorCanBeExpected(): void
{
$this->expectError();
// (可选)测试讯息和某个字符串相等
$this->expectErrorMessage('foo');
// 或者(可选)测试讯息和某个正则表达式匹配
$this->expectErrorMessageMatches('/foo/');
\trigger_error('foo', \E_USER_ERROR);
}
}
use PHPUnit\Framework\TestCase;
final class ErrorTest extends TestCase
{
public function testNoticeCanBeExpected(): void
{
$this->expectNotice();
// (可选)测试讯息和某个字符串相等
$this->expectNoticeMessage('foo');
// 或者(可选)测试讯息和某个正则表达式匹配
$this->expectNoticeMessageMatches('/foo/');
\trigger_error('foo', \E_USER_NOTICE);
}
public function testWarningCanBeExpected(): void
{
$this->expectWarning();
// (可选)测试讯息和某个字符串相等
$this->expectWarningMessage('foo');
// 或者(可选)测试讯息和某个正则表达式匹配
$this->expectWarningMessageMatches('/foo/');
\trigger_error('foo', \E_USER_WARNING);
}
public function testErrorCanBeExpected(): void
{
$this->expectError();
// (可选)测试讯息和某个字符串相等
$this->expectErrorMessage('foo');
// 或者(可选)测试讯息和某个正则表达式匹配
$this->expectErrorMessageMatches('/foo/');
\trigger_error('foo', \E_USER_ERROR);
}
}
运行效果如:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar C:\Users\Administrator\PhpstormProjects\untitled\ErrorTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)
Time: 00:00.005, Memory: 20.00 MB
OK (3 tests, 10 assertions)
关键字词:phpunit,NOTICE,WARNING,ERROR