Skip to content

[docs] Add best practice on using relfection in tests.#1590

Open
kabalin wants to merge 1 commit into
moodle:mainfrom
kabalin:reflection
Open

[docs] Add best practice on using relfection in tests.#1590
kabalin wants to merge 1 commit into
moodle:mainfrom
kabalin:reflection

Conversation

@kabalin
Copy link
Copy Markdown
Collaborator

@kabalin kabalin commented May 10, 2026

Copilot AI review requested due to automatic review settings May 10, 2026 22:59
@netlify
Copy link
Copy Markdown

netlify Bot commented May 10, 2026

Deploy Preview for moodledevdocs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 6b1d63f
🔍 Latest deploy log https://app.netlify.com/projects/moodledevdocs/deploys/6a010db70a7fe90008067ac7
😎 Deploy Preview https://deploy-preview-1590--moodledevdocs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new unit-testing best-practice section to Moodle DevDocs, aiming to discourage the use of reflection in tests and encourage designs that preserve encapsulation.

Changes:

  • Introduces a new “Respect encapsulation” subsection under “Best practice”.
  • Provides guidance on avoiding reflection/visibility changes and preferring public APIs (with suggested alternatives).

Comment on lines +635 to +638
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.
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.
Comment on lines +633 to +637
### 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants