yguedidi/global-functions-mocker
Composer 安装命令:
composer require yguedidi/global-functions-mocker
包简介
A simple class that allow you mock global functions.
README 文档
README
A simple class that allow you mock global functions for your unit tests.
Note: This works only if you don't use absolute function calls (like \phpversion()) in your classes.
Installation
Add yguedidi/global-functions-mocker to the require-dev section of your composer.json
{
"require-dev": {
"yguedidi/global-functions-mocker": "*"
}
}
Basic usage
0 - Prerequisites
1 - Setup
First of all, call the GlobalFunctionsMocker::setUp() method in your test setUp() method.
This is needed to clear mocked global functions between tests.
2 - Namespaced function
Create a function named exactly like the global function in the namespace of your tested class, but define it in your test file.
The body of that function should be:
return GlobalFunctionsMocker::execute('global_function_name', func_get_args());
3 - Mock the function
Then just mock the global function you need in your tests with GlobalFunctionsMocker::mock('global_function_name').
It returns a subset of Mockery\Expectation so you can define some expectations: MockedFunction
Full exemple
This exemple assume your tested class and your unit test class are in different namespace.
// src/FooClass.php namespace Acme\FooClass; class FooClass { public function foo() { return phpversion(); } }
// tests/FooClassTest.php // Note the different namespace namespace Acme\Tests\FooClass { use Acme\FooClass; use YassineGuedidi\GlobalFunctionsMocker\GlobalFunctionsMocker as GFM; class FooClassTest extends \PHPUnit_Framework_TestCase { protected function setUp() { GFM::setUp() } public function testFoo() { GFM::mock('phpversion')->once()->withNoArgs()->andReturn('foo'); $object = new FooClass(); $this->assertEquals('foo', $object->foo()); } } } // IMPORTANT: Define the function in the tested class namespace namespace Acme\FooClass { use YassineGuedidi\GlobalFunctionsMocker\GlobalFunctionsMocker as GFM; function phpversion() { // Don't forget 'func_get_args()' for fallback to the real global function work return GFM::execute('phpversion', func_get_args()); } }
Reference
GlobalFunctionsMocker::mock($functionName)
Mock a global function. Should be called in your tests.
- $functionName
Name of the global function to mock
GlobalFunctionsMocker::execute($functionName, $args)
Execute a mocked global function. Call this in your namespaced global function definition.
-
$functionName
Name of the global function to execute -
$args (optional)
Arguments passed to the global function (empty array by default).
Important: This should befunc_get_args()in the namespaced function. It allow fallback to the real global if it's not mocked.
It will fallback to the real global function if the function isn't mocked by a test, or if you mock it with some expectations but without a return value.
And if the real global function is not define (missing PHP extension for exemple), it will gracefully
throw a PHPUnit_Framework_SkippedTestError exception that will skip the current test with a
useful message.
MockedFunction
Available functions: with(), withArgs(), withNoArgs(), withAnyArgs(), andReturn(), andReturnValues(), andReturnUsing() andReturnUndefined(), andReturnNull(), andThrow(),andThrowExceptions(), zeroOrMoreTimes(), times(), never(), once(), twice(), atLeast(), atMost(), between().
Look at Mockery's documentation about Expectation Declarations and Argument Validation for more detail.
License
See LICENSE file
yguedidi/global-functions-mocker 适用场景与选型建议
yguedidi/global-functions-mocker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 07 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mock」 「function」 「global」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yguedidi/global-functions-mocker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yguedidi/global-functions-mocker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yguedidi/global-functions-mocker 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
PHPUnit extension to Mock PHP internal functions using Runkit.
A lite function pack for PHP.
MediaWiki extension that allows embedding external content, specified by URL, into your wiki pages
PostgreSQL ROUND() function for Doctrine ORM
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-16