Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/guides/testing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment on lines +633 to +637
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewnicols should I copy this to 5.1?

- Prefer testing public APIs, as this use internal implementation details while preserving encapsulation.
Comment thread
kabalin marked this conversation as resolved.
Comment thread
kabalin marked this conversation as resolved.
- 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:
Expand Down
Loading