manifest / unclassified / unit-testing-workshop
Unit Testing Sample
Short and basic introduction to unit testing
★ 1stars
Install
This is the repository archive. It installs, but the folder name will carry the branch suffix and updates will not flow.
Install straight from GitHub with WP-CLI:
wp plugin install https://github.com/stracker-phil/unit-testing-workshop/archive/refs/heads/main.zipNo built-in updater. New versions won't appear under Dashboard → Updates. Install new releases yourself, or let Git Updater track the repository.
We can either write working code, or clean code, but not both at the same time.
Testing allows us to make things work, and ensure it stays working as we make it clean.
-- Summary of TDD philosophy by Kent Beck
Sample Code Structure
workshop-plugin/
├── plugin.php # Plugin header only
├── src/
│ ├── BookingValidator.php # Validates booking data
│ └── BookingProcessor.php # Processes validated bookings
├── tests/
│ ├── phpunit.xml
│ └── (we'll create these together)
├── composer.json
├── CHEATSHEET.md # Quick reference (commands, concepts)
└── README.md # This file
Workshop Agenda
Preparation
- Launch DDEV and verify the site works (before the workshop)
ddev start && ddev orchestrate -f
- Ensure the website works
- Share code-with-me access in Slack
Setup Phase
- Review the sample code we'll test
- Install PHPUnit
composer require --dev phpunit/phpunit:~9.6.0
- Setup & confirm it works:
- Create (copy) the
phpunit.xmlconfig file ddev exec vendor/bin/phpunit
- Create (copy) the
Manual Testing Basics
- Write and run first test
tests/BookingValidatorTest.phpddev exec vendor/bin/phpunit
- Add composer script
"test": "vendor/bin/phpunit"
- 💬 What makes a good unit test?
- Isolation (mock everything except the tested class)
- Stubs vs Mocks (when to use each)
- One assertion per test (or use dataProviders)
- Descriptive test names (i.e. documentation)
- Avoid DRY/abstractions (linear and transparent behavior)
Code Coverage
- 💬 Who is using coverage reports already?
- Install coverage tool
composer require --dev phpmetrics/phpmetrics:^2.9
- Configure coverage in
phpunit.xml - Generate coverage report
ddev exec vendor/bin/phpunit --coverage-html coverage/- Add composer script for convenience
- Open
coverage/index.htmlin browser - Identify untested code paths
- Write one more test to improve coverage
AI-Assisted Testing
- Use Claude to generate remaining tests, using MCP
- 💬 Review AI-generated code quality together
- Focus on: Which tests are useless, what can we delete?
- Iterate: Ask Claude to refactor/improve the tests
- Run full suite → confirm high coverage
Discussion
💬 / review the Claude project at the bottom
Key Takeaways
Three-Step Workflow:
- Manual - Establish pattern (1-2 tests)
- AI Generate - Bulk structure (10-15 tests in seconds)
- Human Refine - Delete trivia, consolidate, focus on behavior
Test coverage reveals risk areas
Claude Project
https://claude.ai/project/01987bbe-7203-7080-a49b-4517ec5a1440
Has a clear understanding of good testing philosophy, and includes capabilities for
- Interactive coach ("What is a good unit test?")
- Create uniform test suites for uploaded/copy-pasted code
- Identify testing gaps (provide test file + covered class)
- Refactor/improve existing tests ("Review and improve the following test suite...")
- TDD without MCP (AI writes test in chat, you copy-paste tests and write code)
- TDD with MCP ("Let's start a TDD session", AI writes test files, you write code)