定制 third-engine/factory 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

third-engine/factory

Composer 安装命令:

composer require third-engine/factory

包简介

Provides object creation that makes mocking and good unit testing easy.

README 文档

README

Factory is used to create objects in a standard way that allow for easy replacement with test double from unit tests. This has only been tested with phpunit, but there isn't a reason it should not be able to work with other unit testing frameworks.

// This is equivalent to $newObject = new NewObjectClass(); in your normal code execution $newObject = Factory::createNewObject(NewObjectClass::class); $newObject->standardOperations();

Then when you are in a unit test, there is a simple method to replace $newObject with a test double.

// $newObjectMock is already a mock object, and you want it to take the place of $newObject Factory::injectObject(NewObjectClass::class, $newObjectMock);

In many cases, your constructors will take parameters. With Factory, it is simple to pass parameters to object constructors.

// This is equivalent to $newObject = new NewObjectClass($someOtherObject); $newObject = Factory::createNewObject(NewObjectClass::class, [$someOtherObject]); $newObject->standardOperations();

Then when you are in a unit test, everything works the same way. $newObjectMock is already a mock object. Factory::injectObject(NewObjectClass::class, $newObjectMock);

Factory also supports being able to create different instances of the same object. Many times you will need to create the same object in multiple methods.

$newObject1 = Factory::createNewObject(NewObjectClass::class);

// a little while later 4newObject2 = Factory::createNewObject(NewObjectClass::class);

// In your test, assuming $newObject1Mock and $newObject2Mock are mock objects already. Use a zero-based index // to order the injections you want. Factory::injectObject(NewObjectClass::class, $newObject1Mock, 0); Factory::injectObject(NewObjectClass::class, $newObject2Mock, 1);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-11-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固