milanowicz / php-testing
PHPUnit 测试库
1.2.2
2022-03-18 15:52 UTC
Requires
- php: ^8.0
Requires (Dev)
- infection/infection: ^0.26
- jangregor/phpstan-prophecy: ^1.0
- markrogoyski/math-php: ^2.5
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^1.4
- phpunit/phpunit: ^9.5
- slevomat/coding-standard: ^7.0
- dev-master
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-dependabot/composer/symfony/string-5.4.21
- dev-dependabot/composer/symfony/console-5.4.21
- dev-dependabot/composer/symfony/finder-5.4.21
- dev-dependabot/composer/colinodell/json5-2.3.0
- dev-dependabot/composer/phpstan/phpdoc-parser-1.15.3
This package is auto-updated.
Last update: 2024-09-29 16:11:06 UTC
README
PHPUnit 测试库。
用法
使用 Composer 安装
> composer require --dev milanowicz/php-testing
方法
在 ClassTest 中使用抽象类的示例
class UnitTest extends Milanowicz\Testing\TestCase { public function testMethod(): void { // All Traits in abstract Milanowicz\Testing\TestCase are extends! // See below for further details from Trait methods } }
或者导入 Trait(s) 使用它们
class UnitTest extends What\Ever\TestCase { use Milanowicz\Testing\TestTrait; public function testMethod(): void { // Access to private or protected Method $this->accessMethod((object) Class, (string) Method); // Run test(s) in it and when an assertion failed would throw, see message and data for the reason $this->catchAssertionFailing((array) $data, function ($data) { $this->assertEquals('whatEver', $data['whatEver']); }, (string) $catchData = ExpectationFailedException::class); // Create class without constructor $this->createInstanceWithoutConstructor((string) Class); // Call a private or protected Method with argument(s) $this->invokeMethod((object) Class, (string) Method, (mixed) ArgumentsForMethod); // Set a value to private or protected property $this->setProperty((object) Class, (string) Property, (mixed) PropertyValue); // Get a value from a private or protected property back $this->getProperty((object) Class, (string) Property); // Execute test of number tries to check, if it runs multiply times successfully $this->testLoops((callable) Function, (int) NumberOfTries, (int) NumberOfErrors); // Execute test and when it's throw an exception, then try it again $this->tryTest((callable) Function, (int) NumberOfTries); } }
TestPerformanceTrait 用于执行两个函数并查看哪个更快
class UnitTest extends What\Ever\TestCase { use Milanowicz\Testing\TestPerformanceTrait; public function testMethod(): void { // Example functions $cb1 = static function () { usleep(100); }; $cb2 = static function () { usleep(200); }; // Call both methods after each other and save run times from them $this->measureTime((callable) $cb1, (callable) $cb2, (int) $n = 20); // Check AVG and Student-Test $this->checkPerformance((bool) $function1 = true, (float) $pValue = 0.05); // $cb1 should be faster $this->checkMeanTime((bool) $function1 = true); // $cb2 should be slower and throw an Exception $this->checkMeanTime((bool) false); // Check if $cb1 is significant faster $this->checkStudentTest((float) $pValue = 0.05); // Get all time measures $this->getTimeMeasures(); // Get both in array with AVG, STD and Median $this->getTimeStats(); // Get result from Student-T test $this->getTimeSignificance(); // Clear all times and measures $this->clearTimes(); } }
异常
使用 AssertionFailedException 获取异常消息中的数据
$e = new AssertionFailedException( (string) $message = 'Testing', (array) $data = [1], (int) $code = 1, (null|Throwable) $previous = null, ); $e->toArray(); // => [1] $e->count(); // => 1 $e->toString(); // => Message and Data as String // and all Exception methods are available: $e->getMessage(); // => Message and Data as String $e->getCode(); // => 1 $e->getPrevious(); // => null OR a Throwable object
开发
运行所有测试套件
> composer tests
运行 PHP 代码格式化
> composer style
运行 PHPStan 分析代码
> composer analyze
运行 PHPUnit 测试
> composer test
通过 Infection 运行突变测试
> composer infection