定制 tcz/phpunit-mockfunction 二次开发

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

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

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

Bitdeli Badge Build Status

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 tcz/phpunit-mockfunction 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 50.44k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 49
  • 点击次数: 27
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 48
  • Watchers: 4
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2013-10-31