|
| 1 | +# Testing HWP Previews |
| 2 | + |
| 3 | +This plugin uses [Codeception](https://codeception.com/) with [WPBrowser](https://wpbrowser.wptestkit.dev/) for automated testing. |
| 4 | +Tests are organized into suites for unit, integration (wpunit), functional, and acceptance testing. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## Test Suites |
| 9 | + |
| 10 | +- **unit**: Pure PHP unit tests, no WordPress loaded. |
| 11 | +- **wpunit**: Unit/integration tests with WordPress loaded. |
| 12 | +- **functional**: Simulates web requests, runs WordPress in a test environment. |
| 13 | +- **acceptance**: Browser-based tests (WPBrowser/WPWebDriver). |
| 14 | + |
| 15 | +Configuration files for each suite are in the `tests/` directory (e.g., `unit.suite.dist.yml`, `wpunit.suite.dist.yml`). |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Local Test Environment |
| 20 | + |
| 21 | +The plugin provides scripts to set up a local WordPress environment for testing, using Docker and environment variables defined in `.env.dist`. |
| 22 | + |
| 23 | +### Prerequisites |
| 24 | + |
| 25 | +- Docker (for local environment) |
| 26 | +- Composer |
| 27 | +- Node.js (for building assets, if needed) |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## Setup |
| 32 | + |
| 33 | +1. **Copy and configure environment variables:** |
| 34 | + |
| 35 | + ```bash |
| 36 | + cp .env.dist .env |
| 37 | + # Edit .env as needed for your local setup |
| 38 | + ``` |
| 39 | + |
| 40 | + |
| 41 | +2. **Set up the test WordPress environment:** |
| 42 | + |
| 43 | + ```bash |
| 44 | + bin/install-test-env.sh |
| 45 | + ``` |
| 46 | + |
| 47 | + This script will: |
| 48 | + - Create the test database (unless `SKIP_DB_CREATE=true`) |
| 49 | + - Download and install WordPress in the directory specified by `WORDPRESS_ROOT_DIR` |
| 50 | + - Symlink the plugin into the WordPress plugins directory |
| 51 | + - Activate the plugin and set up test data |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +## Running Tests |
| 56 | + |
| 57 | +### Unit Tests |
| 58 | + |
| 59 | +Run unit tests (no WordPress loaded): |
| 60 | + |
| 61 | +```bash |
| 62 | +composer run test:unit |
| 63 | +# or |
| 64 | +vendor/bin/codecept run unit |
| 65 | +``` |
| 66 | + |
| 67 | +### WPUnit (WordPress-aware Unit/Integration) Tests |
| 68 | + |
| 69 | +Run WPUnit tests (WordPress loaded): |
| 70 | + |
| 71 | +```bash |
| 72 | +composer run test:wpunit |
| 73 | +# or |
| 74 | +vendor/bin/codecept run wpunit |
| 75 | +``` |
| 76 | + |
| 77 | +### Functional Tests |
| 78 | + |
| 79 | +Run functional tests (simulate web requests): |
| 80 | + |
| 81 | +```bash |
| 82 | +composer run test:functional |
| 83 | +# or |
| 84 | +vendor/bin/codecept run functional |
| 85 | +``` |
| 86 | + |
| 87 | +### Acceptance Tests |
| 88 | + |
| 89 | +Run browser-based acceptance tests: |
| 90 | + |
| 91 | +```bash |
| 92 | +composer run test:acceptance |
| 93 | +# or |
| 94 | +vendor/bin/codecept run acceptance |
| 95 | +``` |
| 96 | + |
| 97 | +### All Tests |
| 98 | + |
| 99 | +To run all suites: |
| 100 | + |
| 101 | +```bash |
| 102 | +composer run test |
| 103 | +# or |
| 104 | +vendor/bin/codecept run |
| 105 | +``` |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +## Code Coverage |
| 110 | + |
| 111 | +To generate code coverage reports (requires Xdebug or PCOV): |
| 112 | + |
| 113 | +```bash |
| 114 | +# Example for wpunit suite |
| 115 | +SUITES=wpunit COVERAGE=1 bin/run-codeception.sh |
| 116 | +``` |
| 117 | + |
| 118 | +Coverage output will be in `tests/_output/` or as specified by `COVERAGE_OUTPUT`. |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +## Useful Scripts |
| 123 | + |
| 124 | +- `bin/install-test-env.sh` — Sets up the WordPress test environment. |
| 125 | +- `bin/run-codeception.sh` — Runs Codeception tests inside the plugin directory. |
| 126 | +- `bin/local/run-unit-tests.sh` — Runs unit tests in Docker. |
| 127 | +- `bin/local/run-qa.sh` — Runs code quality checks (PHPStan, PHPCS, Psalm). |
| 128 | + |
| 129 | +--- |
| 130 | + |
| 131 | +## Notes |
| 132 | + |
| 133 | +- The test database will be reset during setup. **Do not use a database with important data.** |
| 134 | +- You can customize which suites to run by setting the `SUITES` environment variable. |
| 135 | +- See `.env.dist` for all available environment variables and their descriptions. |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +```text |
| 140 | +tests/ |
| 141 | +├── _data/ # Test data (e.g. DB dumps) |
| 142 | +├── _envs/ # Environment configs |
| 143 | +├── _output/ # Test output (logs, coverage) |
| 144 | +├── _support/ # Helper classes, modules |
| 145 | +├── acceptance/ # Acceptance test cases |
| 146 | +├── functional/ # Functional test cases |
| 147 | +├── unit/ # Unit test cases |
| 148 | +├── wpunit/ # WPUnit (WordPress-aware unit/integration) test cases |
| 149 | +├── acceptance.suite.dist.yml |
| 150 | +├── functional.suite.dist.yml |
| 151 | +├── unit.suite.dist.yml |
| 152 | +├── wpunit.suite.dist.yml |
| 153 | +└── wpunit/ |
| 154 | + └── bootstrap.php # Bootstrap for WPUnit tests |
| 155 | +
|
| 156 | +bin/ |
| 157 | +├── install-test-env.sh # Script to set up test WP environment |
| 158 | +├── run-codeception.sh # Script to run Codeception tests |
| 159 | +└── local/ |
| 160 | + ├── run-unit-tests.sh # Run unit tests in Docker |
| 161 | + └── run-qa.sh # Run code quality checks |
| 162 | +
|
| 163 | +.env.dist # Example environment variables for testing |
| 164 | +codeception.dist.yml # Main Codeception config |
| 165 | +``` |
0 commit comments