digitalrevolution/phpunit-extensions

PHPUnit 实用工具和辅助类库

v1.5.1 2024-08-22 15:22 UTC

This package is auto-updated.

Last update: 2024-09-22 15:31:23 UTC


README

Minimum PHP Version Run tests

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 和我们的几家商店。你想加入我们吗? 我们在寻找开发者