komex/influence 问题修复 & 功能扩展

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

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

komex/influence

Composer 安装命令:

composer require --dev komex/influence

包简介

Mocking any objects and classes

README 文档

README

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version License ![Gitter](https://badges.gitter.im/Join Chat.svg)

Influence

Influence is the PHP library gives you ability to mock any objects and static classes. Works only if you use Composer autoloader. This library very useful for testing your code.

Requirements

  • PHP 5.4 or higher
  • SPL extension
  • Tokenizer extension
  • Composer loader

Installation

To add Influence as a local, per-project dependency to your project, simply add a dependency on komex/influence to your project's composer.json file. Here is a minimal example of a composer.json file that just defines a develop dependency on Influence:

{
    "require-dev": {
        "komex/influence": "1.1.*"
    }
}

Usage

Influence must be injected as early as possible. If you are using unit test framework like unteist or PHPUnit the best way to do this is include autoload and influence in bootstrap file.

require 'vendor/autoload.php';
Influence\Influence::affect();

Since this moment you are able to mock any objects and static classes. By default, all objects behave as usual. You need to configure behavior of each object or class you need to control.

Manage objects

Let imagine we have a simple class A:

class A
{
    public function sum($a)
    {
        return $a + $this->rand(0, $a);
    }
    private fuction rand($min, $max)
    {
        return rand($min, $max);
    }
}

Custom method behavior.

So, if we create an object of class A we can invoke only sum() method and control only $a and never know result of our operation.

$a = new A();
echo $a->sum(1); // ??
echo $a->sum(7); // ??

But with Influence you can simply test this code. Just specify the behavior of sum() like this:

$a = new A();
$method = Influence\RemoteControl::controlObject($a)->get('rand');
$method->setValue(new Value(1));
echo $a->sum(1); // 2
echo $a->sum(7); // 8
$method->setValue();
echo $a->sum(1); // ??
echo $a->sum(7); // ??

Log method calls

If you don't need to set custom method behavior, but want to know how many times method was called and with what arguments.

$a = new A();
$method = Influence\RemoteControl::controlObject($a)->get('rand');
$method->setLog(true);
echo $a->sum(1); // ??
echo $a->sum(7); // ??
var_dump($method->getLogs()); // [ [0, 1], [0, 7] ]

License

Influence by Andrey Kolchenko is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Creative Commons License

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: CC
  • 更新时间: 2014-10-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固