digitalrevolution / phpunit-extensions
PHPUnit 实用工具和辅助类库
v1.5.1
2024-08-22 15:22 UTC
Requires
- php: ^8.1
- phpunit/phpunit: ^9.5.24 || ^10.0 || ^11.0
Requires (Dev)
- digitalrevolution/phpunit-file-coverage-inspection: ^2.0
- phpmd/phpmd: ^2.12
- phpstan/extension-installer: ^1.2
- phpstan/phpstan: ^1.9.1
- phpstan/phpstan-phpunit: ^1.2.2
- phpstan/phpstan-strict-rules: ^1.5
- phpstan/phpstan-symfony: ^1.2.16
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^3.7
- symfony/form: ^6.0||^7.0
- symfony/framework-bundle: ^6.0||^7.0
- symfony/security-core: ^6.0||^7.0
- symfony/twig-bundle: ^6.0||^7.0
- symfony/validator: ^6.0||^7.0
Suggests
- symfony/form: Symfony form component is required for testing the controller createForm methods
- symfony/framework-bundle: Symfony framework bundle is required for the AbstractControllerTestCase
- symfony/security-core: Symfony security component is required for testing authentication/authorization related methods
- symfony/twig-bundle: Symfony twig bundle is required to test rendering templates/forms
Conflicts
- symfony/form: <6.0
- symfony/framework-bundle: <6.0
- symfony/security-core: <6.0
- symfony/twig-bundle: <6.0
README
PHPUnit 扩展
实用类,使单元测试更加容易。
特性
withConsecutive
在 PHPUnit 10 中移除了 withConsecutive 方法。为了仍然能够迁移现有的代码库,提供了一种替换方法
PHPUnit <= 9.5
$mock->method('myMethod')->withConsecutive([123, 'foobar'], [456]);
PHPUnit >= 9.6
$mock->method('myMethod')->with(...consecutive([123, 'foobar'], [456]));
Symfony 控制器测试
内部测试 Symfony 控制器会调用依赖容器。一个实用类,可以更轻松地模拟这些类。
use DR\PHPUnitExtensions\Symfony\AbstractControllerTestCase; class MyControllerTest extends AbstractControllerTestCase { public function myTest(): void { $this->expectDenyAccessUnlessGranted('attribute', null, true); $this->expectGetUser(new User()); $this->expectCreateForm(TextType::class); ($this->controller)(); } public function getController() { return new MyController(); } }
方法
expectGetUser
expectDenyAccessUnlessGranted
expectCreateForm
expectAddFlash
expectGenerateUrl
expectRedirectToRoute
expectForward
expectRender
Symfony 约束验证器测试
用于测试 Symfony 约束验证器的测试用例。
use DR\PHPUnitExtensions\Symfony\AbstractConstraintValidatorTestCase; class MyConstraintValidatorTest extends AbstractConstraintValidatorTestCase { public function testValidate(): void { $this->expectBuildViolation($constraint->message, ['parameter' => 123]) ->expectSetCode(789) ->expectAtPath('path') ->expectAddViolation(); $this->validator->validate(123, $this->constraint); } protected function getValidator(): ConstraintValidator { return new MyConstraintValidator(); } protected function getConstraint(): Constraint { return new MyConstraint(); } }
方法
assertHandlesIncorrectConstraintType
expectNoViolations
expectBuildViolation(): ConstraintViolationBuilderAssertion
ConstraintViolationBuilderAssertion
expectSetInvalidValue
expectSetPlural
expectSetCode
expectSetCause
expectSetTranslationDomain
expectSetParameters
expectSetParameter
expectSetParameterWithConsecutive
expectAtPath
expectAddViolation
ResponseAssertions 特性
ResponseAssertions 特性提供了一组断言方法,旨在简化对 Symfony HTTP 响应的测试。这个特性包括用于验证状态码、响应消息内容和特定类型的响应(如 JSON 响应)的方法
方法
assertJsonResponse
assertResponse
assertResponseIsSuccessful
assertResponseIsRedirection
assertResponseIsClientError
assertResponseIsServerError
关于我们
在 123inkt(Digital Revolution B.V. 的组成部分),每天都有超过 50 名开发专业人士在改进我们的内部 ERP 和我们的几家商店。你想加入我们吗? 我们在寻找开发者。