Skip to content

Commit df1e62d

Browse files
committed
Add initial plugin structure for assertEqualHTML examples
- Created .gitignore to exclude vendor, node_modules, and PHPUnit cache files. - Added .wp-env.json for WordPress environment configuration. - Implemented main plugin file (assert-equal-html-examples.php) with hooks for content filters. - Created composer.json and composer.lock for dependency management. - Added package.json and package-lock.json for Node.js dependencies. - Included PHPUnit configuration (phpunit.xml.dist) and bootstrap file for tests. - Developed functions in includes/functions.php for lazy loading images and marking external links. - Created tests for each function to ensure expected behavior. - Added README.md with setup instructions and project structure overview.
0 parents  commit df1e62d

15 files changed

Lines changed: 8179 additions & 0 deletions

‎.gitignore‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vendor/
2+
node_modules/
3+
.phpunit.cache/
4+
.phpunit.result.cache
5+
.wp-env.override.json

‎.wp-env.json‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "https://schemas.wp.org/trunk/wp-env.json",
3+
"core": "https://wordpress.org/wordpress-6.9.zip",
4+
"plugins": ["."],
5+
"phpVersion": "8.2",
6+
"env": {
7+
"tests": {
8+
"phpVersion": "8.2"
9+
}
10+
},
11+
"port": 3333
12+
}

‎README.md‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.

‎assert-equal-html-examples.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Plugin Name: assertEqualHTML examples
4+
* Description: Companion plugin for the "Stop hardcoding HTML strings in your WordPress PHPUnit tests" article.
5+
* Version: 1.0.0
6+
* Requires at least: 6.9
7+
* Requires PHP: 8.1
8+
*/
9+
10+
declare( strict_types=1 );
11+
12+
require_once __DIR__ . '/includes/functions.php';
13+
14+
add_filter( 'the_content', 'my_plugin_lazy_load_images' );
15+
add_filter( 'the_content', 'my_plugin_mark_external_links' );
16+
add_filter( 'render_block', 'my_plugin_add_list_interactivity', 10, 2 );

‎composer.json‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "juanmaguitar/assert-equal-html-examples",
3+
"description": "Companion plugin for the assertEqualHTML developer blog article.",
4+
"type": "wordpress-plugin",
5+
"license": "GPL-2.0-or-later",
6+
"require": {
7+
"php": ">=8.1"
8+
},
9+
"require-dev": {
10+
"phpunit/phpunit": "^9.6",
11+
"doctrine/instantiator": "^1.5",
12+
"yoast/phpunit-polyfills": "^2.0",
13+
"wp-coding-standards/wpcs": "^3.3",
14+
"dealerdirect/phpcodesniffer-composer-installer": "^1.2",
15+
"wp-phpunit/wp-phpunit": "^6.9"
16+
},
17+
"scripts": {
18+
"test": "phpunit",
19+
"install-wp-tests": "bash bin/install-wp-tests.sh wordpress_test root '' localhost latest"
20+
},
21+
"config": {
22+
"allow-plugins": {
23+
"dealerdirect/phpcodesniffer-composer-installer": true
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)