Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Reflection Laravel Package

skagarwal/reflection

View on GitHub
Deep Wiki
Context7

Php-Reflection-Deflector

Build Status 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 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};

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);
  }
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity