PHPackages                             sytzez/data-object-tester - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. sytzez/data-object-tester

ActiveLibrary[Testing &amp; Quality](/categories/testing)

sytzez/data-object-tester
=========================

1.0.0(4y ago)03MITPHPPHP &gt;=8.0

Since Oct 26Pushed 4y ago1 watchersCompare

[ Source](https://github.com/sytzez/data-object-tester)[ Packagist](https://packagist.org/packages/sytzez/data-object-tester)[ RSS](/packages/sytzez-data-object-tester/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (4)Versions (2)Used By (0)

[![PHPUnit](https://github.com/sytzez/data-object-tester/actions/workflows/phpunit.yml/badge.svg)](https://github.com/sytzez/data-object-tester/actions/workflows/phpunit.yml/badge.svg)[![PHPStan](https://github.com/sytzez/data-object-tester/actions/workflows/phpstan.yml/badge.svg)](https://github.com/sytzez/data-object-tester/actions/workflows/phpstan.yml/badge.svg)

Data Object Tester
==================

[](#data-object-tester)

Should you even unit test your data object classes? With DataObjectTester it's so effortless that you don't even need to ask yourself that question! It automates doing automated tests in PHPUnit for immutable data objects. Within a minute, you'll have written a test that covers all possible cases. Check out the code below to see how it works.

### Requirements

[](#requirements)

- PHP 8.0 or above
- PHPUnit

Installation
------------

[](#installation)

```
composer require sytzez/data-object-tester --dev
```

Usage
-----

[](#usage)

Consider the following data class:

```
final class DataClass
{
    public function __construct(
        private string $string,
        private int $int,
        private array $array,
    ) {
    }

    public function getString(): string
    {
        return $this->string;
    }

    public function getInt(): int
    {
        return $this->int;
    }

    public function getArray(): array
    {
        return $this->array;
    }
}
```

To make sure the getter methods always return the given values, all you need to do is write a test that extends the DataObjectTestCase:

```
use Sytzez\DataObjectTester\DataObjects\ClassExpectation
use Sytzez\DataObjectTester\DataObjectTestCase

class DataClassTest extends DataObjectTestCase
{
    /**
     * @test
     */
    public function it_returns_the_right_values(): void
    {
        $this->testDataObjects(
            ClassExpectation::create(DataClass::class, [
                'getString' => ['hello', 'world'],
                'getInt'    => [0, -1, PHP_INT_MAX],
                'getArray'  => [['a', 'b', 'c'], [1, 2, 3]],
            ])
        );
    }
}
```

*(Alternatively to extending the `DataObjectTestCase`, you could also `use TestsDataObjects`)*

This will test that all the getters exist, and that they give back the values provided in the constructor.

The array in the class expectation lists the getters, in the same order as their respective constructor arguments. For each property, any number of possible values can be given. The tester will construct a couple of objects using those values, and assert that the getters return the right values.

Features
--------

[](#features)

### Testing optional arguments

[](#testing-optional-arguments)

If an argument can be optional (having a default value), you can use a `DefaultPropertyCase` to define the default expected value.

```
use Sytzez\DataObjectTester\DataObjects\ClassExpectation
use Sytzez\DataObjectTester\PropertyCases\DefaultPropertyCase;

ClassExpectation::create(ValidatedDataClass::class, [
    'getNumber' => [
        1,
        10,
        new DefaultPropertyCase(0),
    ],
]),
```

### Testing validation exceptions

[](#testing-validation-exceptions)

If passing certain values should cause an error or exception to be thrown during instantiation, use the `ConstructorExceptionPropertyCase`. So if your constructor looks like this:

```
public function __construct(
    private int $number,
) {
    if ($this->number < 0) {
        throw new \InvalidArgumentException('Number cannot be negative');
    }
}
```

You can test it like this:

```
use Sytzez\DataObjectTester\DataObjects\ClassExpectation
use Sytzez\DataObjectTester\PropertyCases\ConstructorExceptionPropertyCase;

ClassExpectation::create(ValidatedDataClass::class, [
    'getNumber' => [
        1,
        10,
        new ConstructorExceptionPropertyCase(-1, 'Number cannot be negative'),
    ],
]),
```

If multiple arguments should cause an exception, the test will assert that one the possible exceptions will be thrown.

### Testing transformative properties

[](#testing-transformative-properties)

If your data class alters the passed in values in some way, you can use the `TransformativePropertyCase` class in your class expectation.

Let's say your getter looks like this:

```
public function getString(): string
{
    return ucfirst($this->string);
}
```

Then you could write the class expectation like this:

```
use Sytzez\DataObjectTester\DataObjects\ClassExpectation
use Sytzez\DataObjectTester\PropertyCases\TransformativePropertyCase;

ClassExpectation::create(TransformativeDataClass::class, [
    'getString' => [
        new TransformativePropertyCase('hello', 'Hello'),
        new TransformativePropertyCase('world', 'World'),
    ],
]),
```

### Testing with closures

[](#testing-with-closures)

You can use the `ClosurePropertyCase` to do your own validation of getter output by writing a closure returning a `bool`. Example:

```
use Sytzez\DataObjectTester\PropertyCases\ClosurePropertyCase;

ClassExpectation::create(AcmeClass::class, [
    'getCollection' => new ClosurePropertyCase(
        new ArrayCollection([1, 2, 3]),
        static fn (Collection $output): bool =>
            $output->contains(1)
            && $output->contains(2)
            && $output->contains(3)
    ),
])
```

### Test case generator

[](#test-case-generator)

By default, a minimal amount of objects is created, covering each specified property value at least once. But if you wish to cover every possible combination of property values, you can use a `MaximalCaseGenerator`, by passing it as the second argument to `testDataObjects`, like so:

```
use Sytzez\DataObjectTester\Generators\MaximalCaseGenerator

$this->testDataObjects(
    ClassExpectation::create(DataClass::class, [
        'getString' => ['hello', 'world'],
        'getInt'    => [0, -1, PHP_INT_MAX],
        'getArray'  => [['a', 'b', 'c'], [1, 2, 3]],
    ]),
    new MaximalCaseGenerator()
);
```

In this case, 12 (2 \* 3 \* 2) objects will be instantiated and tested, instead of the minimum of 3 cases.

You can cap the number of objects by providing an argument to the generator, e.g. `new MaximalCaseGenerator(20)`. The default cap is 100.

You can also provide your own case generators by implementing the [CaseGeneratorStrategy](src/Contracts/Generators/CaseGeneratorStrategy.php).

### Special cases

[](#special-cases)

If there is a case not (yet) covered by the package, you can of course still add your own `@test` methods to the test case.

Code Quality
------------

[](#code-quality)

### Unit tests

[](#unit-tests)

The project has a testing coverage of 100% of lines using PHPUnit.

### PHPStan

[](#phpstan)

PHPstan reports 0 errors on the maximum level 8.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1659d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e710b6dae460dd5050ebb8baee49d9f0a9afd51df0957dc395327cd0c88a8a63?d=identicon)[sytzez](/maintainers/sytzez)

---

Top Contributors

[![sytzez](https://avatars.githubusercontent.com/u/505202?v=4)](https://github.com/sytzez "sytzez (131 commits)")

---

Tags

dtoimmutablephpphpunittestingtestingphpunitdtoimmutable

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sytzez-data-object-tester/health.svg)

```
[![Health](https://phpackages.com/badges/sytzez-data-object-tester/health.svg)](https://phpackages.com/packages/sytzez-data-object-tester)
```

###  Alternatives

[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[spatie/phpunit-snapshot-assertions

Snapshot testing with PHPUnit

69417.9M510](/packages/spatie-phpunit-snapshot-assertions)[phpunit/phpunit-selenium

Selenium Server integration for PHPUnit

59610.9M150](/packages/phpunit-phpunit-selenium)[yoast/phpunit-polyfills

Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests

18338.5M841](/packages/yoast-phpunit-polyfills)[ta-tikoma/phpunit-architecture-test

Methods for testing application architecture

10745.9M13](/packages/ta-tikoma-phpunit-architecture-test)[matthiasnoback/symfony-config-test

Library for testing user classes related to the Symfony Config Component

1679.8M395](/packages/matthiasnoback-symfony-config-test)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
