WordPress plugin with full PHPUnit test coverage, designed for automated testing with Ralph.
This project demonstrates how to set up comprehensive WordPress plugin testing using:
- wp-env — Docker-based WordPress development environment
- PHPUnit — Unit and integration testing framework
- WordPress Test Library — Official WP testing utilities
- Ralph — Copilot CLI runner for iterative development
ralph-wp-testing/
├── .wp-env.json # wp-env Docker configuration
├── package.json # npm scripts for wp-env
├── ralph.sh # Main Ralph loop
├── ralph-once.sh # Single Ralph iteration
├── ralph-worktree.sh # Run Ralph in git worktree
├── ralph-stop.sh # Stop Ralph and cleanup
├── progress.txt # Ralph progress tracking
├── prompts/
│ └── wp-testing-agent.txt # Ralph prompt for this project
├── plans/
│ └── prd.json # Ralph work items (8 items)
├── sample-plugin/
│ ├── sample-plugin.php # Main plugin file
│ ├── composer.json # PHP dependencies
│ ├── phpunit.xml.dist # PHPUnit configuration
│ ├── phpcs.xml.dist # PHP CodeSniffer rules
│ ├── includes/
│ │ ├── class-book-cpt.php # Book custom post type
│ │ ├── class-genre-taxonomy.php # Genre taxonomy
│ │ ├── class-settings.php # Settings page
│ │ ├── class-rest-api.php # REST API endpoints
│ │ └── class-meta-box.php # Book details meta box
│ └── tests/
│ ├── bootstrap.php # Test bootstrapper
│ └── Unit/
│ ├── Test_Book_CPT.php
│ ├── Test_Genre_Taxonomy.php
│ ├── Test_Settings.php
│ ├── Test_REST_API.php
│ ├── Test_Meta_Box.php
│ └── Test_Activation.php
└── .github/
└── workflows/
└── phpunit.yml # CI workflow
- Docker Desktop running
- Node.js 18+ and npm
- PHP 8.1+ (for local Composer)
# Install npm dependencies (wp-env)
npm install
# Install PHP dependencies
cd sample-plugin && composer install && cd ..
# Start WordPress environment
npm run env:start
# Run tests
npm test| Command | Description |
|---|---|
npm run env:start |
Start wp-env Docker containers |
npm run env:stop |
Stop wp-env containers |
npm run env:destroy |
Remove wp-env containers and data |
npm test |
Run PHPUnit tests |
npm run test:watch |
Run tests with testdox output |
npm run lint |
Run PHP CodeSniffer |
Ralph is a Copilot CLI wrapper that iteratively implements features from a PRD (Product Requirements Document).
This project includes Ralph scripts for easy execution:
| Script | Description |
|---|---|
./ralph-worktree.sh |
Run Ralph in an isolated git worktree |
./ralph-stop.sh |
Stop Ralph and clean up worktree |
./ralph.sh |
Main Ralph loop (used by worktree script) |
./ralph-once.sh |
Single Ralph iteration |
# Run Ralph in a worktree (recommended)
./ralph-worktree.sh
# Or with custom worktree name and iterations
./ralph-worktree.sh my-feature 5
# Stop Ralph and optionally remove worktree
./ralph-stop.shThe worktree approach keeps your main branch clean while Ralph works.
# Run Ralph directly
./ralph.sh --prompt prompts/wp-testing-agent.txt \
--prd plans/prd.json \
--allow-profile dev 10The plans/prd.json contains 8 work items:
- testing-setup — Verify wp-env and PHPUnit work
- testing — Book CPT tests
- testing — Genre taxonomy tests
- testing — Settings API tests
- testing — REST API endpoint tests
- testing — Meta box tests
- testing — Activation/deactivation hook tests
- ci — GitHub Actions workflow
| Test File | Tests | Coverage |
|---|---|---|
| Test_Book_CPT.php | 6 | CPT registration, supports, CRUD |
| Test_Genre_Taxonomy.php | 7 | Taxonomy registration, terms, assignments |
| Test_Settings.php | 8 | Options, defaults, sanitization, capabilities |
| Test_REST_API.php | 11 | Endpoints, auth, CRUD, pagination |
| Test_Meta_Box.php | 9 | Nonce, capabilities, validation |
| Test_Activation.php | 8 | Hooks, options, transients |
The sample plugin includes:
- Book Custom Post Type — With title, editor, thumbnail, excerpt support
- Genre Taxonomy — Hierarchical taxonomy for categorizing books
- Settings Page — Under Settings menu with sanitization
- REST API — Custom endpoints at
/wp-json/sample-plugin/v1/books - Meta Box — Book details (author, ISBN, year, pages)
GitHub Actions runs:
- PHPUnit on PHP 8.1, 8.2, 8.3
- WordPress 6.6, 6.7, and latest
- PHP CodeSniffer for coding standards
GPL-2.0-or-later