jced-artem / uopz
php uopz 库函数的包装器。在运行时更改函数行为时,对单元测试非常有用
1.0.6
2016-12-06 15:08 UTC
Requires
- php: >=5.6.0
This package is not auto-updated.
Last update: 2024-09-29 02:04:34 UTC
README
php uopz 库函数的包装器。在运行时更改函数行为时,对单元测试非常有用。
需求
oupz library
您可以通过以下方式安装它
- 对于
php-7
:pecl install uopz
- 对于
php-5.6
:pecl install uopz-2.0.7
安装
composer require jced-artem/uopz
使用方法
class yourClass
{
use Jced\UopzTrait;
// ...
}
将第三方调用的非返回函数挂钩以获取其返回值
public function foo($data) {
return Database::insert($data);
}
public function bar() {
$data = ['field' => 'value'];
foo($data);
}
有时在测试 bar() 时,您可能想知道 foo() 发生了什么
public function testBar() {
$this->uopzFunctionHook(
'foo',
function ($data) {
return $data;
},
$fooResult
);
bar();
$this->assertEqual(['field' => 'value'], $fooResult);
}
使用条件列表替换函数的返回值。
在调用此方法之前,源函数不能重新定义或定义为闭包。
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');
}
您可以通过以下方式模拟所有调用
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
);
}
一致返回
public function testFoo() {
$this->uopzFunctionConditionReturn(
'selectAll',
[
[0 => 'user1', 2 => 'user3'],
'some other result here',
]
);
}
用另一个函数替换一个函数
$this->uopzFunctionReplace(['mysqli', 'query'], 'mysql_query'); // downgrade :)
简单返回
只返回一些东西,没有更多
$this->uopzFunctionSimpleReturn('functionName', 'return string');
备份函数
$this->uopzBackup('functionName');
恢复函数
$this->uopzRestore('functionName');
静音函数
让函数什么也不做 :)
$this->uopzMuteFunction('functionName');
别名
// uops_function
public function uopzFunction($function, Closure $closure, $backup = false);
// uops_redefine
public function uopzRedefine($constant, $value);
// uops_flags
public function uopzFlags($function, $flags);