https://github.com/123inkt/phpunit-extensions
A library with convenient test case classes and functions for phpunit
https://github.com/123inkt/phpunit-extensions
mock phpunit test testcase
Last synced: 4 months ago
JSON representation
A library with convenient test case classes and functions for phpunit
- Host: GitHub
- URL: https://github.com/123inkt/phpunit-extensions
- Owner: 123inkt
- License: mit
- Created: 2023-06-23T08:27:28.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-11-11T09:22:45.000Z (6 months ago)
- Last Synced: 2024-12-17T08:55:17.915Z (5 months ago)
- Topics: mock, phpunit, test, testcase
- Language: PHP
- Homepage:
- Size: 43.9 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
[](https://php.net/)
## 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:
```php
$mock->method('myMethod')->withConsecutive([123, 'foobar'], [456]);
```
PHPUnit >= 9.6:
```php
$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.```php
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.
```php
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`## 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](https://www.werkenbij123inkt.nl/zoek-op-afdeling/it).