wojciech.nawalaniec/mother-object-factory 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wojciech.nawalaniec/mother-object-factory

Composer 安装命令:

composer require wojciech.nawalaniec/mother-object-factory

包简介

Library making creation of mother objects super easy.

README 文档

README

logo

Mother object factory

This library I created for myself, as I like creating Mother Objects 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

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

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

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.

统计信息

  • 总下载量: 13
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-05-28