PHP Classes

How to Test a PHP Interface Using PHPUnit Shown by the Package Interface Testing: Demonstrate how to test an interface with PHPUnit

Recommend this page to a friend!
  Info   View files Documentation   View files View files (11)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-07-11 (6 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 22 This week: 22All time: 11,217 This week: 4Up
Version License PHP version Categories
interface-testing 1.0The PHP License8Data types, Language, Testing, PHP 8
Description 

Author

This package can demonstrate how to test an interface with PHPUnit.

It provides example interface definitions for user repository classes.

The package also provides PHPUnit test classes to show how to test those classes.

Picture of DeGraciaMathieu
  Performance   Level  
Name: DeGraciaMathieu <contact>
Classes: 17 packages by
Country: France France
Innovation award
Innovation award
Nominee: 12x

Winner: 1x

Documentation

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.


  Files folder image Files  
File Role Description
Files folder imageapp (3 directories)
Files folder imagetests (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file readme.md Doc. Documentation

  Files folder image Files  /  app  
File Role Description
Files folder imageAdapters (2 files)
Files folder imageEntities (1 file)
Files folder imagePorts (1 file)

  Files folder image Files  /  app  /  Adapters  
File Role Description
  Plain text file UserRepositoryInMemory.php Class Class source
  Plain text file UserRepositoryInSession.php Class Class source

  Files folder image Files  /  app  /  Entities  
File Role Description
  Plain text file User.php Class Class source

  Files folder image Files  /  app  /  Ports  
File Role Description
  Plain text file UserRepository.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageUnit (3 files)

  Files folder image Files  /  tests  /  Unit  
File Role Description
  Plain text file AbstractUserRepository.php Class Class source
  Plain text file UserRepositoryInMemoryTest.php Class Class source
  Plain text file UserRepositoryInSessionTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:22
This week:22
All time:11,217
This week:4Up