PHPUnit extension with additional assertions and other helpers.
- Support some deprecated functionality
- Easy to implement
Supports deprecated assertion method withConsecutive
Before PHPUnit 10.0
use PHPUnit\Framework\TestCase;
final class CalculatorTest extends TestCase
{
public function testMultiply(): void
{
$mock = $this->createMock(Calculator::class);
$arguments = [[1,2,3], [4,5,6]]
$responses = [6, 120]
$mock->expects(self::exactly(count($arguments)))->method('multiply')
->withConsecutive(...$arguments)
->willReturnOnConsecutiveCalls(...$responses);
// ... test
}
}
After PHPUnit 10.0
use Inspirum\PHPUnit\Extension;
use PHPUnit\Framework\TestCase;
final class CalculatorTest extends TestCase
{
use Extension;
public function testMultiply(): void
{
$mock = $this->createMock(Calculator::class);
$arguments = [[1,2,3], [4,5,6]]
$responses = [6, 120]
$mock->expects(self::exactly(count($arguments)))->method('multiply')
->will(self::withConsecutive($arguments, $responses));
// ... test
}
}
Run composer require command
$ composer require inspirum/phpunit-extension
or add requirement to your composer.json
"inspirum/phpunit-extension": "^1.0"
Validate arguments and responses:
$mock->expects(self::exactly(2))->method('example')
->will(self::withConsecutive(
arguments: [
[1, 2, 0.1],
[2, 3, 0.01],
],
responses: [
true,
false,
],
));
self::assertTrue($mock->example(1, 2, 0.1));
self::assertFalse($mock->example(2, 3, 0.01));
Optional responses:
$mock->expects(self::exactly(2))->method('example')
->will(self::withConsecutive(
arguments: [
[1, 2, 0.1],
[2, 3, 0.01],
],
));
$mock->example(1, 2, 0.1);
$mock->example(2, 3, 0.01);
Simplification for same response for each call
$mock->expects(self::exactly(2))->method('example')
->will(self::withConsecutive(
arguments: [
[1, 2, 0.1],
[2, 3, 0.01],
],
responses: true,
));
self::assertTrue($mock->example(1, 2, 0.1));
self::assertTrue($mock->example(2, 3, 0.01));
Supports throwing exceptions:
$mock->expects(self::exactly(2))->method('example')
->will(self::withConsecutive(
arguments: [
[1, 2, 0.1],
[2, 3, 0.01],
],
responses: [
true,
new RuntimeException('Custom error'),
],
));
self::assertTrue($mock->example(1, 2, 0.1));
try {
$mock->example(2, 3, 0.01);
} catch (RuntimeException $exception) {
self::assertSame('Custom error', $exception->getMessage());
}
To run unit tests, run:
$ composer test:test
To show coverage, run:
$ composer test:coverage
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.