rawr/phpunit-data-provider
PHPUnit data provider helper for PHP tests. Simplifies building and organizing datasets, supports readable, reusable providers, and makes parameterized tests easier to maintain. Works with PHPUnit to generate cases cleanly and consistently.
Install the package as a dev dependency:
composer require --dev rawr/phpunit-data-provider
Then import the entry point:
use TRegx\PhpUnit\DataProviders\DataProvider;
Your first use case is eliminating duplicated data definitions. Instead of copying ['user' => 'alice', 'role' => 'admin'] across multiple @dataProvider methods, define a reusable base and combine it:
public function userPermissions(): iterable
{
return DataProvider::cross(
DataProvider::list(['alice', 'bob']),
DataProvider::list(['read', 'write', 'delete'])
);
}
Always start with DataProvider::list(), ::of(), or ::entries()—then chain combinators like ::cross(), ::join(), or ::zip().
$testData = DataProvider::list([1, 2, 3])
->filter('is_int')
->map(fn($x) => $x * 2)
->cross(DataProvider::list(['sync', 'async']));
::entries() when keys are meaningful (e.g., status codes or enums):
public function statusEndpoints(): iterable
{
return DataProvider::entries([
'draft' => ['status' => 0, 'label' => 'Draft'],
'active' => ['status' => 1, 'label' => 'Active'],
'archived' => ['status' => 2, 'label' => 'Archived'],
]);
}
// Test receives: $key = 'draft', $value = ['status' => 0, 'label' => 'Draft']
return DataProvider::cross(
DataProvider::list(['Chrome', 'Firefox']),
DataProvider::list(['macOS', 'Linux']),
DataProvider::list(['v1', 'v2'])
);
public static function authenticatedUserProvider(): DataProvider
{
return DataProvider::list(['alice', 'bob'])
->map(fn($name) => new User($name, ['role' => 'admin']));
}
::of() for transformation:
return DataProvider::of($this->loadUsersFromFixture())
->filter(fn($user) => $user->isActive())
->zip(DataProvider::list(['success', 'warning']));
\TRegx\PhpUnit\DataProviders\, not \TRegx\DataProvider\. Update imports and avoid mixing old and new namespaces—silent failures may occur.4 (int) and "4" (string) are always treated as distinct rows. This prevents edge-case bugs but may break assumptions in tests expecting loose comparison.filter()->map() instead of map()->filter().->debug() method mid-chain to dump intermediate results:
return DataProvider::list([1, 2, 3])->filter('is_int')->debug()->map(...);
iterable|DataProvider, not array. Returning array in strict mode may cause TypeError or silent truncation.::flatMap(), ::reindex(), ::reindexTo()—these enable advanced reshaping (e.g., flattening nested arrays for parameterized endpoint tests).::of() over in-memory arrays to avoid exhaustion.How can I help you explore Laravel packages today?