Testable¶
A piece of code is testable when it may have related tests, that checks its behavior.
To make a code testable, the following features are useful:
Dependency injection
Programming to interface
Single responsability principle
No global state
No static methods
No New in business logic
Pure functions
Deterministic functions
No direct output
No usage of
exit()
The golden rule: If it is not possible to write a test for it without setting up the whole world first, the code is not testable.
See also Improve your PHP code testability and Manual:Writing testable PHP code.
Related : Exit, Deterministic, Single Responsability Principle (SRP), Static Method, Static Property, Business Logic, Pure Function, Direct Output, Dependency Injection, Program To Interface, Maintenability