vladahejda / phpunit-assert-exception
断言异常/抛出 Throwable/错误 PHPUnit 特性。
v1.3.0
2017-02-23 10:09 UTC
Requires
- php: ~7.1
- phpunit/phpunit: ~6.0
README
AssertException
是一个特性,因此可以轻松地在您的测试用例中使用。
- 为了与 PHP 7.1 和 PHPUnit 6 兼容,需要版本
1.3
。 - 为了与 PHP 7.1 兼容,需要版本
1.2
。 - 为了与 PHP 7 兼容,需要版本
1.1
。 - 为了与 PHP 5 兼容,需要版本
1.0
。
安装
$ composer require vladahejda/phpunit-assert-exception
用法
<?php class MyTest extends \PHPUnit\Framework\TestCase { use VladaHejda\AssertException; public function testMultipleExceptionsAtOnce() { $test = function () { // here comes some test stuff of your unit (tested class) // which you expect that will throw an Exception throw new InvalidArgumentException('Some message 12345', 100); }; // just test if function throws an Exception $this->assertException($test); // pass // test class of an Exception $this->assertException($test, InvalidArgumentException::class); // pass // test Exception code $this->assertException($test, null, 100); // pass // test Exception message $this->assertException($test, null, null, 'Some message 12345'); // pass $this->assertException($test, null, null, 'Some message'); // also pass, because it checks on substring level // test all $this->assertException($test, InvalidArgumentException::class, 100, 'Some message 12345'); // pass // and here some failing tests // wrong class $this->assertException($test, ErrorException::class); // fail // wrong code $this->assertException($test, InvalidArgumentException::class, 200); // fail // wrong message $this->assertException($test, InvalidArgumentException::class, 100, 'Bad message'); // fail } }
还可以查看 assertError
和 assertThrowable
方法,这些方法用于测试是否抛出了 PHP 内部的 Error
或任何 Throwable
。