Skip to content

Commit

Permalink
Merge pull request #34 from wp-graphql/feat/wp-graphql-codeception-mo…
Browse files Browse the repository at this point in the history
…dule

feat: WPGraphQL Codeception Module for E2E testing implemented and tested
  • Loading branch information
kidunot89 authored May 14, 2024
2 parents 743e920 + 1afdcf1 commit ef78ac4
Show file tree
Hide file tree
Showing 26 changed files with 1,296 additions and 110 deletions.
11 changes: 6 additions & 5 deletions .env.testing
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
TEST_SITE_DB_DSN=mysql:host=mysql;dbname=wordpress
TEST_SITE_DB_HOST=mysql
TEST_SITE_DB_DSN=mysql:host=codecept_db;dbname=wordpress
TEST_SITE_DB_HOST=codecept_db
TEST_SITE_DB_PORT=3306
TEST_SITE_DB_NAME=wordpress
TEST_SITE_DB_USER=wordpress
TEST_SITE_DB_PASSWORD=password
Expand All @@ -9,10 +10,10 @@ TEST_SITE_ADMIN_PASSWORD=password
TEST_SITE_WP_ADMIN_PATH=/wp-admin
WP_ROOT_FOLDER=/var/www/html
TEST_DB_NAME=wordpress
TEST_DB_HOST=mysql
TEST_DB_HOST=codecept_db
TEST_DB_USER=wordpress
TEST_DB_PASSWORD=password
TEST_TABLE_PREFIX=wp_
TEST_SITE_WP_URL=localhost:8080
TEST_SITE_WP_DOMAIN=localhost:8080
TEST_SITE_WP_URL=http://localhost
TEST_SITE_WP_DOMAIN=localhost
TEST_SITE_ADMIN_EMAIL=[email protected]
14 changes: 10 additions & 4 deletions .github/workflows/continous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,20 @@ jobs:
composer install
composer require codeception/module-asserts:* \
codeception/util-universalframework:* \
codeception/module-rest:* \
codeception/module-cli:* \
codeception/module-db:* \
codeception/module-filesystem:* \
codeception/module-phpbrowser:* \
codeception/module-webdriver:* \
wp-cli/wp-cli-bundle \
lucatume/wp-browser:^3.1
- name: Run Codeception Tests w/ Docker.
env:
PHP_VERSION: ${{ matrix.php }}
run: composer run-codeception -- -- --coverage --coverage-xml
run: |
composer run-codeception -- -- functional
composer run-codeception -- -- wpunit --coverage --coverage-xml
Expand All @@ -65,8 +72,7 @@ jobs:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
docker-compose run --rm \
--workdir=/var/www/html/wp-content/plugins/wp-graphql-testcase \
--user $(id -u) \
-e COVERALLS_REPO_TOKEN=$COVERALLS_REPO_TOKEN \
wordpress \
codeception_testing \
vendor/bin/php-coveralls -v
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
.vscode

