|
| 1 | +# assertEqualHTML examples |
| 2 | + |
| 3 | +Companion plugin for the article **"Stop hardcoding HTML strings in your WordPress PHPUnit tests"** on the WordPress Developer Blog. |
| 4 | + |
| 5 | +Contains the four plugin functions from the article and their PHPUnit tests, all using `assertEqualHTML()` from `WP_UnitTestCase`. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +- PHP 8.1+ |
| 10 | +- Node.js 20+ and npm |
| 11 | +- Docker (for `wp-env`) |
| 12 | +- Composer |
| 13 | + |
| 14 | +## Quick start |
| 15 | + |
| 16 | +```bash |
| 17 | +# 1. Install dependencies (composer vendor/ is mounted inside the Docker container) |
| 18 | +npm install |
| 19 | +composer install |
| 20 | + |
| 21 | +# 2. Start the WordPress test environment (requires Docker) |
| 22 | +npm run env:start |
| 23 | + |
| 24 | +# 3. Run all tests |
| 25 | +npm run test:php |
| 26 | +``` |
| 27 | + |
| 28 | +On first run `wp-env` pulls the WordPress Docker image (~300 MB). Subsequent runs start instantly. |
| 29 | + |
| 30 | +## What's being tested |
| 31 | + |
| 32 | +| Test file | Function | Article section | |
| 33 | +|---|---|---| |
| 34 | +| `LazyLoadImagesTest.php` | `my_plugin_lazy_load_images()` | Basic usage | |
| 35 | +| `MarkExternalLinksTest.php` | `my_plugin_mark_external_links()` | HTML API transformations | |
| 36 | +| `RenderCardBlockTest.php` | `my_plugin_render_card_block()` | Block render callbacks | |
| 37 | +| `ListInteractivityTest.php` | `my_plugin_add_list_interactivity()` | Interactivity API directives | |
| 38 | + |
| 39 | +## The fragile test demo |
| 40 | + |
| 41 | +`class-test-lazy-load-images.php` includes `test_assertsame_is_fragile()`, which is skipped by default. Remove the `markTestSkipped()` call to see how `assertSame` fails when attribute order doesn't match — then note how `test_adds_loading_attribute()` passes with the same inputs. |
| 42 | + |
| 43 | +## Project structure |
| 44 | + |
| 45 | +``` |
| 46 | +assert-equal-html-examples/ |
| 47 | +├── assert-equal-html-examples.php # Plugin entry point (hooks) |
| 48 | +├── includes/ |
| 49 | +│ └── functions.php # Plugin functions (no hooks — testable) |
| 50 | +├── tests/ |
| 51 | +│ ├── bootstrap.php # PHPUnit bootstrap |
| 52 | +│ ├── LazyLoadImagesTest.php |
| 53 | +│ ├── MarkExternalLinksTest.php |
| 54 | +│ ├── RenderCardBlockTest.php |
| 55 | +│ └── ListInteractivityTest.php |
| 56 | +├── phpunit.xml.dist |
| 57 | +├── composer.json |
| 58 | +├── package.json |
| 59 | +└── .wp-env.json |
| 60 | +``` |
| 61 | + |
| 62 | +## Running a single test class |
| 63 | + |
| 64 | +```bash |
| 65 | +npm run test:php:filter LazyLoadImagesTest |
| 66 | +npm run test:php:filter MarkExternalLinksTest |
| 67 | +npm run test:php:filter RenderCardBlockTest |
| 68 | +npm run test:php:filter ListInteractivityTest |
| 69 | +``` |
| 70 | + |
| 71 | +## Notes |
| 72 | + |
| 73 | +- `assertEqualHTML()` requires WordPress 6.9 or later. |
| 74 | +- The `tests/bootstrap.php` expects `WP_TESTS_DIR` to point to the WordPress test library. `wp-env` sets this automatically inside the Docker container. |
| 75 | +- All plugin functions live in `includes/functions.php` (no `add_filter` calls) so tests can call them directly without side-effects. |
0 commit comments