An open API service indexing awesome lists of open source software.

https://github.com/konsulting/exposer

Access non-public methods and properties on classes.
https://github.com/konsulting/exposer

php reflection testing unit-testing

Last synced: 5 months ago
JSON representation

Access non-public methods and properties on classes.

Awesome Lists containing this project

README

          

# Exposer

Although for the most part only public methods of classes are unit tested, it's sometimes necessary or helpful to have access to protected or private methods and properties.
Exposer provides a convenient way to do this whilst keeping your tests clear and concise.

## Installation
Install via Composer:
```
$ composer require konsulting/exposer
```

## Usage
Consider the following class:
```php
class ClassUnderTest
{
protected $secret = 'My secret';

protected static $anotherSecret = 'My static secret';

protected function add($number1, $number2)
{
return $number1 + $number2;
}

protected static function multiply($number1, $number2)
{
return $number1 * $number2;
}
}
```

There are multiple ways to test the protected methods and properties on this class.

### Base Exposer
The most direct way is with the `BaseExposer` class.
The subject must be passed into each method, and may be either an instance or the (string) class name.

```php
use Konsulting\Exposer\BaseExposer;

// With an instance
BaseExposer::hasMethod(new ClassUnderTest, 'add'); // true
BaseExposer::invokeMethod(new ClassUnderTest, 'add', [1, 1]); // 2
BaseExposer::getProperty(new ClassUnderTest, 'secret'); // 'My secret'

// Static context
BaseExposer::hasMethod(ClassUnderTest::class, 'multiply'); // true
BaseExposer::invokeMethod(ClassUnderTest::class, 'multiply', [2,2]); // 4
BaseExposer::getProperty(ClassUnderTest::class, 'anotherSecret'); // 'My static secret'
```

### Exposer
The `Exposer` class allows the use of a class's inaccessible methods and properties as if they were public.

```php
use Konsulting\Exposer\Exposer;

$exposer = Exposer::make(new ClassUnderTest);

$exposer->add(1, 1); // 2
$exposer->multiply(2, 2); // 4
$exposer->secret; // 'My secret'
$exposer->anotherSecret; // 'My static secret'

// These non-magic methods are also available
$exposer->invokeMethod('add', [1, 1]); // 2
$exposer->getProperty('secret'); // 'My secret'
```

Exposer can also access class's static methods and properties without the need to provide an instance.

```php
use Konsulting\Exposer\Exposer;

$exposer = Exposer::make(ClassUnderTest::class);

$exposer->multiply(2, 2); // 4
$exposer->anotherSecret; // 'My static secret'

$exposer->invokeMethod('multiply', [2, 2]); // 4
$exposer->getProperty('anotherSecret'); // 'My static secret'
```

**Note:** Static methods and properties are available from an instance context, but of course non-static methods and properties are not available from a static context.