Provides a helper trait and generic data providers for tests.

Fund package maintenance!


Integrate Prune Release Renew

Code Coverage Type Coverage

Latest Stable Version Total Downloads

Provides a helper trait and generic data providers for tests.



$ composer require --dev ergebnis/test-util



Import the Ergebnis\Test\Util\Helper trait into your test class:



namespace Foo\Bar\Test\Unit;

use Ergebnis\Test\Util;
use PHPUnit\Framework;

final class BazTest extends Framework\TestCase
    use Util\Helper;

Easy access to localized instances of Faker\Generator

The Helper trait provides a method to fetch a localized instance of Faker\Generator:

  • faker(string $locale = 'en_US') : \Faker\Generator


namespace Example\Test\Unit;

use Ergebnis\Test\Util;
use Example\Player;
use PHPUnit\Framework;

final class PlayerTest extends Framework\TestCase
    use Util\Helper;

    public function testConstructorSetsValues(): void
        $name = self::faker()->firstName;

        $player = new Player($name);

        $this->assertSame($name, $player->firstName());

For reference, see fzaninotto/faker.

Additional Assertions

In addition to the assertions made available by extending from PHPUnit\Framework\TestCase, the Helper trait provides the following assertions:

  • assertClassesAreAbstractOrFinal(string $directory, array $excludeClassNames = [])
  • assertClassesHaveTests(string $directory, string $namespace, string $testNamespace, array $excludeClassyNames = [])
  • assertClassExists(string $className)
  • assertClassExtends(string $parentClassName, string $className)
  • assertClassImplementsInterface(string $interfaceName, string $className)
  • assertClassIsAbstract(string $className)
  • assertClassIsFinal(string $className)
  • assertClassSatisfiesSpecification(callable $specification, string $className, string $message = '')
  • assertClassUsesTrait(string $traitName, string $className)
  • assertClassyConstructsSatisfySpecification(callable $specification, string $directory, array $excludeClassNames = [], $message = '')
  • assertInterfaceExists(string $interfaceName)
  • assertInterfaceExtends(string $parentInterfaceName, string $interfaceName)
  • assertInterfaceSatisfiesSpecification(callable $specification, string $interfaceName, string $message = '')
  • assertTraitExists(string $traitName)
  • assertTraitSatisfiesSpecification(callable $specification, string $traitName, string $message = '')

Data Providers

This package provides the following generic data providers:

Since it is possible to use multiple @dataProvider annotations for test methods, these generic data providers allow for reuse and composition of data providers:



namespace Example\Test;

use PHPUnit\Framework;

final class ExampleTest extends Framework\TestCase
     * @dataProvider \Ergebnis\Test\Util\DataProvider\StringProvider::blank()
     * @dataProvider \Ergebnis\Test\Util\DataProvider\StringProvider::empty()
     * @param string $value
    public function testFromNameRejectsBlankOrEmptyStrings(string $value): void
        $this->expectExceptionMessage('Value can not be an empty or blank string.');



  • arbitrary() provides true, false
  • false() provides false
  • true() provides true

For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\BoolProviderTest.


  • arbitrary() provides arbitrary floats
  • greaterThanOne() provides ints greater than 1.0
  • greaterThanZero() provides ints greater than 0.0
  • lessThanOne() provides ints less than 1.0
  • lessThanZero() provides ints less than 0.0
  • one() provides 1.0
  • zero() provides 0.0

For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\FloatProviderTest.


  • arbitrary() provides arbitrary ints
  • greaterThanOne() provides ints greater than 1
  • greaterThanZero() provides ints greater than 0
  • lessThanOne() provides ints less than 1
  • lessThanZero() provides ints less than 0
  • one() provides 1
  • zero() provides 0

For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\IntProviderTest.


  • null() provides null

For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\NullProviderTest.


  • object() provides an instance of stdClass

For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\ObjectProviderTest.


  • resource() provides a resource

For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\ResourceProviderTest.


  • arbitrary() provides arbitrary strings
  • blank() provides strings consisting of whitespace characters only
  • empty() provides an empty string
  • trimmed() provides non-empty, non-blank strings without leading and trailing whitespace
  • untrimmed() provides non-empty, non-blank strings with additional leading and trailing whitespace
  • withWhitespace() provides non-empty, non-blank, trimmed strings containing whitespace

For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\StringProviderTest.


Please have a look at


Please have a look at

Code of Conduct

Please have a look at


This package is licensed using the MIT License.

Please have a look at


The SrcCodeTest in this and other projects I maintain or contribute to is inspired by ProjectCodeTest in friends-of-php/php-cs-fixer, and was initially created by Dariusz Rumiński.

Curious what I am building?

📬 Subscribe to my list, and I will occasionally send you an email to let you know what I am working on.