flarone/reflection 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

flarone/reflection

最新稳定版本:1.0

Composer 安装命令:

composer require --dev flarone/reflection

包简介

Test any Private/Protected Methods/Properties of any class

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License
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 flarone/reflection --dev

Step 2: Import the trait

Import the Flarone\Reflection\ReflectableTrait in your TestClass of any package. Eg: PhpUnit, PhpSpec, Laracasts\Integrated.

For PhpUnit

use Flarone\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);
  }

Contribution

Feel free to report issues or make Pull Requests. If you find this document can be improved in any way, please feel free to open an issue for it.

License

The php-reflection-deflector was forked from skagarwal/reflection. The php-reflection-deflector is open-sourced software licensed under the MIT license

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固