inspirum / phpunit-extension
PHPUnit 扩展,包含额外的断言
v1.0.0
2024-05-17 12:23 UTC
Requires
- php: ^8.1
- phpunit/phpunit: ^10.5
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-17 13:00:01 UTC
README
PHPUnit 扩展,包含额外的断言和其他辅助工具。
- 支持一些已弃用的功能
- 易于实现
特性
支持已弃用的断言方法 withConsecutive
在 PHPUnit 10.0 之前
use PHPUnit\Framework\TestCase; final class CalculatorTest extends TestCase { public function testMultiply(): void { $mock = $this->createMock(Calculator::class); $arguments = [[1,2,3], [4,5,6]] $responses = [6, 120] $mock->expects(self::exactly(count($arguments)))->method('multiply') ->withConsecutive(...$arguments) ->willReturnOnConsecutiveCalls(...$responses); // ... test } }
在 PHPUnit 10.0 之后
use Inspirum\PHPUnit\Extension; use PHPUnit\Framework\TestCase; final class CalculatorTest extends TestCase { use Extension; public function testMultiply(): void { $mock = $this->createMock(Calculator::class); $arguments = [[1,2,3], [4,5,6]] $responses = [6, 120] $mock->expects(self::exactly(count($arguments)))->method('multiply') ->will(self::withConsecutive($arguments, $responses)); // ... test } }
系统要求
安装
运行 composer require 命令
$ composer require inspirum/phpunit-extension
或添加依赖到您的 composer.json
"inspirum/phpunit-extension": "^1.0"
使用方法
验证参数和响应
$mock->expects(self::exactly(2))->method('example') ->will(self::withConsecutive( arguments: [ [1, 2, 0.1], [2, 3, 0.01], ], responses: [ true, false, ], )); self::assertTrue($mock->example(1, 2, 0.1)); self::assertFalse($mock->example(2, 3, 0.01));
可选响应
$mock->expects(self::exactly(2))->method('example') ->will(self::withConsecutive( arguments: [ [1, 2, 0.1], [2, 3, 0.01], ], )); $mock->example(1, 2, 0.1); $mock->example(2, 3, 0.01);
简化每次调用相同响应的情况
$mock->expects(self::exactly(2))->method('example') ->will(self::withConsecutive( arguments: [ [1, 2, 0.1], [2, 3, 0.01], ], responses: true, )); self::assertTrue($mock->example(1, 2, 0.1)); self::assertTrue($mock->example(2, 3, 0.01));
支持抛出异常
$mock->expects(self::exactly(2))->method('example') ->will(self::withConsecutive( arguments: [ [1, 2, 0.1], [2, 3, 0.01], ], responses: [ true, new RuntimeException('Custom error'), ], )); self::assertTrue($mock->example(1, 2, 0.1)); try { $mock->example(2, 3, 0.01); } catch (RuntimeException $exception) { self::assertSame('Custom error', $exception->getMessage()); }
测试
要运行单元测试,运行
$ composer test:test
要显示覆盖率,运行
$ composer test:coverage
贡献
请参阅CONTRIBUTING和CODE_OF_CONDUCT以获取详细信息。
安全
如果您发现任何安全相关的问题,请发送电子邮件至tomas.novotny@inspirum.cz,而不是使用问题跟踪器。
鸣谢
许可协议
MIT 许可协议(MIT)。请参阅许可文件以获取更多信息。