This package contains support traits to ease unit testing.
You should install this package into your project using composer. To do so you can add the following to your composer.json file:
"require-dev": {
"jippi/cakephp-test-utilities": "dev-master"
}
The usage of these traits requires at least PHP 5.4. At this point there is one trait:
AccessibilityHelperTrait
: Gain access protected properties and methods.
This trait gains you access to protected properties and methods. You don't need of a new class with pass-through methods. It uses reflection to achieve this.
Add the trait at the top of your test case:
use \FriendsOfCake\TestUtilities\AccessibilityHelperTrait;
Now that you have the trait you need to set which object you want to access. You can do this globally for the entire test in setUp()
or in your test methods:
$object = new ObjectIAmGoingToTest();
$this->setReflectionClassInstance($object);
$this->defaultReflectionTarget = $object; // (optional)
You can get and set the protected properties:
$data = 'FriendsOfCake';
$this->setProtectedProperty('_myProperty', $data, $object);
$expected = $data;
$actual = $this->getProtectedProperty('_myProperty', $object);
$this->assertEquals($expected, $actual);
You can directly call protected methods:
$parameters = [$argument1, $argument2];
$expected = $expectedReturnValue;
$actual = $this->callProtectedMethod('_myMethod', $parameters, $object);
$this->assertEquals($expected, $actual);