inspirum/phpunit-extension

PHPUnit extension with additional assertions

v1.0.0 2024-05-17 12:23 UTC

This package is auto-updated.

Last update: 2024-10-17 13:12:45 UTC


README

Latest Stable Version Build Status Coverage Status Quality Score PHPStan Total Downloads Software License

PHPUnit extension with additional assertions and other helpers.

  • Support some deprecated functionality
  • Easy to implement

Features

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
    }
}

System requirements

Installation

Run composer require command

$ composer require inspirum/phpunit-extension

or add requirement to your composer.json

"inspirum/phpunit-extension": "^1.0"

Usage

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

Testing

To run unit tests, run:

$ composer test:test

To show coverage, run:

$ composer test:coverage

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email tomas.novotny@inspirum.cz instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.