atanvarno / test-util
PHPUnit test case utilities
Requires
- php: ^7.0|^8.0
Suggests
- phpunit/phpunit: The PHP unit testing framework
This package is auto-updated.
Last update: 2024-10-22 20:39:53 UTC
README
Utility traits for PHPUnit test cases for interacting with protected and private methods and properties.
Requirements
PHP >= 7.0 is required, but the latest stable version of PHP is recommended.
Installation
$ composer require atanvarno/test-util:^0.1.0
Basic Usage
class YourTest extends \PHPUnit\Framework\TestCase { // Include the traits use Atanvarno\PHPUnit\{CallProtectedMethodTrait, SetProtectedPropertyTrait}; // Write your tests public function testYourMethod() { $testObject = new SomeClass(); // Set an inaccessible property $this->setProtectedProperty($testObject, 'propertyName', 'value'); // Call an inaccessible method $result = $this->callProtectedMethod( $testObject, 'methodName', ['argument 1', 'argument 2', '...'] ); // Do your assertations // ... } }
Atanvarno\PHPUnit\CallProtectedMethodTrait
Provides means to call a protected or private method of an object for test purposes.
public function callProtectedMethod($object, string $method, array $arguments = [])
Calls a protected or private method and returns its result.
Parameters
-
object
$objectRequired. The instance containing the method to call.
-
string
$methodRequired. The method name.
-
mixed[]
$argumentsOptional. Defaults to
[]
. Arguments to pass to the method.
Throws
-
Non-object passed as first parameter.
Returns
-
mixed
The return value of the method call.
Atanvarno\PHPUnit\SetProtectedPropertyTrait
Provides means to set a protected or private property of an object for test purposes.
public function setProtectedProperty($object, string $property, $value)
Sets a protected or private property.
Parameters
-
object
$objectRequired. The instance containing the property to set.
-
string
$propertyRequired. The property name.
-
mixed
$valueRequired. The value to set.
Throws
-
Non-object passed as first parameter.
Returns
void