数字革命 / 配件对约束
Digital Revolution PHPUnit Constraint for accessorpairs
v2.4.0
2024-09-16 18:33 UTC
Requires
- php: >=8.1
- doctrine/inflector: ^2.0
- phpdocumentor/type-resolver: ^1.7
- phpunit/phpunit: ^10.0 || ^11.0
Requires (Dev)
- phpmd/phpmd: @stable
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-strict-rules: ^1.0
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^3.6
This package is auto-updated.
Last update: 2024-09-18 13:53:07 UTC
README
配件对约束
自动单元测试(并覆盖)您数据类中所有getter和setter的一种方式。
安装
$ composer require --dev digitalrevolution/accessorpair-constraint
用法
一旦将 AccessorPairAsserter 特性导入您的测试类或 TestCase 基类,您就可以调用 assertAccessorPairs
方法来自动测试所有getter/setter。
如果您想跟踪覆盖率,请配置PHPUnit注释以覆盖您类的所有方法。
可选地,断言器还可以检查所有类的属性初始值以及是否在调用setter之前调用getter。
示例
<?php use DigitalRevolution\AccessorPairConstraint\AccessorPairAsserter; use PHPUnit\Framework\TestCase; /** * @coversDefaultClass \DataClass * @covers ::<public> */ class DataClassTest extends TestCase { use AccessorPairAsserter; public function testDataClass() { static::assertAccessorPairs(DataClass::class); } }
示例:简单 DataClass
在此示例中,数据类由getter和setter方法以及设置属性的构造函数组成。配件对约束可以匹配setter方法与getter方法,并将为每个对执行测试。约束还可以匹配构造函数参数与getter方法,并将对这些对进行测试。
<?php class DataClass { private $property; private $default; public function __construct(string $property, bool $default) { $this->property = $property; $this->default = $default; } public function getProperty(): string { return $this->property; } public function setProperty(string $param): self { $this->property = $param; return $this; } public function isDefault(): bool { return $this->default; } public function setDefault(bool $param): self { $this->default = $param; return $this; } }
示例:配置约束
在此示例中,构造函数参数 $property 将与 getProperty 方法匹配,与 setProperty 方法的 getProperty 匹配。由于构造函数改变了数据,配件对约束无法断言类的正确性。仍然可以使用约束配置测试 setProperty-getProperty 方法对。
数据类
<?php class DataClass { private $property; public function __construct(string $property) { $this->property = strtoupper($property); } public function setProperty(string $property) { $this->property = $property; } public function getProperty(): string { return $this->property; } }
单元测试
<?php use DigitalRevolution\AccessorPairConstraint\AccessorPairAsserter; use DigitalRevolution\AccessorPairConstraint\Constraint\ConstraintConfig; use PHPUnit\Framework\TestCase; /** * @coversDefaultClass \DataClass * @covers ::<public> */ class DataClassTest extends TestCase { use AccessorPairAsserter; public function testDataClass() { static::assertAccessorPairs(DataClass::class, (new ConstraintConfig())->setAssertConstructor(false)); } }
可能的配置选项
<?php class ConstraintConfig { /** * Enabled by default. * Let the constraint pair all getter and setter methods, * and pass test data to the setter to assert that the getter returns the exact same value. */ public function setAssertAccessorPair(bool $assertAccessorPair); /** * Enabled by default. * Let the constraint pair the constructor's parameters with the class' getter methods. * These pairs will be tested in the same ways as the getter/setter method pairs. */ public function setAssertConstructor(bool $assertConstructor); /** * Disabled by default. * When enabled, the getter methods are called on an empty instance of the test object. * This makes sure that all the properties have the correct default type, * conforming the getter return typehint. */ public function setAssertPropertyDefaults(bool $assertPropertyDefaults); /** * Enabled by default. * When disabled, only the direct class methods will be asserted and none of the parent's * class methods. */ public function setAssertParentMethods(bool $assertParentMethods); /** * A list of exact method names that should be excluded from the assertions. */ public function setExcludedMethods(array $excludedMethods); /** * Callback function to create the constructor arguments for the class under test. * * Test data or mocks will be used by default. * * @param callable(): mixed[] $callback * @return $this */ public function setConstructorCallback(callable $callback): self; }
关于我们
在 123inkt(Digital Revolution B.V. 的部分),每天超过 50 名开发专业人士正在改进我们的内部 ERP 和我们的几家商店。你想加入我们吗? 我们正在寻找开发者。