coduo/phpspec-data-provider-extension
PHPSpec extension adding data providers to run the same specification with multiple input/output datasets. Define examples in your specs for concise, parameterized tests and cleaner coverage of edge cases without duplicating scenarios.
Installation Add the package via Composer in your Laravel project:
composer require --dev coduo/phpspec-data-provider-extension
Enable Extension
In your phpspec.yml (or phpspec.yml.dist), add the extension under extensions:
extensions:
Coduo\PhpSpec\DataProviderExtension\DataProviderExtension:
format: 'dsl' # or 'array'
First Use Case Define a data provider in your spec file:
// src/MyClassSpec.php
namespace spec\App;
use PhpSpec\ObjectBehavior;
use Coduo\PhpSpec\DataProviderExtension\DataProvider;
class MyClassSpec extends ObjectBehavior
{
public function it_handles_different_inputs(DataProvider $data)
{
$data->provide([
'string input' => ['input' => 'test'],
'integer input' => ['input' => 123],
])->then(function ($input) {
// Your spec logic here
});
}
}
Data-Driven Specs
Use data providers to avoid repetitive it() blocks for similar test cases:
public function it_processes_various_values(DataProvider $data)
{
$data->provide([
'zero' => [0],
'positive' => [5],
'negative' => [-3],
])->then(function ($value) {
$this->shouldReturnGreaterThanZero($value);
});
}
External Data Sources
Load test data from external files (e.g., data.yml):
# data.yml
users:
- { id: 1, name: 'Alice' }
- { id: 2, name: 'Bob' }
$data->fromYaml('data.yml')->provide('users')->then(function ($user) {
// Test user-related logic
});
Combining with Laravel Use the extension alongside Laravel’s testing helpers:
public function it_validates_requests(DataProvider $data)
{
$data->provide([
'valid' => [['name' => 'Valid']],
'invalid' => [['name' => '']],
])->then(function ($input) {
$response = $this->post('/api/users', $input);
// Assertions...
});
}
Shared Providers Define reusable providers in a base spec class:
abstract class BaseSpec extends ObjectBehavior
{
protected function getTestData()
{
return [
'edge case' => [null],
'normal case' => ['valid'],
];
}
}
Archived Package The package is unmaintained (last release: 2015). Consider alternatives like:
phpspec/prophecy for mocking.array union types or generators.DSL vs. Array Format
$data->provide('name', 'age')->then(function ($name, $age) { ... });
$data->provide([['name' => 'Alice', 'age' => 30]])->then(function ($data) { ... });
Laravel-Specific Quirks
DatabaseMigrations or RefreshDatabase traits, as data providers may conflict with test isolation.beforeEach() to reset state between provider iterations.Debugging
then() arguments matches the provider keys.
// Fails: $data->provide(['a'])->then(function ($a, $b)) { ... }
fromYaml('data.yml') expects the file in the project root).Combine with phpspec/extension
Extend the extension for custom logic:
use Coduo\PhpSpec\DataProviderExtension\DataProviderExtension;
class CustomExtension extends DataProviderExtension
{
public function getConfig()
{
return [
'format' => 'array',
'custom_key' => 'value',
];
}
}
Laravel Artisan Integration Run specs with Laravel’s test environment:
phpspec run --config=phpspec.yml --env=testing
Performance
For large datasets, use ->skip() to exclude specific cases:
$data->provide([...])->skip('edge case')->then(...);
Legacy Code If stuck with this package, wrap providers in a service class to abstract away quirks:
class SpecDataProvider
{
public function provide(array $data): DataProvider
{
return new DataProvider($data);
}
}
How can I help you explore Laravel packages today?