承接 jced-artem/uopz 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

jced-artem/uopz

最新稳定版本:1.0.6

Composer 安装命令:

composer require --dev jced-artem/uopz

包简介

Wrapper for php uopz library's functions. Useful for unit testing for changing function's behavior in runtime

README 文档

README

Wrapper for php uopz library's functions. Very useful for unit testing for changing function's behavior in runtime.

Requirements

oupz library

You can install it:

  • for php-7: pecl install uopz
  • for php-5.6: pecl install uopz-2.0.7

Install

composer require jced-artem/uopz

Usage

class yourClass
{
    use Jced\UopzTrait;
    // ...
}

Hook non-return third-party-called function to get return from it

public function foo($data) {
    return Database::insert($data);
}
public function bar() {
    $data = ['field' => 'value'];
    foo($data);
}

Sometime when you testing bar() you may want to know what happend in foo()

public function testBar() {
    $this->uopzFunctionHook(
        'foo',
        function ($data) {
            return $data;
        },
        $fooResult
    );
    bar();
    $this->assertEqual(['field' => 'value'], $fooResult);
}

Replace function's return value using list of conditions.

Source function cant be redefined or defined as closure before using this method.

public function selectAll($table) {
    return $db->select()->from($table)->fetchAll();
}
public function foo() {
    $result1 = $this->selectAll('user');
    // do something
    $result2 = $this->selectAll('article');
    // do something
    $result3 = $this->selectAll('post');
}

You can mock all calls just doing:

public function testFoo() {
    $this->uopzFunctionConditionReturn(
        'selectAll',
        [
            ['table', 'user', [0 => 'user1', 2 => 'user3']],
            ['table', 'post', function () { return 'some other result here'; }],
        ],
        null // for all other queries
    );
}

Consistent return

public function testFoo() {
    $this->uopzFunctionConditionReturn(
        'selectAll',
        [
            [0 => 'user1', 2 => 'user3'],
            'some other result here',
        ]
    );
}

Replace one function with another

$this->uopzFunctionReplace(['mysqli', 'query'], 'mysql_query'); // downgrade :)

Simple return

Just return something, nothing more

$this->uopzFunctionSimpleReturn('functionName', 'return string');

Backup function

$this->uopzBackup('functionName');

Restore function

$this->uopzRestore('functionName');

Mute function

Ask function to do nothing :)

$this->uopzMuteFunction('functionName');

Aliases

// uops_function
    public function uopzFunction($function, Closure $closure, $backup = false);
// uops_redefine
    public function uopzRedefine($constant, $value);
// uops_flags
    public function uopzFlags($function, $flags);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-11-30