具有附加属性的 Composer 库,可增强 PHPUnit 的测试。
作曲家要求 --dev elishaeussler/phpunit-attributes
该库附带了一个随时可用的 PHPUnit 扩展。它必须在您的 PHPUnit 配置文件中注册:
<?xml 版本=“1.0”编码=“UTF-8”?> <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="供应商/phpunit/phpunit/phpunit.xsd" bootstrap =“供应商/autoload.php” >+ <extensions>+ <bootstrap class="EliasHaeusslerPHPUnitAttributesPHPUnitAttributesExtension" />+ </extensions> <testsuites> <测试套件名称=“单元”> <目录>测试</目录> </测试套件> </测试套件> <来源> <包括> <目录>src</目录> </包括> </来源> </phpunit>
某些属性可以使用自定义扩展参数进行配置。这些必须添加到扩展注册部分,如下所示:
<extensions>- <bootstrap class="EliasHaeusslerPHPUnitAttributesPHPUnitAttributesExtension" />+ <bootstrap class="EliasHaeusslerPHPUnitAttributesPHPUnitAttributesExtension">+ <parameter name="fancyParameterName" value="fancyParameterValue" />+ </bootstrap> </extensions>
该库附带以下属性:
#[RequiresClass]
#[RequiresPackage]
#[RequiresClass]
范围:类和方法级别
使用此属性,可以将测试或测试用例标记为仅在某个类存在时才执行。给定的类必须可由当前类加载器(通常是 Composer 的默认类加载器)加载。
默认情况下,会跳过需要不存在类的测试用例。但是,可以使用handleMissingClasses
扩展参数来配置此行为。如果设置为fail
,缺少类的测试用例将失败(默认为skip
):
<扩展名> <bootstrap class="EliasHaeusslerPHPUnitAttributesPHPUnitAttributesExtension"> <参数名称=“handleMissingClasses”值=“失败”/> </引导> </扩展名>
最终类 DummyTest 扩展了 TestCase { #[RequiresClass(AnImportantClass::class)]public function testDummyAction(): void{// ...} }
班级级别:
#[RequiresClass(AnImportantClass::class)]最终类 DummyTest 扩展 TestCase {public function testDummyAction(): void{// 如果缺少 AnImportantClass,则跳过。}public function testOtherDummyAction(): void{// 如果缺少 AnImportantClass,则跳过。} }
方法级别:
最终类 DummyTest 扩展了 TestCase { #[RequiresClass(AnImportantClass::class)]public function testDummyAction(): void{// 如果 AnImportantClass 缺失则跳过。}public function testOtherDummyAction(): void{// 不跳过。} }
班级级别:
#[RequiresClass(AnImportantClass::class, '此测试需要 `AnImportantClass` 类。')]final class DummyTest 扩展 TestCase {public function testDummyAction(): void{// 如果缺少 AnImportantClass,则跳过,以及自定义消息。}public function testOtherDummyAction(): void{// 如果缺少 AnImportantClass,则跳过,以及自定义消息。} }
方法级别:
最终类 DummyTest 扩展了 TestCase { #[RequiresClass(AnImportantClass::class, '此测试需要 `AnImportantClass` 类。')]public function testDummyAction(): void{// 如果缺少 AnImportantClass 以及自定义消息,则跳过。}public function testOtherDummyAction(): void{// 不跳过。} }
班级级别:
#[RequiresClass(AnImportantClass::class,outcomeBehavior: OutcomeBehavior::Fail)]final class DummyTest 扩展 TestCase {public function testDummyAction(): void{// 如果缺少 AnImportantClass,则失败。}public function testOtherDummyAction(): void{// 如果缺少 AnImportantClass,则失败。} }
方法级别:
最终类 DummyTest 扩展了 TestCase { #[RequiresClass(AnImportantClass::class, OutcomeBehavior: OutcomeBehavior::Fail)]public function testDummyAction(): void{// 如果 AnImportantClass 缺失则失败。}public function testOtherDummyAction(): void{// 不会失败。} }
班级级别:
#[RequiresClass(AnImportantClass::class)] #[RequiresClass(AnotherVeryImportantClass::class)]最终类 DummyTest 扩展 TestCase {public function testDummyAction(): void{// 如果缺少 AnImportantClass 和/或 AnotherVeryImportantClass,则跳过。}public function testOtherDummyAction(): void{// 如果缺少 AnImportantClass 和/或 AnotherVeryImportantClass,则跳过。} }
方法级别:
最终类 DummyTest 扩展了 TestCase { #[RequiresClass(AnImportantClass::class)] #[RequiresClass(AnotherVeryImportantClass::class)]public function testDummyAction(): void{// 如果 AnImportantClass 和/或 AnotherVeryImportantClass 缺失则跳过。}public function testOtherDummyAction(): void{// 不跳过。} }
#[RequiresPackage]
范围:类和方法级别
此属性可用于定义单个测试以及完整测试类的特定包要求。所需的包预计将通过 Composer 安装。您可以选择定义版本约束和自定义消息。
重要的
该属性根据 Composer 构建的构建时生成的InstalledVersions
类确定已安装的 Composer 包。为了正确读取此类,必须在 PHPUnit 引导脚本中包含 Composer 生成的自动加载器:
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php"><! -- ... --></phpunit>
您还可以将脚本作为命令选项传递: phpunit --bootstrap vendor/autoload.php
默认情况下,不满足要求的测试用例将被跳过。但是,可以使用handleUnsatisfiedPackageRequirements
扩展参数来配置此行为。如果设置为fail
,不满足要求的测试用例将失败(默认为skip
):
<扩展名> <bootstrap class="EliasHaeusslerPHPUnitAttributesPHPUnitAttributesExtension"> <parameter name="handleUnsatisfiedPackageRequirements" value="失败" /> </引导> </扩展名>
最终类 DummyTest 扩展了 TestCase { #[RequiresPackage('symfony/console')]public function testDummyAction(): void{// ...} }
班级级别:
#[RequiresPackage('symfony/console')]最终类 DummyTest 扩展了 TestCase {public function testDummyAction(): void{// 如果未安装 symfony/console,则跳过。}public function testOtherDummyAction(): void{// 如果未安装 symfony/console,则跳过。} }
方法级别:
最终类 DummyTest 扩展了 TestCase { #[RequiresPackage('symfony/console')]public function testDummyAction(): void{// 如果未安装 symfony/console 则跳过。}public function testOtherDummyAction(): void{// 不跳过。} }
班级级别:
#[RequiresPackage('symfony/*')]最终类 DummyTest 扩展了 TestCase {public function testDummyAction(): void{// 如果没有安装 symfony/* 软件包则跳过。}public function testOtherDummyAction(): void{// 如果没有安装 symfony/* 软件包则跳过。} }
方法级别:
最终类 DummyTest 扩展了 TestCase { #[RequiresPackage('symfony/*')]public function testDummyAction(): void{// 如果没有安装 symfony/* 包则跳过。}public function testOtherDummyAction(): void{// 不跳过。} }
班级级别:
#[RequiresPackage('symfony/console', '>= 7')]最终类 DummyTest 扩展 TestCase {public function testDummyAction(): void{// 如果安装的 symfony/console 版本 < 7,则跳过。}public function testOtherDummyAction(): void{// 如果安装的 symfony/console 版本 < 7,则跳过。} }
方法级别:
最终类 DummyTest 扩展了 TestCase { #[RequiresPackage('symfony/console', '>= 7')]public function testDummyAction(): void{// 如果安装的 symfony/console 版本 < 7 则跳过。}public function testOtherDummyAction(): void{//没有跳过。} }
班级级别:
#[RequiresPackage('symfony/console', message: '此测试需要 Symfony 控制台。')]final class DummyTest 扩展 TestCase {public function testDummyAction(): void{// 如果未安装 symfony/console,则跳过,并附带自定义消息。}public function testOtherDummyAction(): void{// 如果未安装 symfony/console,则跳过,并附带自定义消息。 } }
方法级别:
最终类 DummyTest 扩展了 TestCase { #[RequiresPackage('symfony/console', message: '此测试需要 Symfony 控制台。')]public function testDummyAction(): void{// 如果未安装 symfony/console 则跳过,并附带自定义消息。}public function testOtherDummyAction(): void{// 不跳过。} }
班级级别:
#[RequiresPackage('symfony/console',outcomeBehavior: OutcomeBehavior::Fail)]最终类 DummyTest 扩展 TestCase {public function testDummyAction(): void{// 如果未安装 symfony/console,则失败。}public function testOtherDummyAction(): void{// 如果未安装 symfony/console,则失败。} }
方法级别:
最终类 DummyTest 扩展了 TestCase { #[RequiresPackage('symfony/console', OutcomeBehavior: OutcomeBehavior::Fail)]public function testDummyAction(): void{// 如果未安装 symfony/console,则会失败。}public function testOtherDummyAction(): void{// 不会失败。} }
班级级别:
#[RequiresPackage('symfony/console')] #[RequiresPackage('guzzlehttp/guzzle')]最终类 DummyTest 扩展了 TestCase {public function testDummyAction(): void{// 如果未安装 symfony/console 和/或 guzzlehttp/guzzle,则跳过。}public function testOtherDummyAction(): void{// 如果未安装 symfony/console 和/或 guzzlehttp/guzzle,则跳过已安装。} }
方法级别:
最终类 DummyTest 扩展了 TestCase { #[RequiresPackage('symfony/console')] #[RequiresPackage('guzzlehttp/guzzle')]public function testDummyAction(): void{// 如果未安装 symfony/console 和/或 guzzlehttp/guzzle 则跳过。}public function testOtherDummyAction(): void{// 不跳过。 } }
请查看CONTRIBUTING.md
。
该项目已获得 GNU 通用公共许可证 3.0(或更高版本)的许可。