Download the PHP package kairos-project/tests without Composer
On this page you can find all versions of the php package kairos-project/tests. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package tests
Tests
A test helper library
This library is a simple helper for tests of PHP application, it provide functions that aim to be involved in constructor testing, getters and setters, etc...
Basic usage
The test helper library is designed to automate the tests for basic logic elements. These basics elements will be the simple constructor testing, the getter/setter or more abstractly accessor tests, and finally the instance construction without requirements.
To be used, the test case has to extends the AbstractTestClass
class, and define the tested class.
Constructor test
First of all is the constructor test. To validate the construction of an instance, its necessary to test multiples cases:
- The parameter assignation
- The parameter default value
To do this, the assertConstructor()
method allow two arguments as array. The first array will contain the name of
the property expected to be assigned as key and the value to assign as value, representing the given arguments in the
same order as the call. The second array will have the same structure and represent the default value of the
properties.
To test the value assignment as reference (useful for object), the the property have to be prefixed by the same:
keyword.
Since version 2.3
It is also possible to make more complex assertion by providing a user defined constraint using the
KairosProject\Constrait\InjectionConstraint
class.
Here an example of data that is injected in an array by the constructor before be stored in the property :
Access a protected or private method
To access a private or protected method, the getClassMethod('name')
can be used. It will return an instance of
ReflectionMethod configured to be accessible by default.
Test a simple getter or a simple setter
The simple getters and setters are a common practice to access private or protected properties in an object. To
validate that a property is correctly assigned or returned, then the
assertIsSimpleGetter('property', 'getterMethodName', 'value')
or the
assertIsSimpleSetter('property', 'getterMethodName', 'value')
methods can be used.
If you want to test both at the same time, then it is possible to use the
assertHasSimpleAccessor('property', 'value')
method.
Get a fresh instance without calling the constructor
As we don't want to test the constructor for each tests, it is possible to get a fresh instance by calling the
getInstance()
method. The argument of this method is optional and can be used to set the dependencies directly
inside the properties, using the ReflectionProperty instead of the constructor or the setters.
Get invocation builder to configure mock calls
To create a new InvocationBuilder instance, the getInvocationBuilder($mock, new Invocation(), 'methodName')
can be
used. This method is nothing more than a helper for $mock->expect($count)->method('methodName')
;
Bulk properties content validation
To validate the value stored by a set of property, use the assertPropertiesSame
and assertPropertiesEqual
methods.
Set up the tested class at runtime
In specific cases it is possible to see the need of defining the tested class at runtime. For this specific purpose, it is possible to use the runTestWithInstanceOf
method.