PHP Classes

File: readme.md

Recommend this page to a friend!
  Classes of DeGraciaMathieu   Interface Testing   readme.md   Download  
File: readme.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: Interface Testing
Demonstrate how to test an interface with PHPUnit
Author: By
Last change:
Date: 3 months ago
Size: 948 bytes
 

Contents

Class file image Download

How to test an interface

It is difficult to test an interface directly because it contains signatures and not behaviors.

We could create as many test classes as there are implementations of an interface, but this would be time-consuming and potentially bug-inducing.

An effective solution is to separate the business logic of an interface from the implementation details.

image

  • `AbstractUserRepository` contains the test methods and various assertions, e.g. : "it_able_to_find_user_by_id"; they do not contain any implementation-specific logic.
  • `UserRepositoryInMemoryTest` and `UserRepositoryInSessionTest` contain only the instantiation logic of the implementations; they do not contain any test logic.

This separation enhances the reliability and relevance of the tests and allows behaviors to be assigned to interfaces.