skagarwal/reflection
最新稳定版本:1.0.5
Composer 安装命令:
composer require --dev skagarwal/reflection
包简介
Test any Private/Protected Methods/Properties of any class
关键字:
README 文档
README
Test your Private/Protected Methods/Properties with any testing package and with Zero configuration.
New: Can reflect multiple classes at same time.
Usage
Step 1: Install Through Composer
composer require skagarwal/reflection --dev
Step 2: Import the trait
Import the SKAgarwal\Reflection\ReflectableTrait in your TestClass of any package. Eg: PhpUnit, PhpSpec, Laracasts\Integrated.
For PhpUnit
use SKAgarwal\Reflection\ReflectableTrait class ModelTest extends PHPUnit_Framework_TestCase { use ReflectableTrait; }
And That's it. You are all set to go. :)
Now you can do the following to test private/protected methods or properties:
Instantiate the Reflector
By using reflect() method like this:
$randomClass = new RandomClass(); $this->reflect($randomClass);
This Method is not chainable
Note: Its preferable to use this in constructor or for PhpUnit in setUp() method etc.
By using on() method like this:
$randomClass = new RandomClass(); $this->on($randomClass)->call($method, $args = []);
This method is chainable **Note: **This method relfects the class object only once. That is only for one call to method or property. It can be used for relfecting multiple classes at same time.
Available Methods
reflect($classObj);
Description: Reflect the Class Object
on($classObj);
Description: Reflect the Class Object. This is Chainable Method.
Possible Chaining:
$this->on($classObject)->callMethod($arguments = []); $this->on($classObject)->call($method, $arguments = []); $this->on($classObject)->get($property); $this->on($classObject)->get{Proerty};
#### call($method, $arguments = []); **Description:** Call any valid public/Private/Protected Method of reflected Class Object. This is not Chainable Method.
#### call{Method}($arguments = []); **Description:** Same as `call()` but dynamically calls the method. This is not Chainable Method.
{method} Can be any valid public/private/protected method of reflected Class Object.
#### get($property); **Description:** Get the value of any valid Public/Private/Protected property of the reflected Class Object. This is not Chainable Method.
#### get{Property}; **Description:** Same as `get()` but dynamically gets the value of the property. This is not Chainable Method.
{property} Can be any valid Public/Private/Protected property of the reflected Class Object.
#### set($name, $value); **Description:** Set the value of valid Public/Private/Protected property of the reflected Class Object. This is not Chainable Method.
#### set{Property} = $value; **Description:** Same as `set()` but dynamically sets the value of the property. This is not Chainable Method.
{property} Can be any valid Public/Private/Protected property of the reflected Class Object.
Reflect Multiple classes at same time
Example:
// Considering phpunit protected function setUp() { $this->foo = new Foo(); $this->reflect($this->foo); } public function test_something() { $hello = $this->callSayHello(); // this will call SayHello() of class `Foo` $this->assertEquals('Hello', $hello); $hello = $this->on(new FooBar())->callSayHello(); // this will call SayHello() of class `FooBar` $this->assertEquals('Hello FooBar', $hello); $hello = $this->callSayHello(); // this will call SayHello() of class `Foo` $this->assertEquals('Hello', $hello); }
skagarwal/reflection 适用场景与选型建议
skagarwal/reflection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.3k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2015 年 05 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「testing」 「reflection」 「unittesting」 「Private Methods」 「Protected Methods」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 skagarwal/reflection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 skagarwal/reflection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 skagarwal/reflection 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Library emulating the PHP internal reflection using just the tokenized source code.
Testing Suite For Lumen like Laravel does.
Parse use statements for a reflection object
The PHP SDK for Checkmango
Generate UML class diagrams by reflection for your PHP projects
Powerful class discovery system for Laravel with attribute-based scanning, directory traversal, and monorepo support
统计信息
- 总下载量: 6.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 16
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-05-30