|
| 1 | +# Testing WPGraphQL Logging |
| 2 | + |
| 3 | +## Table of Contents |
| 4 | + |
| 5 | +- [Overview](#overview) |
| 6 | + - [Directory Structure](#directory-structure) |
| 7 | + - [Technologies](#technologies) |
| 8 | +- [Usage](#usage) |
| 9 | + - [Running Tests](#running-tests) |
| 10 | + - [GitHub Actions](#github-actions) |
| 11 | +- [Setup Tests Locally](#setup-tests-locally) |
| 12 | + - [Prerequisites](#prerequisites) |
| 13 | + - [Docker Setup](#docker-setup) |
| 14 | + - [What the Setup Script Does](#what-the-setup-script-does) |
| 15 | + - [Running Tests Locally](#running-tests-locally) |
| 16 | +- [Troubleshooting](#troubleshooting) |
| 17 | +- [Contributing](#contributing) |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## Overview |
| 22 | + |
| 23 | +HWP Previews comes with automated tests for unit, integration, and acceptance (E2E) scenarios to ensure code quality and functionality. |
| 24 | + |
| 25 | +### Directory Structure |
| 26 | + |
| 27 | +A list of related files and directories for testing: |
| 28 | + |
| 29 | +```text |
| 30 | +bin/ |
| 31 | +├── install-test-env.sh # Set up test WP environment |
| 32 | +├── run-codeception.sh # Run Codeception tests |
| 33 | +├── run-e2e.sh # Run E2E (Playwright) tests |
| 34 | +├── run-coverage.sh # Generate coverage reports |
| 35 | +└── local/ |
| 36 | + ├── setup-docker-env.sh # Setup Docker environment |
| 37 | + ├── run-unit-tests.sh # Run unit tests in Docker with Codeception |
| 38 | + ├── run-e2e-tests.sh # Run e2e tests in Docker with Playwright |
| 39 | + ├── run-qa.sh # Run php code quality checks with PHPStan, Psalm and PHPCS |
| 40 | + ├── run-wpunit.sh # Run WPUnit tests in Docker |
| 41 | + └── run-functional.sh # Run functional tests in Docker |
| 42 | +
|
| 43 | +tests/ |
| 44 | +├── _data/ # Test data (e.g. DB dumps) |
| 45 | +├── _envs/ # Environment configs |
| 46 | +├── _output/ # Test output (logs, coverage) |
| 47 | +├── _support/ # Helper classes, modules |
| 48 | +├── wpunit/ # WPUnit (WordPress-aware unit/integration) test cases |
| 49 | +├── wpunit.suite.dist.yml |
| 50 | +└── wpunit/ |
| 51 | + └── bootstrap.php # Bootstrap for WPUnit tests |
| 52 | +
|
| 53 | +.env.dist # Example environment variables for testing |
| 54 | +codeception.dist.yml # Main Codeception config |
| 55 | +``` |
| 56 | + |
| 57 | +### Technologies |
| 58 | + |
| 59 | +We use the following technologies to run our tests: |
| 60 | + |
| 61 | +- [Codeception](https://codeception.com/) - PHP testing framework |
| 62 | +- [WPBrowser](https://wpbrowser.wptestkit.dev/) - WordPress-specific testing tools |
| 63 | +- [WPUnit](https://github.com/lipemat/wp-unit) - WordPress unit testing |
| 64 | +- [Docker](https://www.docker.com/) - Containerized testing environment |
| 65 | +- [Composer](https://getcomposer.org/) - PHP dependency management |
| 66 | +- [Playwright](https://playwright.dev/) - End-to-end testing framework |
| 67 | +- [npm](https://www.npmjs.com/) - JavaScript package manager |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## Usage |
| 72 | + |
| 73 | +The plugin includes the following test suites: |
| 74 | + |
| 75 | +1. **WP Unit Tests** – Unit and Integration Tests |
| 76 | +2. **E2E Tests** – Acceptance tests using Playwright |
| 77 | + |
| 78 | +### Running Tests |
| 79 | + |
| 80 | +| Command | Description | |
| 81 | +|------------------------------------------|----------------------------------------------------------| |
| 82 | +| `composer run test:unit:coverage` | Run WPUnit (unit/integration) tests with coverage report | |
| 83 | +| `composer run test:unit:coverage-html` | Generate an HTML code coverage report | |
| 84 | +| `composer run test:e2e` | Run end-to-end (E2E) acceptance tests | |
| 85 | +| `composer run test` | Run all available test suites | |
| 86 | + |
| 87 | +### GitHub Actions |
| 88 | + |
| 89 | +Automated testing runs on every pull request via GitHub Actions for a modified plugin: |
| 90 | + |
| 91 | +| Workflow | Description | Status | |
| 92 | +|-------------------------|---------------------------------------------|--------| |
| 93 | +| **Code Quality** | Runs static analysis and linting checks | [View Workflow](../../actions/workflows/code-quality.yml) | |
| 94 | +| **E2E Tests** | Runs Playwright end-to-end acceptance tests | [View Workflow](../../actions/workflows/e2e.yml) | |
| 95 | +| **Codeception (WPUnit)** | Runs unit and integration tests | [View Workflow](../../actions/workflows/codeception.yml) | |
| 96 | + |
| 97 | + |
| 98 | +>[!IMPORTANT] |
| 99 | +> Test coverage for WP Unit Tests is **95%**. Any new code will require tests to be added in order to pass CI checks. This is set in [text](codeception.dist.yml) in the parameter `min_coverage`. |
| 100 | +
|
| 101 | +--- |
| 102 | + |
| 103 | +## Setup Tests Locally |
| 104 | + |
| 105 | +### Prerequisites |
| 106 | + |
| 107 | +- Docker and Docker Compose installed and running |
| 108 | +- Composer installed |
| 109 | +- Node.js and npm installed (for E2E tests) |
| 110 | +- Terminal/command line access |
| 111 | + |
| 112 | +### Docker Setup |
| 113 | + |
| 114 | +>[!NOTE] |
| 115 | +> You need Docker running locally before setting up tests. Alternatively, you can copy `.env.dist` to `.env` and update the database details to point to your local database. However, this will make database changes, so we recommend using the Docker setup instead. |
| 116 | +
|
| 117 | +To set up your local Docker environment, run: |
| 118 | + |
| 119 | +```shell |
| 120 | +sh bin/local/setup-docker-env.sh |
| 121 | +``` |
| 122 | + |
| 123 | +This script will automatically handle the complete Docker environment setup process. |
| 124 | + |
| 125 | +### What the Setup Script Does |
| 126 | + |
| 127 | +The setup script performs the following operations: |
| 128 | + |
| 129 | +#### 1. Environment Verification |
| 130 | +- ✅ Checks that Docker is running |
| 131 | +- ✅ Verifies required files exist |
| 132 | + |
| 133 | +#### 2. Configuration Setup |
| 134 | +- 📁 Copies `bin/local/.env.local` to `.env` |
| 135 | + - Uses local development configuration (different from `.env.dist`) |
| 136 | + - Sets appropriate database credentials and WordPress settings |
| 137 | + |
| 138 | +#### 3. Docker Container Management |
| 139 | +- 🐳 Runs `composer run docker:build` |
| 140 | + - Executes `sh bin/build-docker.sh` to create the Docker container |
| 141 | + - Builds WordPress environment with PHP 8.2 |
| 142 | +- 🚀 Runs `docker compose up -d` to start the container in detached mode |
| 143 | + - Creates container named `wpgraphql-logging-wordpress-1` |
| 144 | + - Sets up WordPress with test database |
| 145 | + |
| 146 | +#### 4. Code Coverage Setup |
| 147 | +- 🔧 Installs and configures PCOV extension (preferred for performance) |
| 148 | +- 🔄 Falls back to XDebug if PCOV installation fails |
| 149 | +- ⚙️ Configures coverage settings automatically |
| 150 | +- 🔄 Restarts container to ensure extensions are loaded |
| 151 | + |
| 152 | +#### 5. WordPress Installation |
| 153 | +- 📝 Installs WordPress if not already present |
| 154 | +- 🔌 Activates the plugin automatically |
| 155 | +- ✅ Verifies the installation is working correctly |
| 156 | + |
| 157 | +### Running Tests Locally |
| 158 | + |
| 159 | +Once setup is complete, you can run tests using Composer: |
| 160 | + |
| 161 | +<[!NOTE] |
| 162 | +< Ensure the docker container is running before executing tests. You can run `composer run docker:start` to start it. |
| 163 | + |
| 164 | +```shell |
| 165 | +# Run unit tests with coverage |
| 166 | +composer run test:unit:coverage |
| 167 | + |
| 168 | +# Run all tests |
| 169 | +composer run test |
| 170 | + |
| 171 | +# Run E2E tests |
| 172 | +composer run test:e2e |
| 173 | +``` |
| 174 | + |
| 175 | +For a full list of available test commands, see the [Usage](#usage) section above. |
| 176 | + |
| 177 | +--- |
| 178 | + |
| 179 | +## Troubleshooting |
| 180 | + |
| 181 | +### Container Issues |
| 182 | + |
| 183 | +```shell |
| 184 | +# Check container status |
| 185 | +docker ps | grep wpgraph-logging |
| 186 | + |
| 187 | +# Restart containers if needed |
| 188 | +docker compose restart |
| 189 | + |
| 190 | +# View container logs |
| 191 | +docker compose logs wpgraph-logging-wordpress-1 |
| 192 | +``` |
| 193 | + |
| 194 | +### Permission Issues |
| 195 | + |
| 196 | +```shell |
| 197 | +# Fix test output permissions |
| 198 | +docker exec wpgraphql-logging-wordpress-1 chmod 777 -R tests/_output |
| 199 | +``` |
| 200 | + |
| 201 | +### Coverage Driver Issues |
| 202 | + |
| 203 | +```shell |
| 204 | +# Check which coverage driver is available |
| 205 | +docker exec wpgraphql-logging-wordpress-1 php -m | grep -E "(pcov|xdebug)" |
| 206 | + |
| 207 | +# Re-run setup if coverage isn't working |
| 208 | +sh bin/local/setup-docker-env.sh |
| 209 | +``` |
| 210 | + |
| 211 | +### WordPress Database Issues |
| 212 | + |
| 213 | +```shell |
| 214 | +# Reinstall WordPress |
| 215 | +docker exec wpgraphql-logging-wordpress-1 wp core install \ |
| 216 | + --url=http://localhost \ |
| 217 | + --title="Test Site" \ |
| 218 | + --admin_user=admin \ |
| 219 | + --admin_password=admin \ |
| 220 | + |
| 221 | + --allow-root |
| 222 | +``` |
| 223 | + |
| 224 | +### Clean Up Environment |
| 225 | + |
| 226 | +```shell |
| 227 | +# Stop containers |
| 228 | +docker compose down |
| 229 | + |
| 230 | +# Remove containers and volumes (complete cleanup) |
| 231 | +docker compose down -v |
| 232 | +``` |
| 233 | + |
| 234 | +--- |
| 235 | + |
| 236 | +## Contributing |
| 237 | + |
| 238 | +If you feel like something is missing or you want to add tests or testing documentation, we encourage you to contribute! Please check out our [Contributing Guide](https://github.com/wpengine/hwptoolkit/blob/main/CONTRIBUTING.md) for more details. |
0 commit comments