alphametric/reflection-assertions

This package is abandoned and no longer maintained. No replacement package was suggested.

A package to enable unit testing an object's private & protected properties and methods.

v1.0 2018-10-29 15:43 UTC

This package is auto-updated.

Last update: 2020-02-17 10:07:52 UTC


README

This package adds a set of reflection-based assertions to $this within your Laravel TestCase, or generic PHP TestCase. It also adds several methods that allow direct access to private & protected properties and methods on any object. You can use these methods to bypass PHP's scope restrictions and fully unit test your classes.

Installation

You can install the package via composer:

composer require alphametric/reflection-assertions

Once the package is installed, you'll need to add the ReflectionAssertions trait to the TestCase class:

namespace Tests;

use Alphametric\Assertions\ReflectionAssertions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication, ReflectionAssertions;
}

Usage

If you want to directly access properties or methods on an object, you can use the dedicated functions:

Name Arguments
getProperty $property_name, $object_instance
setProperty $property_name, $object_instance, $new_value
callFunction $function_name, $object_instance, $function_parameters[]
$object = new MailMessage();

$this -> getProperty("bcc_address", $object);
$this -> setProperty("bcc_address", $object, "john@example.com");

$this -> callFunction("getBcc", $object);
$this -> callFunction("removeCC", $object, ["john@example.com"]);
$this -> callFunction("internalSend", $object, ["john@example.com", "Hello John!"]);

Assertions

Each of the dedicated functions is also made available as a traditional assertion:

$this -> assertFunctionReturns();

Since all of the assertions return the class instance, you can also chain multiple assertions:

$this -> assertPropertyIs()
      -> assertFunctionReturns()
      -> assertPropertyIs();

The following assertions are available to use:

Name Arguments
assertFunctionReturns $expected_value, $function_name, $object_instance, $function_parameters[]
assertFunctionDoesNotReturn $expected_value, $function_name, $object_instance, $function_parameters[]
assertPropertyIs $expected_value, $property_name, $object_instance
assertPropertyIsNot $expected_value, $property_name, $object_instance
$this -> assertFunctionReturns(1, "privateMethod", $object);
$this -> assertFunctionDoesNotReturn(2, "protectedMethod", $object, ["param_1", "param_2"]);
$this -> assertPropertyIs("John", "privateProperty", $object);
$this -> assertPropertyIsNot("Alex", "protectedProperty", $object);

Since the setProperty method finishes by calling and returning the getProperty method, you can take advantage of two additional assertions to verify the change to the object's property. This is useful for Laravel's accessors / mutators:

Name Arguments
assertPropertyWhenSetIs $expected_value, $property_name, $object_instance, $new_value
assertPropertyWhenSetIsNot $expected_value, $property_name, $object_instance, $new_value
$this -> assertPropertyWhenSetIs("Mr. Alex", "privateProperty", $object, "Alex");
$this -> assertPropertyWhenSetIsNot("Ms. Jane", "protectedProperty", $object, "Jane");

Testing

You can run the test suite by using the following command within the root package directory:

composer test

Changelog

V1.0 - Initial release.

License

The MIT License (MIT). Please see License File for more information.