Skip to content

Commit 1ac8b5a

Browse files
committed
Created branch off POC. Tidied up. Added QA and unit tests setup to the plugin. Upgraded Monolog to version 3 and also upgraded other dev dependencies.
1 parent c9ba020 commit 1ac8b5a

19 files changed

+1827
-1412
lines changed

plugins/wpgraphql-logging/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# WPGraphQL Logging
22

3-
A POC plugin for logging GraphQL requests using Monolog.
3+
A WPGraphQL logging plugin that provides visibility into request lifecycle to help quickly identify and resolve bottlenecks in your headless WordPress application.
44

55
* [Join the Headless WordPress community on Discord.](https://discord.gg/headless-wordpress-836253505944813629)
66
* [Documentation](#getting-started)
@@ -15,6 +15,6 @@ TODO
1515

1616

1717
> [!CAUTION]
18-
> This plugin is currently is a POC and not production ready
18+
> This plugin is currently a POC and not production ready yet.
1919
2020
---
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
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.

plugins/wpgraphql-logging/bin/build-docker.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ print_usage_instructions() {
1414
echo "Example use:"
1515
echo " npm run docker:build"
1616
echo ""
17-
echo " WP_VERSION=6.7 PHP_VERSION=8.2 npm run docker:build -- - c"
17+
echo " WP_VERSION=6.8 PHP_VERSION=8.2 npm run docker:build -- - c"
1818
echo ""
19-
echo " WP_VERSION=6.7 PHP_VERSION=8.2 bin/build-docker.sh -- c"
19+
echo " WP_VERSION=6.8 PHP_VERSION=8.2 bin/build-docker.sh -- c"
2020
exit 1
2121
}
2222

@@ -50,7 +50,7 @@ done
5050

5151

5252
TAG=${TAG:-latest}
53-
WP_VERSION=${WP_VERSION:-6.7}
53+
WP_VERSION=${WP_VERSION:-6.8}
5454
PHP_VERSION=${PHP_VERSION:-8.2}
5555

5656
BUILD_NO_CACHE=${BUILD_NO_CACHE:-}

plugins/wpgraphql-logging/bin/install-test-env.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ setup_file_permissions() {
144144

145145
setup_plugin() {
146146
if [ "${SKIP_WP_SETUP}" = "true" ]; then
147-
echo -e "$(warning_message "Skipping wpgraphql-Logging installation...")"
147+
echo -e "$(warning_message "Skipping wpgraphql-logging installation...")"
148148
return 0
149149
fi
150150

151151
# Add this repo as a plugin to the repo
152-
if [ ! -d $WORDPRESS_ROOT_DIR/wp-content/plugins/wpgraphql-Logging ]; then
152+
if [ ! -d $WORDPRESS_ROOT_DIR/wp-content/plugins/wpgraphql-logging ]; then
153153
echo -e "$(status_message "Symlinking the plugin to the WordPress plugins directory...")"
154154

155155
cd "$ORIGINAL_PATH"

plugins/wpgraphql-logging/bin/local/run-unit-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ if [[ "$1" == "coverage" ]]; then
2424
fi
2525
fi
2626

27-
docker exec $COVERAGE_ARG $COVERAGE_OUTPUT_ARG -e SUITES=wpunit -w /var/www/html/wp-content/plugins/wpgraphql-Logging wpgraphql-logging-wordpress-1 bin/run-codeception.sh
27+
docker exec $COVERAGE_ARG $COVERAGE_OUTPUT_ARG -e SUITES=wpunit -w /var/www/html/wp-content/plugins/wpgraphql-logging wpgraphql-logging-wordpress-1 bin/run-codeception.sh

plugins/wpgraphql-logging/bin/local/setup-docker-env.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ else
114114
115115
--allow-root
116116

117+
# Install WP-GraphQL plugin via WP-CLI
118+
docker exec wpgraphql-logging-wordpress-1 wp plugin install wp-graphql --activate --allow-root
119+
117120
if [ $? -eq 0 ]; then
118121
echo "✓ WordPress installed successfully"
119122
else
@@ -124,11 +127,13 @@ fi
124127

125128
# Install and activate the plugin if needed
126129
echo "Checking plugin activation..."
127-
if docker exec wpgraphql-logging-wordpress-1 wp plugin is-active wpgraphql-Logging --allow-root 2>/dev/null; then
130+
if docker exec wpgraphql-logging-wordpress-1 wp plugin is-active wpgraphql-logging --allow-root 2>/dev/null; then
128131
echo "✓ Plugin is active"
129132
else
130133
echo "Activating plugin..."
131-
docker exec wpgraphql-logging-wordpress-1 wp plugin activate wpgraphql-Logging --allow-root
134+
# Install WP-GraphQL plugin via WP-CLI
135+
docker exec wpgraphql-logging-wordpress-1 wp plugin install wp-graphql --activate --allow-root
136+
docker exec wpgraphql-logging-wordpress-1 wp plugin activate wpgraphql-logging --allow-root
132137
fi
133138

134139
# Verify coverage driver is working
@@ -155,7 +160,7 @@ echo ""
155160
echo "🎉 Docker environment setup complete!"
156161
echo ""
157162
echo "You can now run tests with:"
158-
echo " docker exec -e COVERAGE=1 -e SUITES=wpunit -w /var/www/html/wp-content/plugins/wpgraphql-Logging wpgraphql-logging-wordpress-1 bin/run-codeception.sh"
163+
echo " docker exec -e COVERAGE=1 -e SUITES=wpunit -w /var/www/html/wp-content/plugins/wpgraphql-logging wpgraphql-logging-wordpress-1 bin/run-codeception.sh"
159164
echo ""
160165
echo "Or without coverage:"
161-
echo " docker exec -e SUITES=wpunit -w /var/www/html/wp-content/plugins/wpgraphql-Logging wpgraphql-logging-wordpress-1 bin/run-codeception.sh"
166+
echo " docker exec -e SUITES=wpunit -w /var/www/html/wp-content/plugins/wpgraphql-logging wpgraphql-logging-wordpress-1 bin/run-codeception.sh"

plugins/wpgraphql-logging/codeception.dist.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ coverage:
5151
- /vendor/*
5252
- /src/Templates/*
5353
show_only_summary: true
54-
min_coverage: 10 #TODO change
54+
min_coverage: 90
5555
modules:
5656
config:
5757
REST:
@@ -97,7 +97,9 @@ modules:
9797
adminEmail: "%WORDPRESS_ADMIN_EMAIL%"
9898
title: 'Test'
9999
plugins:
100+
- wp-graphql/wp-graphql.php
100101
- wpgraphql-logging/wpgraphql-logging.php
101102
activatePlugins:
103+
- wp-graphql/wp-graphql.php
102104
- wpgraphql-logging/wpgraphql-logging.php
103105
configFile: 'tests/_data/config.php'

0 commit comments

Comments
 (0)