Download the PHP package adgoal/phpunit-entity-tester without Composer
On this page you can find all versions of the php package adgoal/phpunit-entity-tester. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download adgoal/phpunit-entity-tester
More information about adgoal/phpunit-entity-tester
Files in adgoal/phpunit-entity-tester
Package phpunit-entity-tester
Short Description Quickly test your entities
License MIT
Informations about the package phpunit-entity-tester
phpunit-entity-tester : Quickly test your entities
You can write very quickly unit tests for the accessors, adders and removers of your entities.
Requirements
- PHP : 7.0 or later
- phpunit/phpunit : 6 or later
Installation
- With Composer
Quick example
This is an example of unit test file:
Documentation
Test the accessors
1. Add 'use' statement
2. Create the tester object
Where $entity
is the entity to test and 'attribute'
the base name of setter and getter methods.
In this case, the tester will test the methods 'setAttribute' and 'getAttribute'.
3. Configure the tester
Your can change the setter and getter methods like this :
You can remove the fluent constaint for the setter (set to true
by default)
4. Run tests
Simply use the test
method :
In this case the tester :
- calls the setter with 'the value'
- tests the fluent constraint
- tests that the getter return 'the value'
If the value returned by the getter have to be different to the value used with the setter
use the second argument of the test
method like this :
Test Setter
You can also only test the setter method like that :
This method calls the setter method and tests the fluent constraint
Test Getter
You can also only test the setter method like that :
This method calls the getter method and tests the return with value in parameter
Test the adders, removers and collection getters
1. Add 'use' statement
2. Create the tester object
Where $entity
is the entity to test and 'items'
the base name for adder, remover and getter methods. For the adder and remover methods, the final 's' of the base name will be removed.
In this case, ther tester will test the methods 'addItem', 'removeItem' and 'getItems'.
3. Configure the tester
You can change the adder, remover and getter methods like this :
You can remove the fluent constraint for the adder and remover methods (set to true
by default)
By default, the tester considere that the collection respect the unicity of its items. You can force the tester to considere thaht the collection don't respect the unicity like that :
4. Run tests
Simply use the test
method :
This method needs two arguments to work. In this case the tester :
- calls and tests the adder method with the first value
- try to remove the second value (that is not in collection yet)
- calls and tests the adder method with the second value
- try to add again the first value (to tetunique constraint)
- try to remove the first value
- add again the first value (to obtain a collection with the first and the second value in it)
This method uses the following three methods that you can also use separatly.
Test Adder
You can also only test the adder method like that :
This method calls the adder method and tests :
- the fluent constraint
- if the value is in collection (added)
- the unicity constraint
Test Remover
You can also only test the remover method like that :
This method calls the remover method and tests :
- the fluent constraint
- if the value is not in colleciton (removed)
- if the remover removed the good number of items (all that match value but not more)
Test Getter
You can also only test the getter method like that :
This method tests if the returned value of the getter method is :
- (if return is an object) implement
Countable
interface - (if return is an object) implement
Traversable
interface - (if return is not an object) is an
array
ornull
Tricks
To test entity quicker, use dataProvider
with tester.
-
Create the test method
- Create the data provider
You can also use the process for AccessorCollectionTester
by customize the test method.
Credits
Special thanks for gerg0ire who encourage me to do that library