Building the Data Factory Package: Framework-Agnostic Test Data Generation
These articles are AI-generated summaries. Please check the original sources for full details.
Building the Data Factory Package
[2-sentence hook. Name the event, person, or system + one hard fact.]
Francisco Barrento created Data Factory, a framework-agnostic PHP package, to eliminate repetitive test data arrays. Laravel developers use factories like $deployment = Deployment::factory()->make(); now, non-Laravel projects can too.
Why This Matters
The ideal of test data generation is clean, reusable code that adapts to API changes. The reality for framework-agnostic PHP packages is brittle, repetitive array construction. Updating dozens of test arrays when a data structure changes violates DRY principles and increases maintenance overhead, as seen in SDKs like Laravel Ortto.
Key Insights
- “Multiply this by dozens of tests, and you’ve got a maintenance problem.” – Francisco Barrento, 2025
- “States over manual overrides for test data consistency”: DeploymentFactory defines
succeeded()andfailed()states to avoid hardcoded values. - “Temporal used by Stripe, Coinbase” → Not applicable here; Data Factory is used by Laravel Cloud SDK for server test data.
Working Example
// DeploymentFactory.php
use FBarrento\DataFactory\Factory;
class DeploymentFactory extends Factory
{
protected string $dataObject = Deployment::class;
protected function definition(): array
{
return [
'id' => $this->fake->uuid(),
'status' => 'pending',
'branch_name' => 'main',
'commit_hash' => $this->fake->sha1(),
];
}
}
// Deployment.php
use FBarrento\DataFactory\Concerns\HasDataFactory;
readonly class Deployment
{
use HasDataFactory;
public function __construct(
public string $id,
public string $status,
public string $branch_name,
public string $commit_hash
) {}
public static function newFactory(): DeploymentFactory
{
return new DeploymentFactory();
}
}
Practical Applications
- Use Case: Laravel Cloud SDK uses Data Factory to generate server test data with states like
provisioning()andlarge(). - Pitfall: Hardcoding test data values (e.g.,
['status' => 'failed']) instead of using factory states leads to inconsistent test suites.
Reference: https://dev.to/fbarrento/from-laravel-factories-to-framework-agnostic-building-the-data-factory-package-4pib
Continue reading
Next article
Building a Telemetry Pipeline with OpenTelemetry Collector
Related Content
Polyfactory for Production-Grade Mock Data Pipelines
Polyfactory generates rich, realistic mock data directly from Python type hints, supporting dataclasses, Pydantic, attrs, and nested models with a 95% reduction in test data boilerplate.
Automated Future: Scaling Test Results Beyond Ephemeral CI Logs
Steve Pryde launches Automated Future to solve test data loss for teams scaling to 30,000 tests per month.
Automating Email Verification Testing in Playwright: Mailpit vs ZeroDrop
Compare three methods for testing Playwright email flows, ranging from Docker-based SMTP traps like Mailpit to zero-infrastructure SDKs.