Atanvarno\PHPUnit
, (*1)
Utility traits for PHPUnit test cases for interacting
with protected and private methods and properties., (*2)
Requirements
PHP >= 7.0 is required, but the latest stable version of PHP is recommended., (*3)
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., (*4)
public function callProtectedMethod($object, string $method, array $arguments = [])
Calls a protected or private method and returns its result., (*5)
Parameters
-
object
$object, (*6)
Required. The instance containing the method to call., (*7)
-
string
$method, (*8)
Required. The method name., (*9)
-
mixed[]
$arguments, (*10)
Optional. Defaults to []
. Arguments to pass to the method., (*11)
Throws
Returns
Atanvarno\PHPUnit\SetProtectedPropertyTrait
Provides means to set a protected or private property of an object for test
purposes., (*16)
public function setProtectedProperty($object, string $property, $value)
Sets a protected or private property., (*17)
Parameters
-
object
$object, (*18)
Required. The instance containing the property to set., (*19)
-
string
$property, (*20)
Required. The property name., (*21)
-
mixed
$value, (*22)
Required. The value to set., (*23)
Throws
Returns