Recommend this page to a friend! |
Classes of Alexandre | Doubles | doc/0_intro.md | Download |
|
DownloadIntroductionRequirementsThe "sitphp/doubles" library requires at least PhpUnit 6 and at least PHP 7. It should be installed from composer which will make sure your configuration matches requirements. > {.note .info} Note : You can get composer here : https://getcomposer.org.
InstallOnce you have composer installed, add the line
Then run the following composer command :
This will install the latest version of the "sitphp/doubles' library with the required PhpUnit package. What is a doubleA double is just a class that implements the same methods as the original class (like a copy) except that these methods can be tested and manipulated. The are two types of doubles that you should know about : doubles of type "dummy" and doubles of type "mock" : What is a "dummy" double ?A double is called a "dummy" when all the methods of the original class are overwritten to return
Here is what the double of type "dummy" would look like :
As you can see, every method of the original class is overwritten in the double to return What is a "mock" double ?A double is called a "mock" when all the methods of the original class are overwritten to behave the same as in the original class. Here is what a "mock" of our previously created "Name" class would look like :
As you can see, the double will behave exactly the same as the original class. But unlike the original class, it can be tested and manipulated so can use it instead of our original class for our test. How doubles can help you with testing ?Let's say you have two classes : If you wanted to test the methods of class
The "sitphp/doubles" library can generate doubles that look like the original classes but can be manipulated and tested (sort of a copy of a class). These doubles can then be used instead of the original classes for your test. This library can create doubles of any kind of class, interface or trait. Here are the 3 things that doubles can do for testing :
>{.note .info} Note : Doubles are often called "mocks". But in this library, the "mock" word is used to name a special kind of double. |