ergebnis/data-provider
Generate data providers for PHPUnit from arrays, iterables, or generators with a small, focused API. Helps build readable, reusable test datasets and named cases, reducing boilerplate when writing parameterized tests in PHP.
This project provides a composer package with generic data providers for use with phpunit/phpunit.
Run
composer require ergebnis/data-provider
This package provides the following generic data providers:
Ergebnis\DataProvider\BoolProviderErgebnis\DataProvider\FloatProviderErgebnis\DataProvider\IntProviderErgebnis\DataProvider\NullProviderErgebnis\DataProvider\ObjectProviderErgebnis\DataProvider\ResourceProviderErgebnis\DataProvider\StringProviderErgebnis\DataProvider\UuidProviderSince it is possible to use multiple @dataProvider annotations or PHPUnit\Framework\Attribute\DataProviderExternal attributes for test methods, these generic data providers allow for reuse and composition of data providers:
<?php
declare(strict_types=1);
namespace Example\Test;
use Ergebnis\DataProvider;
use PHPUnit\Framework;
final class ExampleTest extends Framework\TestCase
{
/**
* @dataProvider \Ergebnis\DataProvider\StringProvider::blank()
* @dataProvider \Ergebnis\DataProvider\StringProvider::empty()
*/
public function testFromNameRejectsInvalidValueWithAnnotation(string $value): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Value can not be an empty or blank string.');
UserName::fromString($value);
}
#[Framework\DataProviderExternal(DataProvider\StringProvider::class, 'blank')]
#[Framework\DataProviderExternal(DataProvider\StringProvider::class, 'empty')]
public function testFromNameRejectsInvalidValueWithAttribute(string $value): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Value can not be an empty or blank string.');
UserName::fromString($value);
}
}
DataProvider\BoolProviderarbitrary() provides true, falsefalse() provides falsetrue() provides trueFor examples, see Ergebnis\DataProvider\Test\Unit\BoolProviderTest.
DataProvider\FloatProviderarbitrary() provides arbitrary floatsgreaterThanOne() provides ints greater than 1.0greaterThanZero() provides ints greater than 0.0lessThanOne() provides ints less than 1.0lessThanZero() provides ints less than 0.0one() provides 1.0zero() provides 0.0For examples, see Ergebnis\DataProvider\Test\Unit\FloatProviderTest.
DataProvider\IntProviderarbitrary() provides arbitrary intsgreaterThanOne() provides ints greater than 1greaterThanZero() provides ints greater than 0lessThanOne() provides ints less than 1lessThanZero() provides ints less than 0one() provides 1zero() provides 0For examples, see Ergebnis\DataProvider\Test\Unit\IntProviderTest.
DataProvider\NullProvidernull() provides nullFor examples, see Ergebnis\DataProvider\Test\Unit\NullProviderTest.
DataProvider\ObjectProviderobject() provides an instance of stdClassFor examples, see Ergebnis\DataProvider\Test\Unit\ObjectProviderTest.
DataProvider\ResourceProviderresource() provides a resourceFor examples, see Ergebnis\DataProvider\Test\Unit\ResourceProviderTest.
DataProvider\StringProviderarbitrary() provides arbitrary stringsblank() provides strings consisting of whitespace characters onlyempty() provides an empty stringtrimmed() provides non-empty, non-blank strings without leading and trailing whitespaceuntrimmed() provides non-empty, non-blank strings with additional leading and trailing whitespacewithWhitespace() provides non-empty, non-blank, trimmed strings containing whitespaceFor examples, see Ergebnis\DataProvider\Test\Unit\StringProviderTest.
DataProvider\UuidProviderarbitrary() provides lower- and upper-case UUIDscaseLower() provides a lower-case UUIDscaseUpper() provides an upper-case UUIDsFor examples, see Ergebnis\DataProvider\Test\Unit\UuidProviderTest.
The maintainers of this project record notable changes to this project in a changelog.
The maintainers of this project suggest following the contribution guide.
The maintainers of this project ask contributors to follow the code of conduct.
The maintainers of this project provide limited support.
You can support the maintenance of this project by sponsoring @ergebnis.
This project supports PHP versions with active and security support.
The maintainers of this project add support for a PHP version following its initial release and drop support for a PHP version when it has reached the end of security support.
This project has a security policy.
This project uses the MIT license.
Follow @localheinz and @ergebnis on Twitter.
How can I help you explore Laravel packages today?