Skip to content

Commit 9483952

Browse files
authored
Add a PHPUnit Docker Container (#4279)
This allows a Gutenberg dev environment to be created by running `bin/setup-local-env.sh`. Some scripts that previously assumed they were installed in a local WordPress install have also been updated to make use of the WordPress container.
1 parent f0d2e4b commit 9483952

File tree

9 files changed

+62
-21
lines changed

9 files changed

+62
-21
lines changed

.travis.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,13 @@ before_install:
2222

2323
jobs:
2424
include:
25-
- stage: test
26-
php: 5.6
27-
script: composer install && ./vendor/bin/phpcs
28-
2925
- stage: test
3026
script:
3127
- npm install || exit 1
3228
- npm run ci || exit 1
3329

3430
- stage: test
35-
php: 7.1
36-
env: WP_VERSION=latest
31+
env: WP_VERSION=latest DOCKER=true
3732
script:
3833
- ./bin/run-wp-unit-tests.sh
3934

bin/install-wp-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ install_db() {
119119
fi
120120

121121
# create database
122-
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
122+
mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute "CREATE DATABASE IF NOT EXISTS $DB_NAME;"
123123
}
124124

125125
install_wp

bin/run-wp-unit-tests.sh

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
cd "$(dirname "$0")/../"
44

55
export PATH="$HOME/.composer/vendor/bin:$PATH"
6-
bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
7-
source bin/install-php-phpunit.sh
6+
if [ ${DOCKER} = "true" ]; then
7+
bin/setup-local-env.sh
8+
else
9+
bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
10+
source bin/install-php-phpunit.sh
11+
fi
812
# Run the build because otherwise there will be a bunch of warnings about
913
# failed `stat` calls from `filemtime()`.
14+
composer install || exit 1
1015
npm install || exit 1
1116
npm run build || exit 1
17+
1218
# Make sure phpegjs parser is up to date
1319
node bin/create-php-parser.js || exit 1
1420
if ! git diff --quiet --exit-code lib/parser.php; then
@@ -18,11 +24,24 @@ if ! git diff --quiet --exit-code lib/parser.php; then
1824
sleep .2 # Otherwise Travis doesn't want to print the whole message
1925
exit 1
2026
fi
27+
2128
echo Running with the following versions:
22-
php -v
23-
phpunit --version
29+
if [ ${DOCKER} = "true" ]; then
30+
docker-compose -f docker/docker-compose.yml run --rm wordpress_phpunit php -v
31+
docker-compose -f docker/docker-compose.yml run --rm wordpress_phpunit phpunit --version
32+
else
33+
php -v
34+
phpunit --version
35+
fi
36+
2437
# Check parser syntax
2538
php lib/parser.php || exit 1
39+
2640
# Run PHPUnit tests
27-
phpunit || exit 1
28-
WP_MULTISITE=1 phpunit || exit 1
41+
if [ ${DOCKER} = "true" ]; then
42+
npm run test-php || exit 1
43+
npm run test-unit-php-multisite || exit 1
44+
else
45+
phpunit || exit 1
46+
WP_MULTISITE=1 phpunit || exit 1
47+
fi

bin/setup-local-env.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set -e
66
# Change to the expected directory
77
cd "$(dirname "$0")/../docker"
88

9-
# Launch the WordPress docker
9+
# Launch the containers
1010
docker-compose up -d
1111

1212
# Wait until the docker containers are setup properely
@@ -21,3 +21,6 @@ docker run -it --rm --volumes-from wordpress-dev --network container:wordpress-d
2121

2222
# Activate Gutenberg
2323
docker run -it --rm --volumes-from wordpress-dev --network container:wordpress-dev wordpress:cli plugin activate gutenberg
24+
25+
# Install the PHPUnit test scaffolding
26+
docker-compose run --rm wordpress_phpunit /app/bin/install-wp-tests.sh wordpress_test root example mysql latest false

docker/docker-compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ services:
88
- 8888:80
99
environment:
1010
WORDPRESS_DB_PASSWORD: example
11+
ABSPATH: /usr/src/wordpress/
1112
volumes:
1213
- ../:/var/www/html/wp-content/plugins/gutenberg
1314
container_name: wordpress-dev
@@ -16,3 +17,15 @@ services:
1617
image: mysql:5.7
1718
environment:
1819
MYSQL_ROOT_PASSWORD: example
20+
MYSQL_DATABASE: wordpress_test
21+
22+
wordpress_phpunit:
23+
image: chriszarate/wordpress-phpunit
24+
environment:
25+
PHPUNIT_DB_HOST: mysql
26+
volumes:
27+
- ..:/app
28+
- testsuite:/tmp
29+
30+
volumes:
31+
testsuite: {}

docs/testing-overview.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,15 @@ cypress_base_url=http://my-custom-basee-url cypress_username=myusername cypress_
179179

180180
## PHP Testing
181181

182-
Tests for PHP use [PHPUnit](https://phpunit.de/) as the testing framework. Before starting, you should install PHPUnit and have a copy of [WordPress Develop](https://github.com/WordPress/wordpress-develop) available and setup a [`wp-tests-config.php`](https://make.wordpress.org/core/handbook/testing/automated-testing/phpunit/#setup) file. If the Gutenberg plugin is installed in the context of a WordPress Develop site, you can run `phpunit` directly from the command-line. Otherwise, you will need to specify the path to WordPress Develop's test directory as an environment variable:
182+
Tests for PHP use [PHPUnit](https://phpunit.de/) as the testing framework. If you're using the built-in [local environment](https://github.com/WordPress/gutenberg/blob/master/CONTRIBUTING.md#local-environment), you can run the PHP tests locally using this command:
183183

184-
```
185-
WP_TESTS_DIR=/path/to/wordpress-develop/tests/phpunit phpunit
184+
```bash
185+
npm run test-php
186186
```
187187

188-
Code style in PHP is enforced using [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer). It is recommended that you install PHP_CodeSniffer and the [WordPress Coding Standards for PHP_CodeSniffer](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards#installation) ruleset using [Composer](https://getcomposer.org/). With Composer installed, run `composer install` from the project directory to install dependencies, then `composer run-script lint` to verify PHP code standards.
188+
Code style in PHP is enforced using [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer). It is recommended that you install PHP_CodeSniffer and the [WordPress Coding Standards for PHP_CodeSniffer](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards#installation) ruleset using [Composer](https://getcomposer.org/). With Composer installed, run `composer install` from the project directory to install dependencies. The above `npm run test-php` will execute both unit tests and code linting. Code linting can be verified independently by running `npm run lint-php`.
189189

190+
To run unit tests only, without the linter, use `npm run test-unit-php` instead.
190191

191192
[snapshot testing]: https://facebook.github.io/jest/docs/en/snapshot-testing.html
192193
[update snapshots]: https://facebook.github.io/jest/docs/en/snapshot-testing.html#updating-snapshots

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,20 @@
139139
"build": "cross-env BABEL_ENV=default NODE_ENV=production webpack",
140140
"gettext-strings": "cross-env BABEL_ENV=gettext webpack",
141141
"lint": "eslint -f json . | eslines",
142+
"lint-php": "vendor/bin/phpcs",
142143
"predev": "check-node-version --package",
143144
"dev": "cross-env BABEL_ENV=default webpack --watch",
144145
"test": "npm run lint && npm run test-unit",
146+
"test-php": "npm run lint-php && npm run test-unit-php",
145147
"ci": "concurrently \"npm run lint && npm run build\" \"npm run test-unit:coverage-ci\"",
146148
"fixtures:clean": "rimraf \"blocks/test/fixtures/*.+(json|serialized.html)\"",
147-
"fixtures:server-registered": "./bin/get-server-blocks.php > blocks/test/server-registered.json",
149+
"fixtures:server-registered": "docker-compose -f docker/docker-compose.yml run -w /var/www/html/wp-content/plugins/gutenberg --rm wordpress ./bin/get-server-blocks.php > blocks/test/server-registered.json",
148150
"fixtures:generate": "npm run fixtures:server-registered && cross-env GENERATE_MISSING_FIXTURES=y npm run test-unit",
149151
"fixtures:regenerate": "npm run fixtures:clean && npm run fixtures:generate",
150152
"package-plugin": "./bin/build-plugin-zip.sh",
151153
"test-unit": "jest",
154+
"test-unit-php": "docker-compose -f docker/docker-compose.yml run --rm wordpress_phpunit phpunit",
155+
"test-unit-php-multisite": "docker-compose -f docker/docker-compose.yml run -e WP_MULTISITE=1 --rm wordpress_phpunit phpunit",
152156
"test-unit:coverage": "jest --coverage",
153157
"test-unit:coverage-ci": "jest --coverage --maxWorkers 1 && codecov",
154158
"test-unit:watch": "jest --watch",

phpunit/class-parsing-test.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ function parsing_test_filenames() {
1313

1414
require_once dirname( dirname( __FILE__ ) ) . '/lib/parser.php';
1515

16-
$fixture_filenames = glob( self::$fixtures_dir . '/*.{json,html}', GLOB_BRACE );
16+
$fixture_filenames = array_merge(
17+
glob( self::$fixtures_dir . '/*.json' ),
18+
glob( self::$fixtures_dir . '/*.html' )
19+
);
1720
$fixture_filenames = array_values( array_unique( array_map(
1821
array( $this, 'clean_fixture_filename' ),
1922
$fixture_filenames

phpunit/class-serializing-test.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ function serializing_test_filenames() {
1313

1414
require_once dirname( dirname( __FILE__ ) ) . '/lib/parser.php';
1515

16-
$fixture_filenames = glob( self::$fixtures_dir . '/*.{json,html}', GLOB_BRACE );
16+
$fixture_filenames = array_merge(
17+
glob( self::$fixtures_dir . '/*.json' ),
18+
glob( self::$fixtures_dir . '/*.html' )
19+
);
1720
$fixture_filenames = array_values( array_unique( array_map(
1821
array( $this, 'clean_fixture_filename' ),
1922
$fixture_filenames

0 commit comments

Comments
 (0)