tcz/phpunit-mockfunction
Composer 安装命令:
composer require --dev tcz/phpunit-mockfunction
包简介
PHPUnit extension that uses runkit to mock PHP functions (both user-defined and system)
README 文档
README
MockFunction is a PHPUnit extension that uses runkit to mock PHP functions (both user-defined and system) or static methods and use mockobject-style invocation matchers, parameter constraints and all that magic.
To use this extension, you have to install runkit first (PECL package). For a working version see https://github.com/zenovich/runkit/
To be able to mock system function (not user-defined ones), you need to turn on runkit.internal_override in the PHP config.
Installation
If you use composer, installing MockFunction is easy:
"require-dev": {
"tcz/phpunit-mockfunction": "1.0.0"
}
Then
php composer.phar update tcz/phpunit-mockfunction
Usage
Assuming you are in a PHPUnit test:
// Back to the future:
$flux_capacitor = new PHPUnit_Extensions_MockFunction( 'time', $this->object );
$einsteins_clock = time() + 60;
$flux_capacitor->expects( $this->atLeastOnce() )->will( $this->returnValue( $einsteins_clock ) );
Where $flux_capacitor is the stub function. It can be set up with the same fluent interface as a MockObject (excluding method, of course).
The 2nd parameter of the constructor ($this->object) is the object where we expect the function to be called. The "mocking" only takes effect from here, from all the other sources it will execute the "normal" function (see next line).
Variable $einsteins_clock contains the value that we will return instead of the "regular" value (we add 1 minute for the current time).
In the next line we set up the mock function with the fluent interface of a mock object.
The mocked function is active for the test object instance until $flux_capacitor->restore(); is called. If you happen to forget this in the end of the test case, normally it is not a problem, because you will test anew instance of your tested class with each test case.
To mock static methods, you can use PHPUnit_Extensions_MockStaticMethod class. It work int the same way as with functions:
$mocked_static = new PHPUnit_Extensions_MockStaticMethod( 'MyClass::myMethod', $this->object );
Advanced mocking
You can use all invocation matchers, constraints and stub returns, for example:
// This will execute the original function at the end, but will test
// the number of exections ( $this->once() ) and the correct parameter ( $this->equalTo() ).
$mocked_strrev = new PHPUnit_Extensions_MockFunction( 'strrev', $this->object );
$mocked_strrev->expects( $this->once() )->with( $this->equalTo( 'abc' ) )->will( $this->returnCallback( 'strrev' ) );
// This object cannot execute shell_exec.
$mocked_shell = new PHPUnit_Extensions_MockFunction( 'shell_exec', $this->object );
$mocked_shell->expects( $this->never() );
// Expecting to check the existence of 2 file, returning true for both.
$mocked_file_exists = new PHPUnit_Extensions_MockFunction( 'file_exists', $this->object );
$mocked_file_exists->expects( $this->exactly( 2 ) )
->with(
$this->logicalOr(
$this->equalTo( '/tmp/file1.exe' ),
$this->equalTo( '/tmp/file2.exe' )
)
)->will( $this->returnValue( true ) );
For further information see http://www.phpunit.de/manual/3.0/en/mock-objects.html
tcz/phpunit-mockfunction 适用场景与选型建议
tcz/phpunit-mockfunction 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 50.44k 次下载、GitHub Stars 达 48, 最近一次更新时间为 2013 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「testing」 「phpunit」 「mock」 「unit」 「runkit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tcz/phpunit-mockfunction 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tcz/phpunit-mockfunction 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tcz/phpunit-mockfunction 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
HiDev plugin for PHPUnit
Testing Suite For Lumen like Laravel does.
A cli tool which generates unit tests.
The PHP SDK for Checkmango
Mock PSR-18 HTTP client
统计信息
- 总下载量: 50.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 49
- 点击次数: 27
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2013-10-31