PHPackages                             wojciech.nawalaniec/mother-object-factory - 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. wojciech.nawalaniec/mother-object-factory

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

wojciech.nawalaniec/mother-object-factory
=========================================

Library making creation of mother objects super easy.

013PHP

Since May 28Pushed 2y ago1 watchersCompare

[ Source](https://github.com/wnnawalaniec/MotherObjectFactory)[ Packagist](https://packagist.org/packages/wojciech.nawalaniec/mother-object-factory)[ RSS](/packages/wojciechnawalaniec-mother-object-factory/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

 [![logo](https://camo.githubusercontent.com/1df19161610092abe44ac9f2d9d6064f638d4136a7020e2b21f482d3d33e740c/68747470733a2f2f692e6962622e636f2f387254636368362f6d6f746865722d6f626a6563742d7265706f2d6c6f676f2d72662e706e67)](https://camo.githubusercontent.com/1df19161610092abe44ac9f2d9d6064f638d4136a7020e2b21f482d3d33e740c/68747470733a2f2f692e6962622e636f2f387254636368362f6d6f746865722d6f626a6563742d7265706f2d6c6f676f2d72662e706e67)

Mother object factory
=====================

[](#mother-object-factory)

This library I created for myself, as I like creating [Mother Objects](https://martinfowler.com/bliki/ObjectMother.html)in my test code. I've noticed that most of it is just bunch of boilerplate code that could be easily generated.

A mother object is a testing class that creates example objects for testing purposes, helping to streamline test setup and reuse fixtures across multiple tests. More on this can be found in link above.

Install
-------

[](#install)

`composer require --dev wojciech.nawalaniec/mother-object-factory`

Example
-------

[](#example)

Assume we have class like this in our code, which we use a lot in our tests:

```
final class User
{
    public function __construct(string $name)
    {
        $this->name = $name;
    }

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

    private string $name;
}
```

It is very simple class, and creating new instance of this class is as simple as:

```
$obj = new User('John Wick');
```

Yet, event though it's simple to create an object it's still not the best idea to have dozens of places responsible for creating `User` objects, as for when constructor of this class will change, many tests will have too. But there is much better reason why you would avoid creating new instances of `User` object in every test of an object where `User` is one of dependencies. Imagine class like:

```
class Post
{
    public static function create(Title $title, Content $content, User $author): void
    {
        // ...
    }
}
```

For class like this we would probably have many tests, as there are 3 arguments. In not every test value of every argument is evenly special for us. Some tests will focus more on `$title` argument, some on `$content` and some on `$author`. **Creating instances of each object in each test, can blur an image of what is the goal of that specific test case.**

So when I Want to test this `create` method in context of `$title` param I want to be able to write a test, where `$title`parameter is the main star, and it catches 99% of reader's attention. I want to make very clear of what is tested.

```
public function testCreatingPost_TitleIsTooLong_ThrowsException(): void
{
    $tooLongTitle = TitleMother::newObject()
        ->ofLength(Title::MAX_LENGHT+1)
        ->create();

    Post::create($tooLongTitle, ContentMother::any(), UserMother::any());
}
```

Usage
-----

[](#usage)

To generate mother object simply type:

```
./vendor/bin/motherObjectFactory generate App\\Package\\Class
```

Change `App\\Package\\Class` to whatever class in your code. Then you will be asked to give a namespace, where mother object shall be created. You can use autocomplete based on your `composer.json` file, typing first letters and using `TAB` key. When you are done, type `enter` and new fill will be created. Change it if it's needed.

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity25

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/c20f6cbb4e00fe3a0d81fb57ebf952e1cd82e5a7488f5cf4044e76f7a6bba9f9?d=identicon)[Wojciech Nawalaniec](/maintainers/Wojciech%20Nawalaniec)

---

Top Contributors

[![wnnawalaniec](https://avatars.githubusercontent.com/u/39557684?v=4)](https://github.com/wnnawalaniec "wnnawalaniec (18 commits)")

---

Tags

object-motherphpphp-librarytesting-toolsunit-testing

### Embed Badge

![Health badge](/badges/wojciechnawalaniec-mother-object-factory/health.svg)

```
[![Health](https://phpackages.com/badges/wojciechnawalaniec-mother-object-factory/health.svg)](https://phpackages.com/packages/wojciechnawalaniec-mother-object-factory)
```

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k77.5M6.7k](/packages/vimeo-psalm)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M570](/packages/beberlei-assert)[mikey179/vfsstream

Virtual file system to mock the real file system in unit tests.

1.4k108.0M2.7k](/packages/mikey179-vfsstream)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)

PHPackages © 2026

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