# Composer files.
vendor/
vendor/*

# Local test configuration files.
codeception.yml
tests/*.suite.yml
.env.testing.local
.env.*
!.env.testing

# Patchwork cache folder.
cache/*
Expand All @@ -21,6 +22,7 @@ composer.lock
# Some local vim files
tags
*.swp
*.sql

# Release notes
.rel
Expand Down
4 changes: 3 additions & 1 deletion codeception.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ coverage:
enabled: true
include:
- src/*
exclude:
exclude:
- src/Codeception/Module/*
- src/TestCase/WPGraphQLUnitTestCase.php
- src/Logger/PHPUnitLogger.php
show_only_summary: false
Expand All @@ -26,3 +27,4 @@ extensions:
- Codeception\Command\GenerateWPXMLRPC
params:
- .env.testing
- .env.docker
26 changes: 18 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,31 @@
"src/"
]
},
"repositories": [
{
"type": "composer",
"url": "https://wpackagist.org"
}
],
"require": {
"php-extended/polyfill-php80-str-utils": "^1.3"
},
"require-dev": {
"composer/installers": "^1.9",
"johnpbloch/wordpress": "^6.1",
"wp-graphql/wp-graphql": "^1.1.8",
"squizlabs/php_codesniffer": "^3.5",
"automattic/vipwpcs": "^2.3",
"wp-coding-standards/wpcs": "^2.3",
"php-coveralls/php-coveralls": "2.4.3"
"php-coveralls/php-coveralls": "2.4.3",
"wpackagist-plugin/wp-graphql": "^1.26"
},
"scripts": {
"cli": "docker-compose run --rm --workdir=/var/www/html/wp-content/plugins/wp-graphql-testcase --user $(id -u) wordpress wait_for_it $TEST_DB -s -t 300 --",
"codeception": "codecept run wpunit --",
"run_phpunit_env": "docker-compose run --rm --workdir=/var/www/html/wp-content/plugins/wp-graphql-testcase --user $(id -u) wp_phpunit_testing wait-for-it $TEST_DB -s -t 300 --",
"run_codecept_env": "docker-compose run --rm --user $(id -u) codeception_testing wait-for-it $TEST_DB -s -t 300 --",
"codeception": "codecept run --",
"phpunit": "phpunit --",
"run-codeception": "env TEST_DB=mysql:3306 composer cli vendor/bin/codecept run wpunit",
"run-phpunit": "env TEST_DB=mysql_phpunit:3306 composer cli vendor/bin/phpunit"
"run-codeception": "env TEST_DB=codecept_db:3306 composer run_codecept_env vendor/bin/codecept run",
"run-phpunit": "env TEST_DB=phpunit_db:3306 composer run_phpunit_env vendor/bin/phpunit"
},
"extra": {
"wordpress-install-dir": "local/public",
Expand All @@ -45,13 +52,16 @@
"suggest": {
"codeception/module-asserts": "Needed for \\Tests\\WPGraphQL\\TestCase\\WPGraphQLTestcase to work.",
"codeception/util-universalframework": "Needed for \\Tests\\WPGraphQL\\TestCase\\WPGraphQLTestcase to work.",
"codeception/module-rest": "Needed for \\Tests\\WPGraphQL\\TestCase\\WPGraphQLTestcase to work.",
"lucatume/wp-browser": "Needed for \\Tests\\WPGraphQL\\TestCase\\WPGraphQLTestcase to work.",
"phpunit/phpunit": "Needed for \\Tests\\WPGraphQL\\TestCase\\WPGraphQLUnitTestcase to work.",
"wp-phpunit/wp-phpunit": "Needed for \\Tests\\WPGraphQL\\TestCase\\WPGraphQLUnitTestcase to work.",
"yoast/phpunit-polyfills": "Needed for \\Tests\\WPGraphQL\\TestCase\\WPGraphQLUnitTestcase to work."
"yoast/phpunit-polyfills": "Needed for \\Tests\\WPGraphQL\\TestCase\\WPGraphQLUnitTestcase to work.",
"guzzlehttp/guzzle": "Needed for \\Tests\\WPGraphQL\\Codeception\\Module\\WPGraphQL to work."
},
"config": {
"optimize-autoloader": true,
"process-timeout": 0,
"sort-packages": true,
"allow-plugins": {
"composer/installers": true,
"johnpbloch/wordpress-core-installer": true,
Expand Down
64 changes: 53 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,82 @@ version: '3'

services:

mysql:
codecept_db:
image: mariadb:10.2
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: password
networks:
codecept:
aliases:
- codecept_db

wordpress:
image: wp-graphql/wordpress:${WP_VERSION:-latest}
codeception_testing:
image: wp-graphql/codeception-testing:${WP_VERSION:-latest}
build:
context: ./docker
dockerfile: codeception.Dockerfile
args:
PHP_VERSION: ${PHP_VERSION:-8.0}
depends_on:
- mysql
- mysql_phpunit
- codecept_db
ports:
- "8080:80"
volumes:
- ./local/public:/var/www/html # WP core files.
- .:/var/www/html/wp-content/plugins/wp-graphql-testcase
- ./local/config/wp-config.php:/var/www/html/wp-config.php
- ./local/config/wp-tests-config.php:/var/www/html/wp-tests-config.php
- ./local/config/.htaccess:/var/www/html/.htaccess
- ./local/config/enable-app-passwords.php:/var/www/html/wp-content/mu-plugins/enable-app-passwords.php
env_file: .env.testing
environment:
WORDPRESS_DOMAIN: localhost
COMPOSER_HOME: /tmp/.composer
APACHE_RUN_USER: "#1000" # Ensure Apache can write to the filesystem.
WP_TESTS_DIR: /var/www/html/wp-content/plugins/wp-graphql-testcase/vendor/wp-phpunit/wp-phpunit
WP_PHPUNIT__TESTS_CONFIG: /var/www/html/wp-tests-config.php
networks:
codecept:
aliases:
- codeception_testing

mysql_phpunit:
phpunit_db:
image: mariadb:10.2
restart: always
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
MYSQL_DATABASE: "wordpress"
MYSQL_ROOT_PASSWORD: ""
MYSQL_ROOT_PASSWORD: ""
networks:
phpunit:
aliases:
- phpunit_db

wp_phpunit_testing:
image: wp-graphql/wp-phpunit-testing:${WP_VERSION:-latest}
build:
context: ./docker
dockerfile: wp-phpunit.Dockerfile
args:
PHP_VERSION: ${PHP_VERSION:-8.0}
depends_on:
- phpunit_db
ports:
- "8081:80"
volumes:
- ./local/public:/var/www/html # WP core files.
- .:/var/www/html/wp-content/plugins/wp-graphql-testcase
- ./local/config/wp-tests-config.php:/var/www/html/wp-tests-config.php
env_file: .env.testing
environment:
COMPOSER_HOME: /tmp/.composer
APACHE_RUN_USER: "#1000" # Ensure Apache can write to the filesystem.
WP_TESTS_DIR: /var/www/html/wp-content/plugins/wp-graphql-testcase/vendor/wp-phpunit/wp-phpunit
WP_PHPUNIT__TESTS_CONFIG: /var/www/html/wp-tests-config.php
networks:
phpunit:
aliases:
- wp_phpunit_testing

networks:
phpunit:
codecept:
54 changes: 54 additions & 0 deletions docker/codeception.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
ARG PHP_VERSION=8.1

FROM wordpress:php${PHP_VERSION}-apache

# See: https://xdebug.org/docs/compat to match the Xdebug version with the PHP version.
ARG XDEBUG_VERSION=3.3.1

RUN apt-get update; \
apt-get install -y --no-install-recommends \
# WP-CLI dependencies.
bash less default-mysql-client git \
# MailHog dependencies.
msmtp;

COPY php.ini /usr/local/etc/php/php.ini

RUN pecl install "xdebug-${XDEBUG_VERSION}"; \
docker-php-ext-enable xdebug

ENV XDEBUG_MODE=coverage

# Install PDO MySQL driver.
RUN docker-php-ext-install \
pdo_mysql

ENV WP_ROOT_FOLDER="/var/www/html"
ENV WORDPRESS_DB_HOST=${TEST_SITE_DB_HOST}
ENV WORDPRESS_DB_PORT=${TEST_SITE_DB_PORT}
ENV WORDPRESS_DB_USER=${TEST_SITE_DB_USER}
ENV WORDPRESS_DB_PASSWORD=${TEST_SITE_DB_PASSWORD}
ENV WORDPRESS_DB_NAME=${TEST_SITE_DB_NAME}
ENV PLUGINS_DIR="${WP_ROOT_FOLDER}/wp-content/plugins"
ENV PROJECT_DIR="${PLUGINS_DIR}/wp-graphql-testcase"

WORKDIR $PROJECT_DIR

# Set up Apache
RUN echo 'ServerName localhost' >> /etc/apache2/apache2.conf
RUN a2enmod rewrite

ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh /usr/local/bin/wait-for-it
RUN chmod 755 /usr/local/bin/wait-for-it

ADD https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar /usr/local/bin/wp
RUN chmod 755 /usr/local/bin/wp

# Remove exec statement from base entrypoint script.
RUN sed -i '$d' /usr/local/bin/docker-entrypoint.sh

# Set up entrypoint
COPY entrypoint.sh /usr/local/bin/app-entrypoint.sh
RUN chmod 755 /usr/local/bin/app-entrypoint.sh
ENTRYPOINT ["app-entrypoint.sh"]
CMD ["apache2-foreground"]
81 changes: 81 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash


work_dir=$(pwd)

cd "${WP_ROOT_FOLDER}" || exit

# Run WordPress docker entrypoint.
# shellcheck disable=SC1091
. docker-entrypoint.sh 'apache2'

set +u

# Ensure mysql is loaded
wait-for-it -s -t 300 "${TEST_SITE_DB_HOST}:${TEST_SITE_DB_PORT:-3306}" -- echo "Application database is operationally..."

if [ -f "${WP_ROOT_FOLDER}/wp-config.php" ]; then
echo "Deleting old wp-config.php"
rm -rf "${WP_ROOT_FOLDER}/wp-config.php"
fi

echo "Creating wp-config.php..."
wp config create \
--path="${WP_ROOT_FOLDER}" \
--dbname="${TEST_SITE_DB_NAME}" \
--dbuser="${TEST_SITE_DB_USER}" \
--dbpass="${TEST_SITE_DB_PASSWORD}" \
--dbhost="${TEST_SITE_DB_HOST}" \
--dbprefix="${TEST_TABLE_PREFIX}" \
--skip-check \
--quiet \
--allow-root

# Install WP if not yet installed
echo "Installing WordPress..."
wp core install \
--path="${WP_ROOT_FOLDER}" \
--url="${TEST_SITE_WP_URL}" \
--title='Test' \
--admin_user="${TEST_SITE_ADMIN_USERNAME}" \
--admin_password="${TEST_SITE_ADMIN_PASSWORD}" \
--admin_email="${TEST_SITE_ADMIN_EMAIL}" \
--allow-root

wp plugin activate wp-graphql --allow-root

if [ -f "${PROJECT_DIR}/tests/codeception/_data/dump.sql" ]; then
rm -rf "${PROJECT_DIR}/tests/codeception/_data/dump.sql"
fi

wp user application-password delete 1 --all --allow-root

app_user="admin"
app_password=$(wp user application-password create 1 testing --porcelain --allow-root)

echo "Creating .env.docker file..."
echo TEST_SITE_ADMIN_APP_PASSWORD="$(echo -n "${app_user}:${app_password}" | base64)" > "$PROJECT_DIR/.env.docker"
echo TEST_SITE_WP_DOMAIN="${TEST_SITE_WP_DOMAIN}" >> "$PROJECT_DIR/.env.docker"
echo TEST_SITE_WP_URL="${TEST_SITE_WP_URL}" >> "$PROJECT_DIR/.env.docker"

echo "Dumping app database..."
wp db export "${PROJECT_DIR}/tests/codeception/_data/dump.sql" \
--dbuser="${TEST_SITE_DB_USER}" \
--dbpass="${TEST_SITE_DB_PASSWORD}" \
--skip-plugins \
--skip-themes \
--allow-root

wp config set WP_SITEURL "${TEST_SITE_WP_URL}" --allow-root
wp config set WP_HOME "${TEST_SITE_WP_URL}" --allow-root

echo "Setting pretty permalinks..."
wp rewrite structure '/%year%/%monthnum%/%postname%/' --allow-root

service apache2 start

echo "Running WordPress version: $(wp core version --allow-root) at $(wp option get home --allow-root)"

cd "${work_dir}" || exit

exec "$@"
4 changes: 2 additions & 2 deletions docker/Dockerfile → docker/wp-phpunit.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ ENV XDEBUG_MODE=coverage
RUN docker-php-ext-install \
pdo_mysql

ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh /usr/local/bin/wait_for_it
RUN chmod 755 /usr/local/bin/wait_for_it
ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh /usr/local/bin/wait-for-it
RUN chmod 755 /usr/local/bin/wait-for-it
Loading

0 comments on commit ef78ac4

Please sign in to comment.