| name | acc-create-test-builder |
| description | Generates Test Data Builder and Object Mother patterns for PHP 8.5. Creates fluent builders with sensible defaults and factory methods for test data creation. |
Test Data Builder Generator
Generates Test Data Builder and Object Mother patterns for test data creation.
Patterns
Test Data Builder
Fluent interface for constructing test objects with customizable properties.
When to use:
- Complex objects with many properties
- Need to customize specific properties per test
- Want to express test intent clearly
Object Mother
Factory methods returning pre-configured objects for common scenarios.
When to use:
- Standard test fixtures (default user, pending order)
- Shared across many tests
- Named scenarios (premium customer, expired subscription)
Builder Template
<?php
declare(strict_types=1);
namespace Tests\Builder;
use {FullyQualifiedClassName};
final class {ClassName}Builder
{
private {IdType} $id;
private {Property1Type} ${property1};
private {Property2Type} ${property2};
private function __construct()
{
$this->id = {IdType}::generate();
$this->{property1} = {default1};
$this->{property2} = {default2};
}
public static function a{ClassName}(): self
{
return new self();
}
public static function an{ClassName}(): self
{
return new self();
}
public function withId({IdType} $id): self
{
$clone = clone $this;
$clone->id = $id;
return $clone;
}
public function with{Property1}({Property1Type} ${property1}): self
{
$clone = clone $this;
$clone->{property1} = ${property1};
return $clone;
}
public function with{Property2}({Property2Type} ${property2}): self
{
$clone = clone $this;
$clone->{property2} = ${property2};
return $clone;
}
public function build(): {ClassName}
{
return new {ClassName}(
$this->id,
$this->{property1},
$this->{property2}
);
}
}
Object Mother Template
<?php
declare(strict_types=1);
namespace Tests\Mother;
use {FullyQualifiedClassName};
final class {ClassName}Mother
{
public static function default(): {ClassName}
{
return {ClassName}Builder::a{ClassName}()->build();
}
public static function {scenario1}(): {ClassName}
{
return {ClassName}Builder::a{ClassName}()
->with{Property}({value})
->build();
}
public static function {scenario2}(): {ClassName}
{
return {ClassName}Builder::a{ClassName}()
->with{Property1}({value1})
->with{Property2}({value2})
->build();
}
}
Complete Example: Order
OrderBuilder
<?php
declare(strict_types=1);
namespace Tests\Builder;
use App\Domain\Order\Order;
use App\Domain\Order\OrderId;
use App\Domain\Order\OrderItem;
use App\Domain\Order\OrderStatus;
use App\Domain\Customer\CustomerId;
use App\Domain\Shared\Money;
use DateTimeImmutable;
final class OrderBuilder
{
private OrderId $id;
private CustomerId $customerId;
private array $items = [];
private OrderStatus $status;
private DateTimeImmutable $createdAt;
private {
->id = ::();
->customerId = ::();
->status = ::;
->createdAt = ();
}
{
();
}
{
= ;
->id = ;
;
}
{
= ;
->customerId = ;
;
}
{
= ;
->items[] = (, );
;
}
{
= ;
->items = ;
;
}
{
= ;
->items = [
(::(), ),
];
;
}
{
= ;
->status = ::;
;
}
{
= ;
->status = ::;
((->items)) {
->items[] = (::(), );
}
;
}
{
= ;
->status = ::;
((->items)) {
->items[] = (::(), );
}
;
}
{
= ;
->status = ::;
;
}
{
= ;
->createdAt = ;
;
}
{
->(
()
);
}
{
= (->id, ->customerId, ->createdAt);
(->items ) {
->(->(), ->());
}
(->status === ::) {
->();
} (->status === ::) {
->();
->();
} (->status === ::) {
->();
}
;
}
}
OrderMother
<?php
declare(strict_types=1);
namespace Tests\Mother;
use App\Domain\Order\Order;
use App\Domain\Customer\CustomerId;
use App\Domain\Shared\Money;
use Tests\Builder\OrderBuilder;
final class OrderMother
{
public static function pending(): Order
{
return OrderBuilder::anOrder()->pending()->build();
}
public static function confirmed(): Order
{
return OrderBuilder::anOrder()->confirmed()->build();
}
public static ():
{
::()->()->();
}
{
::()->()->();
}
{
::()
->()
->();
}
{
::()
->()
->();
}
{
::()->();
}
{
::()
->()
->();
}
{
::()
->()
->()
->();
}
}
Value Object Builders
<?php
declare(strict_types=1);
namespace Tests\Builder;
use App\Domain\Shared\Money;
final class MoneyBuilder
{
private int $amount;
private string $currency;
private function __construct()
{
$this->amount = 1000;
$this->currency = 'EUR';
}
public static function money(): self
{
return new self();
}
public function withAmount(int $amount): self
{
$clone = clone $this;
$clone->amount = $amount;
return $clone;
}
public {
= ;
->currency = ;
;
}
{
= ;
->currency = ;
;
}
{
->();
}
{
(->amount, ->currency);
}
}
{
{
::();
}
{
::();
}
{
::();
}
{
::();
}
}
Usage in Tests
$order = OrderBuilder::anOrder()
->forCustomer($customerId)
->withItem($book, 2)
->withItem($pen, 5)
->confirmed()
->createdDaysAgo(7)
->build();
$pendingOrder = OrderMother::pending();
$confirmedOrder = OrderMother::confirmed();
$expiredOrder = OrderMother::expired();
$customerOrder = OrderMother::forCustomer($customerId);
Generation Instructions
-
Analyze the target class:
- Constructor parameters
- Required vs optional properties
- Value objects used
- State transitions (for entities)
-
Determine sensible defaults:
- Generate IDs automatically
- Use common/valid values
- Consider relationships
-
Create Builder with:
- Private constructor with defaults
- Static factory method (
aOrder, anEmail)
with* methods for each property
- Immutable (clone in each method)
build() method
-
Create Mother with:
default() method
- Named scenarios (
pending, confirmed, premium)
- Parameterized methods (
forCustomer, withTotal)
-
File placement:
- Builders:
tests/Builder/{ClassName}Builder.php
- Mothers:
tests/Mother/{ClassName}Mother.php
Best Practices
- Sensible defaults — Tests should work without customization
- Fluent interface — Chain method calls
- Immutable builders — Clone in each
with* method
- Expressive names —
pending() not withStatus(pending)
- Composition — Builders can use other Mothers/Builders
- Single Responsibility — One builder per aggregate/entity