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   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-07-11 (2 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 30 This week: 1All time: 11,118 This week: 38Up
Version License PHP version Categories
interface-testing 1.0.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.

Innovation Award
PHP Programming Innovation award winner
July 2024
Winner
PHP provides interfaces to help developers define multiple classes that have part of the functionality in common.

Testing classes are good practices experienced developers apply to ensure their classes' code quality.

Testing interfaces is a challenge because interfaces may not define functionality.

This package shows how to solve the challenge of testing PHP interfaces of real classes.

Manuel Lemos
Picture of DeGraciaMathieu
  Performance   Level  
Name: DeGraciaMathieu <contact>
Classes: 21 packages by
Country: France France
Age: ???
All time rank: 289780 in France France
Week rank: 4 Up1 in France France Up
Innovation award
Innovation award
Nominee: 15x

Winner: 2x

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 (11)  
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 (11)  /  app  
File Role Description
Files folder imageAdapters (2 files)
Files folder imageEntities (1 file)
Files folder imagePorts (1 file)

  Files folder image Files (11)  /  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 (11)  /  app  /  Entities  
File Role Description
  Plain text file User.php Class Class source

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

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

  Files folder image Files (11)  /  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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:30
This week:1
All time:11,118
This week:38Up