digitalrevolution/phpunit-extensions

A library for phpunit utility and support classes

v1.4.0 2024-02-10 09:31 UTC

This package is auto-updated.

Last update: 2024-04-22 09:46:15 UTC


README

Minimum PHP Version Run tests

PHPUnit extensions

Utility classes to make unit testing life easier.

Features

withConsecutive

In PHPUnit 10 withConsecutive method was removed. To still be able to migrate existing codebases a replacement method:

PHPUnit <= 9.5:

$mock->method('myMethod')->withConsecutive([123, 'foobar'], [456]);

PHPUnit >= 9.6:

$mock->method('myMethod')->with(...consecutive([123, 'foobar'], [456]));

Symfony controller tests

Testing a Symfony controller internally invokes the dependency container. A utility class to mock these classes more easily.

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();    
    }    
}

Methods

  • expectGetUser
  • expectDenyAccessUnlessGranted
  • expectCreateForm
  • expectAddFlash
  • expectGenerateUrl
  • expectRedirectToRoute
  • expectForward
  • expectRender

Symfony ConstraintValidator tests

TestCase for testing Symfony ConstraintValidators.

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();
    }
}

Methods

  • assertHandlesIncorrectConstraintType
  • expectNoViolations
  • expectBuildViolation(): ConstraintViolationBuilderAssertion

ConstraintViolationBuilderAssertion

  • expectSetInvalidValue
  • expectSetPlural
  • expectSetCode
  • expectSetCause
  • expectSetTranslationDomain
  • expectSetParameters
  • expectSetParameter
  • expectSetParameterWithConsecutive
  • expectAtPath
  • expectAddViolation

About us

At 123inkt (Part of Digital Revolution B.V.), every day more than 50 development professionals are working on improving our internal ERP and our several shops. Do you want to join us? We are looking for developers.