https://github.com/degraciamathieu/interface-testing
How to test an interface with phpunit
https://github.com/degraciamathieu/interface-testing
phpunit test testing
Last synced: 13 days ago
JSON representation
How to test an interface with phpunit
- Host: GitHub
- URL: https://github.com/degraciamathieu/interface-testing
- Owner: DeGraciaMathieu
- Created: 2024-07-01T18:53:48.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-01T19:55:20.000Z (about 1 year ago)
- Last Synced: 2025-06-16T21:52:45.245Z (15 days ago)
- Topics: phpunit, test, testing
- Language: PHP
- Homepage:
- Size: 11.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
## 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.

- `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.