Skip to content

Commit

Permalink
feat: WPGraphQL Codeception Module for E2E test implemented and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed May 14, 2024
1 parent 743e920 commit e4b9d46
Show file tree
Hide file tree
Showing 17 changed files with 1,021 additions and 42 deletions.
5 changes: 3 additions & 2 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_PORT=3306
TEST_SITE_DB_NAME=wordpress
TEST_SITE_DB_USER=wordpress
TEST_SITE_DB_PASSWORD=password
Expand All @@ -13,6 +14,6 @@ TEST_DB_HOST=mysql
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]
7 changes: 6 additions & 1 deletion .github/workflows/continous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ 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.
Expand Down
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
1 change: 1 addition & 0 deletions codeception.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ extensions:
- Codeception\Command\GenerateWPXMLRPC
params:
- .env.testing
- .env.docker
23 changes: 15 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,30 @@
"src/"
]
},
"repositories": [
{
"type": "composer",
"url": "https://wpackagist.org"
}
],
"require": {
"php-extended/polyfill-php80-str-utils": "^1.3"
"php-extended/polyfill-php80-str-utils": "^1.3",
"ivome/graphql-relay-php": "^0.7.0"
},
"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 --",
"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 --",
"phpunit": "phpunit --",
"run-codeception": "env TEST_DB=mysql:3306 composer cli vendor/bin/codecept run wpunit",
"run-codeception": "env TEST_DB=mysql:3306 composer cli vendor/bin/codecept run",
"run-phpunit": "env TEST_DB=mysql_phpunit:3306 composer cli vendor/bin/phpunit"
},
"extra": {
Expand All @@ -45,11 +52,11 @@
"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": {
"allow-plugins": {
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ services:
- .:/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:
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
Expand Down
31 changes: 29 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,32 @@ 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
WORKDIR /var/www/html

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"

# 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"]
54 changes: 54 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/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}:${DB_PORT:-3306}" -- echo "Application database is operationally..."

# 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

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

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

echo TEST_SITE_ADMIN_APP_PASSWORD="$(echo -n "${app_user}:${app_password}" | base64)" >> $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

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

cd "${work_dir}" || exit

exec "$@"
15 changes: 15 additions & 0 deletions local/config/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
7 changes: 7 additions & 0 deletions local/config/enable-app-passwords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
/**
* Plugin Name: Enable App Passwords
* Text Domain: enable-app-passwords
*/

add_filter( 'wp_is_application_passwords_available', '__return_true' );
8 changes: 5 additions & 3 deletions local/config/wp-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
define( 'DB_CHARSET', 'utf8' );
define( 'DB_COLLATE', '' );

$table_prefix = 'wptests_';
$table_prefix = 'wp_';

define( 'AUTH_KEY', 'value' );
define( 'SECURE_AUTH_KEY', 'value' );
Expand All @@ -23,6 +23,8 @@
define( 'LOGGED_IN_SALT', 'value' );
define( 'NONCE_SALT', 'value' );

define( 'ABSPATH', __DIR__ . '/' );
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' );
}

require_once ABSPATH . 'wp-settings.php';
require_once ABSPATH . 'wp-settings.php';
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<exclude>
<file>src/TestCase/WPGraphQLTestCase.php</file>
<file>src/Logger/CodeceptLogger.php</file>
<directory>src/Codeception</directory>
<directory>vendor/</directory>
<directory>local/</directory>
</exclude>
Expand Down
Loading

0 comments on commit e4b9d46

Please sign in to comment.