digitalrevolution / phpunit-extensions
A library for phpunit utility and support classes
Installs: 60 040
Dependents: 3
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 1
Open Issues: 2
Requires
- php: ^8.1
- phpunit/phpunit: ^10.0 || ^11.0 || ^12.0
Requires (Dev)
- digitalrevolution/phpunit-file-coverage-inspection: ^3.0
- mikey179/vfsstream: ^1.6.12
- phpmd/phpmd: ^2.12
- phpstan/extension-installer: ^1.2
- phpstan/phpstan: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpstan/phpstan-symfony: ^2.0
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^3.7
- symfony/clock: ^6.4||^7.0
- 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
- tecnickcom/tcpdf: ^6.0
Suggests
- ext-imagick: Imagick is required for IsSameImageConstraint
- symfony/clock: Symfony clock component is required when using the ClockTestTrait
- 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
- tecnickcom/tcpdf: TCPDF is required for PdfTestTrait
Conflicts
- symfony/form: <6.0
- symfony/framework-bundle: <6.0
- symfony/security-core: <6.0
- symfony/twig-bundle: <6.0
README
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
ResponseAssertions trait
The ResponseAssertions trait provides a set of assertion methods designed to streamline the testing of Symfony HTTP responses. This trait includes methods for verifying the status code, response message content and specific types of responses such as JSON responses
Methods
assertJsonResponse
assertResponse
assertResponseIsSuccessful
assertResponseIsRedirection
assertResponseIsClientError
assertResponseIsServerError
ClockTestTrait
The ClockTestTrait provides a set of methods to manipulate the current time in tests. This trait will automatically freeze the time at the start of
each test. The trait also provides methods to get the current time as timestamp or DateTimeImmutable
object,
Methods
self::time(): int
self::now(): DateTimeImmutable
self::sleep(int|float $seconds): void
self::assertNow()
self::assertSameTime()
ImageTestTrait
The ImageTestTrait
provides methods to compare two images by binary string
, SplFileInfo
or resource
. (Requires Imagick
). Internally
Imagick::compareImages()
is used to compare the images and therefore supports various image formats such as PNG, JPEG, GIF, PDF and more.
Methods
self::assertSameImage(string|SplFileInfo|resource, string|SplFileInfo|resource)
self::assertNotSameImage(string|SplFileInfo|resource, string|SplFileInfo|resource)
PdfTestTrait
The PdfTestTrait
provides methods to compare a TCPDF
object against an image of binary string
, SplFileInfo
, resource
or another TCPDF
file. (Requires Imagick
with ghostscript
and TCPDF
)
Methods
self::assertSamePdf(string|SplFileInfo|resource|TCPDF, TCPDF)
self::assertNotSamePdf(string|SplFileInfo|resource|TCPDF, TCPDF)
.
For both ImageTestTrait and PdfTestTrait:
The environment variable PHPUNIT_EXTENSIONS_IMAGE_DIFF_OUTPUT_PATH
can be set to a directory where a diff.html
will be generated which will
show the differences. Optionally, the environment variable PHPUNIT_EXTENSIONS_IMAGE_DIFF_OUTPUT_URL
can be set to inform the user where
to find the outputted diff.html
file. This is useful when running tests in a CI environment.
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.