Add test helper functions for controlling HTML ID generation#274
Conversation
vjik
commented
May 7, 2026
| Q | A |
|---|---|
| Is bugfix? | ❌ |
| New feature? | ✔️ |
| Breaks BC? | ❌ |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #274 +/- ##
===========================================
Coverage 100.00% 100.00%
- Complexity 828 831 +3
===========================================
Files 89 91 +2
Lines 2225 2233 +8
===========================================
+ Hits 2225 2233 +8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR introduces a deterministic testing mechanism for Html::generateId() by extracting the ID-generation logic into an internal IdGenerator and adding opt-in test helper functions to control timestamp seeding and counters. It also updates the PHPUnit bootstrap, documentation, and test suite to use the new approach instead of mocking hrtime().
Changes:
- Add
Yiisoft\Html\IdGeneratorand refactorHtml::generateId()to delegate to it. - Add
src/test-functions.phpwithenableSeed(),disableSeed(), andreset()helpers for deterministic ID generation in tests. - Update PHPUnit bootstrap/config, documentation, changelog, and replace the old
hrtime()mock-based test with a new dedicated test class.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/mocks/hrtime.php | Removes the old hrtime() mocking approach. |
| tests/HtmlTest.php | Drops the previous generateId test and associated mock plumbing. |
| tests/HtmlGenerateIdTest.php | Adds new focused tests covering seeded vs deterministic ID generation and reset behavior. |
| tests/bootstrap.php | Adds a PHPUnit bootstrap that loads Composer autoload + the new test helper functions. |
| src/test-functions.php | Introduces test-only helper functions to manipulate IdGenerator state. |
| src/IdGenerator.php | Adds extracted internal ID generation logic and state. |
| src/Html.php | Refactors generateId() to use IdGenerator::generate() and updates PHPDoc. |
| README.md | Links to the new “Testing ID generation” documentation. |
| phpunit.xml.dist | Switches PHPUnit bootstrap to tests/bootstrap.php. |
| docs/testing-id-generation.md | Adds documentation describing how to make ID generation deterministic in tests. |
| CHANGELOG.md | Records the new feature entry for the upcoming release. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>