From 6b1d63f86c1050172f7dee01c20eabf1c70f57cd Mon Sep 17 00:00:00 2001 From: Ruslan Kabalin Date: Sun, 10 May 2026 23:57:34 +0100 Subject: [PATCH] [docs] Add best practice on using relfection in tests. --- docs/guides/testing/index.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/guides/testing/index.md b/docs/guides/testing/index.md index 37ab89e2b..a98ff4b0f 100644 --- a/docs/guides/testing/index.md +++ b/docs/guides/testing/index.md @@ -630,6 +630,15 @@ namespace mod_myplugin; There are several best practices, suggestions, and things to avoid which you should consider when writing unit tests. Some of these are described below. +### Respect encapsulation + +As a general principle, testing should not compromise encapsulation. Most behavior should be verifiable through public methods without need of using reflection. If substantial logic exists only behind private or protected methods, this may indicate that responsibilities should be extracted into a separate class with own public API. + +- Avoid changing API visibility in core code solely to simplify testing. +- Prefer testing public APIs, as this use internal implementation details while preserving encapsulation. +- When protected functionality requires direct testing, use inheritance-based approaches. +- Using reflection may be considered as a last resort. + ### Using the magic `::class` constant {/* #using-the-magic-class-constant */} PHP supports the use of a magic `::class` constant to correctly and consistently define class names. This can be used in a range of situations, including: