Skip to content

Commit

Permalink
Setup GitHub Actions for Testing/Linting Plugin (#582)
Browse files Browse the repository at this point in the history
* Lint plugin

* Run unit tests via docker/github actions

* Use -T in docker-compose

* Use docker command instead of docker compose

* Remove interactive tty

* List files in dir

* Simplify docker from

* Fix wp-cli

* Use wp cli from run step

* Run unit test on windows

* Test docker unit tests with macos

* Remove volume from docker compose

* inspect container

* Add working-directory to inspect step

* Use entrypoint/cmd

* Sleep for 30 secs

* Link db

* Try simplifying testing env

* Use more verbose phpunit command

* Check current dir

* Composer install

* Clean up test action

* Set sleep to 15 seconds

* Fix CircleCI test

* Update documentation

* Update action names
  • Loading branch information
blakewilson authored Oct 19, 2021
1 parent 4a7f147 commit 9a42ed7
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ jobs:
- run:
name: Setup WordPress testing framework
command: |
/bin/bash tests/install-wp-tests.sh wp_database wp_user wp_pass 127.0.0.1 latest true
/bin/bash bin/install-wp-tests.sh wp_database wp_user wp_pass 127.0.0.1 latest true
working_directory: .
- run:
name: Run testing suite
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: lint
name: Lint Packages
on: pull_request
jobs:
lint:
lint_packages:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/lint-plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Lint Plugin
on: pull_request
jobs:
lint_plugin:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.3'
- name: Install Dependencies
working-directory: plugins/wpe-headless
run: composer install
- name: Run Lint
working-directory: plugins/wpe-headless
run: composer lint
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: test
name: Test Packages
on: pull_request
jobs:
test:
test_packages:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/test-plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Test Plugin
on: pull_request
jobs:
test_plugin:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Create Docker Containers
working-directory: ./plugins/wpe-headless
run: docker-compose up -d
- name: Sleep 15 seconds
run: sleep 15
- name: Setup testing framework
working-directory: ./plugins/wpe-headless
run: docker exec $(docker-compose ps -q wordpress) init-testing-environment.sh
- name: Run unit tests
working-directory: ./plugins/wpe-headless
run: docker exec -w /var/www/html/wp-content/plugins/wpe-headless $(docker-compose ps -q wordpress) composer test
29 changes: 13 additions & 16 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,29 @@ composer phpcs:fix
```

**WordPress Unit Tests**
To run WordPress unit tests, set up the test framework:

```
/bin/bash /path/to/faustjs/plugins/wpe-headless/tests/install-wp-tests.sh faustwp_tests db_name db_password
```

If you connect to MySQL via a sock connection, you can run the following.
To run WordPress unit tests, first create the Docker containers from the `plugins/wpe-headless` directory:

```
/bin/bash /path/to/faustjs/plugins/wpe-headless/tests/install-wp-tests.sh faustwp_tests db_name db_password localhost:/path/to/mysql/mysqld.sock
docker-compose up -d
```

Install the composer packages from within `wpe-headless` directory if you haven't already.
Once the containers are up, set up the test framework:

```
composer install
docker-compose exec wordpress init-testing-environment.sh
```

Within the `wpe-headless` directory, run `phpunit` either directly or as a composer command:
Run the unit tests:

```
vendor/bin/phpunit
docker-compose exec -w /var/www/html/wp-content/plugins/wpe-headless wordpress composer test
```

or
Finally, to remove the containers:

```
composer test
docker-compose down
```

## End-2-End Testing
Expand All @@ -110,13 +105,15 @@ Use [Codeception](https://codeception.com/) for running end-2-end tests in the b
### 2. Front-end Setup

1. Create the following `.env.test` in `examples/next/getting-started`.

```
# Your WordPress site URL
NEXT_PUBLIC_WORDPRESS_URL=http://localhost:8080
# Plugin secret found in WordPress Settings->Headless
WP_HEADLESS_SECRET=00000000-0000-0000-0000-000000000001
```

2. From within `examples/next/getting-started`, run `NODE_ENV=test npm run dev`.

### 3. WordPress Setup
Expand All @@ -125,9 +122,9 @@ WP_HEADLESS_SECRET=00000000-0000-0000-0000-000000000001
1. Move into the WPE Headless plugin directory `plugins/wpe-headless`.
1. Run `composer install` if you haven't already.
1. Prepare a test WordPress site.
1. Run `docker-compose up -d --build`. If building for the first time, it could take some time to download and build the images.
1. Run `docker-compose exec --workdir=/var/www/html/wp-content/plugins/wpe-headless --user=www-data wordpress wp plugin install wp-graphql --activate`
1. Run `docker-compose exec --workdir=/var/www/html/wp-content/plugins/wpe-headless --user=www-data wordpress wp db export tests/_data/dump.sql`
1. Run `docker-compose up -d --build`. If building for the first time, it could take some time to download and build the images.
1. Run `docker-compose exec --workdir=/var/www/html/wp-content/plugins/wpe-headless --user=www-data wordpress wp plugin install wp-graphql --activate`
1. Run `docker-compose exec --workdir=/var/www/html/wp-content/plugins/wpe-headless --user=www-data wordpress wp db export tests/_data/dump.sql`
1. Copy `.env.testing.example` to `.env.testing`.
1. Run `vendor/bin/codecept run acceptance` to start the end-2-end tests.

Expand Down
33 changes: 21 additions & 12 deletions plugins/wpe-headless/.docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
ARG PHP_VERSION=7.4
ARG WORDPRESS_VERSION=5.6.2

FROM wordpress:cli-php${PHP_VERSION} AS wp-cli
FROM wordpress:${WORDPRESS_VERSION}-php${PHP_VERSION}-apache
FROM wordpress:latest

# Needed for Codeception WPDB test integration.
RUN docker-php-ext-install pdo pdo_mysql

# Add wp-cli to the main container.
COPY --from=wp-cli --chown=www-data:www-data /usr/local/bin/wp /public/bin/wp-cli.phar
COPY wp-cli/wp-cli.sh /usr/local/bin/wp
# Install WP cli
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \
&& chmod +x wp-cli.phar \
&& mv wp-cli.phar /usr/local/bin/wp \
&& wp --info

RUN set -e \
; apt-get -qq update \
Expand All @@ -18,10 +16,21 @@ RUN set -e \
less \
openssh-server \
sudo \
subversion \
default-mysql-client \
telnet \
vim \
-qq --no-install-recommends \
; sudo adduser www-data sudo \
; chmod +x /public/bin/wp-cli.phar \
; chmod +x /usr/local/bin/wp \
; chown -R www-data:www-data /public /var/www \
; rm -rf /var/lib/apt/lists/*
; rm -rf /var/lib/apt/lists/*

# Copy the script to create the testing environment
COPY scripts/init-testing-environment.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/init-testing-environment.sh

# Install composer
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
RUN chmod +x /usr/local/bin/composer

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["apache2-foreground"]
12 changes: 12 additions & 0 deletions plugins/wpe-headless/.docker/scripts/init-testing-environment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

cd /var/www/html/wp-content/plugins/$WP_PLUGIN_FOLDER

# Setup WordPress test core files and database
bash -c "./bin/install-wp-tests.sh $WP_TESTS_DB_NAME $WORDPRESS_DB_USER $WORDPRESS_DB_PASSWORD $WORDPRESS_DB_HOST latest"

# Install composer deps
composer install

# Back to the root WP folder
cd /var/www/html/
42 changes: 34 additions & 8 deletions ...ns/wpe-headless/tests/install-wp-tests.sh → plugins/wpe-headless/bin/install-wp-tests.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SKIP_DB_CREATE=${6-false}
TMPDIR=${TMPDIR-/tmp}
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress/}
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress}

download() {
if [ `which curl` ]; then
Expand Down Expand Up @@ -62,10 +62,10 @@ install_wp() {
mkdir -p $WP_CORE_DIR

if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
mkdir -p $TMPDIR/wordpress-nightly
download https://wordpress.org/nightly-builds/wordpress-latest.zip $TMPDIR/wordpress-nightly/wordpress-nightly.zip
unzip -q $TMPDIR/wordpress-nightly/wordpress-nightly.zip -d $TMPDIR/wordpress-nightly/
mv $TMPDIR/wordpress-nightly/wordpress/* $WP_CORE_DIR
mkdir -p $TMPDIR/wordpress-trunk
rm -rf $TMPDIR/wordpress-trunk/*
svn export --quiet https://core.svn.wordpress.org/trunk $TMPDIR/wordpress-trunk/wordpress
mv $TMPDIR/wordpress-trunk/wordpress/* $WP_CORE_DIR
else
if [ $WP_VERSION == 'latest' ]; then
local ARCHIVE_NAME='latest'
Expand Down Expand Up @@ -107,15 +107,17 @@ install_test_suite() {
if [ ! -d $WP_TESTS_DIR ]; then
# set up testing suite
mkdir -p $WP_TESTS_DIR
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes --ignore-externals
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data --ignore-externals
rm -rf $WP_TESTS_DIR/{includes,data}
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
fi

if [ ! -f wp-tests-config.php ]; then
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
# remove all forward slashes in the end
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s:__DIR__ . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
Expand All @@ -124,6 +126,23 @@ install_test_suite() {

}

recreate_db() {
shopt -s nocasematch
if [[ $1 =~ ^(y|yes)$ ]]
then
mysqladmin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA
create_db
echo "Recreated the database ($DB_NAME)."
else
echo "Leaving the existing database ($DB_NAME) in place."
fi
shopt -u nocasematch
}

create_db() {
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
}

install_db() {

if [ ${SKIP_DB_CREATE} = "true" ]; then
Expand All @@ -147,7 +166,14 @@ install_db() {
fi

# create database
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
if [ $(mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep ^$DB_NAME$) ]
then
echo "Reinstalling will delete the existing test database ($DB_NAME)"
read -p 'Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB
recreate_db $DELETE_EXISTING_DB
else
create_db
fi
}

install_wp
Expand Down
6 changes: 5 additions & 1 deletion plugins/wpe-headless/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3'
version: '3.6'

services:
wordpress:
Expand All @@ -7,11 +7,15 @@ services:
restart: always
ports:
- 8080:80
links:
- db
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: wordpress
WP_TESTS_DB_NAME: wordpress_unit_test
WP_PLUGIN_FOLDER: wpe-headless
volumes:
- wordpress:/var/www/html
- ./:/var/www/html/wp-content/plugins/wpe-headless
Expand Down
2 changes: 1 addition & 1 deletion plugins/wpe-headless/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
>
<testsuites>
<testsuite name="integration">
<directory prefix="test-" suffix=".php">./tests/integration/</directory>
Expand Down

0 comments on commit 9a42ed7

Please sign in to comment.