pbweb/mimic
Composer 安装命令:
composer require --dev pbweb/mimic
包简介
Mimic classes for functional testing
README 文档
README
This library can be used to create mimics of your classes, which can be used for (functional) testing.
A mimicked class is similar to mock objects with stub methods in phpunit, but on a functional level.
Installation
Install with composer
Install mimic using composer:
composer require pbweb/mimic
Example
Lets say you have a client class which talks to an external REST service. The interface might look like this:
interface RestClient { public function get($something); public function put($something); }
Now, using a class which really connects to the REST server in a (functional) test is a bad idea, since that server may not be in the scope of your test and influence the results.
To create a mimic client you need to extend MimicActionHandler like this:
class MimicRestClient extends MimicActionHandler implements RestClient { public function get($something) { return $this->handleAction(__FUNCTION__, func_get_args()); } public function put($something) { return $this->handleAction(__FUNCTION__, func_get_args()); } }
Now you can mimic the behaviour of the RestClient in your tests:
// In your dependency injection container: $mimicClient = new MimicRestClient(); // In your test setup: $mimicClient->enqueue('get', ['cheese'], 'cheese result'); // In your test or in a class which you are testing: $result = $mimicClient->get('cheese'); // returns 'cheese result'
Argument Matchers
In case you want more control over the arguments that are expected for a call, you can use argument matchers.
The ArgumentMatchers class instantiates several matchers which can be passed to the expected argument list of enqueue.
// Matches any call to update that has 1 as its first argument and any value as its second argument. $mimicClient->enqueue('update', [1, ArgumentMatchers::any(), 'result');
Usage
Extending MimicActionHandler will allow your class to have the mimic enqueue system.
Every method you want to mimic should have this as the body:
return $this->handleAction(__FUNCTION__, func_get_args());
See tests/Service/SampleMimic for an example.
enableQueue
$mimic->enableQueue();
enableQueue will enable the use of the queue.
disableQueue
$mimic->disableQueue();
disableQueue will stop the use of the queue.
isQueueEnabled
$isQueueEnabled = $mimic->isQueueEnabled();
isQueueEnabled will return a boolean value with the state of the queue.
enqueue
$mimic->enqueue($method, array $argumentList = [], $response = null, $throw = false);
enqueue will add a method call to the expected queue.
You can add as many method prediction as you like.
If the next call to the mimic is the expected call then the given response will be returned and it will be removed from the queue.
If the next call to the mimic is not the same as the expected call added to the queue then an exception will be thrown.
If you set throw to true then the response will be thrown instead of returned.
getQueueContent
$actionList = $mimic->getQueueContent();
getQueueContent will return all the remaining actions added to the queue as Action models.
See the Action class for more information about the model.
isFinished
$isFinished = $mimic->isFinished();
isFinished will return true if there are no more action left in the queue. false otherwise.
clearQueue
$mimic->clearQueue();
clearQueue will remove all the remaining actions from the queue.
pbweb/mimic 适用场景与选型建议
pbweb/mimic 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.6k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 08 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「testing」 「library」 「mock」 「stub」 「class」 「mimic」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pbweb/mimic 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pbweb/mimic 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pbweb/mimic 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
Testing Suite For Lumen like Laravel does.
The PHP SDK for Checkmango
Inbox pattern process implementation for your Laravel Applications
Mock PSR-18 HTTP client
Core library that defines common interfaces used by the rest of the intahwebz..
统计信息
- 总下载量: 8.6k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-08-24