konsulting / exposer
Requires (Dev)
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2024-10-28 07:12:53 UTC
README
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:
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.
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.
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.
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.