Skip to content

Commit

Permalink
CI: switch to GitHub Actions - step 4: PHP Unit tests
Browse files Browse the repository at this point in the history
This commit:
* Adds a GH Actions workflow for the PHPUnit CI check.
* Removes all references to that check from the `.travis.yml` configuration.

Notes:
1. Builds will run on:
    - Select pushes using branch filtering similar to before.
    - All pull requests.
    - When manually triggered.
        Note: manual triggering of builds has to be [explicitly allowed](https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/). This is not a feature which is enabled by default.
2. If a previous GH actions run for the same branch hadn't finished yet when the same branch is pushed again, the previous run will be cancelled.
    In Travis, this was an option on the "Settings" page - "Auto cancellation" -, which was turned on for most, if not all, Yoast repos. The `concurrency` configuration in the GHA script emulates the same behaviour.
3. The default ini settings used by the `setup-php` action are based on the `php.ini-production` configuration.
    This means that `error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT`, `display_errors` is set to `Off` and `zend.assertions` is set to `-1` (= do not compile).
    For the purposes of CI, especially for linting and testing code, I'd recommend running with `zend.assertions=-1, `error_reporting=-1` and `display_errors=On` to ensure **all** PHP notices are shown.
    Note: the defaults will be changed in the next major of `setup-php` to be based on the `php.ini-develop` configuration, but that may still be a while off.
    Refs:
    * shivammathur/setup-php#450
    * shivammathur/setup-php#469
4. While the `setup-php` action offers the ability to [install the PHAR file for PHPUnit](https://github.com/shivammathur/setup-php#wrench-tools-support), I've elected not to use that option as we need to do a `composer install` anyway to get the other dependencies, like WP Test Utils.
5. Composer dependency downloads will be cached for faster builds using a [predefined GH action](https://github.com/marketplace/actions/install-composer-dependencies) specifically created for this purpose.
    The alternative would be to handle the caching manually, which would add three extra steps to the script.
    Note: Caching works differently between Travis and GH Actions.
    On GH Actions, once a cache has been created, it can't be updated. It can only be replaced by a new cache with a different key.
    As the PHP version, the `composer.json` and a potential `composer.lock` hash are all part of the key used by the above mentioned action, this difference should not have a significant impact.
    Ref: https://docs.github.com/en/actions/advanced-guides/caching-dependencies-to-speed-up-workflows
6. As the Duplicate Post plugin only contains BrainMonkey based unit tests, we can (and should) use the most appropriate PHPUnit/BrainMonkey/Mockery/etc version for the PHP version the tests are being run on.
    However, as there is no committed `composer.lock` file and the `composer.json` file does not contain a `platform - php` configuration, just running a plain `composer install` will already take care of that.
    If, at some point in the future, the `composer.lock` file would be committed or a `platform - php` config would be added to the `composer.json` file, this will need revisiting.

Differences with the Travis implementation:
* In addition to the PHP versions against which the tests were previously run on Travis, the tests will now also be run against PHP 8.1.
    As PHP 8.1 has been released and the test run currently succeeds on PHP 8.1 without notices, this build will not be allowed to fail.
  • Loading branch information
jrfnl committed Jan 6, 2022
1 parent 060bc71 commit 01b316f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 37 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/test.yml
@@ -0,0 +1,49 @@
name: Test

on:
# Run on pushes to select branches and on all pull requests.
push:
branches:
- master
- trunk
- 'release/**'
- 'hotfix/[0-9]+.[0-9]+*'
- 'feature/**'
pull_request:
# Allow manually triggering the workflow.
workflow_dispatch:

# Cancels all previous workflow runs for the same branch that have not yet completed.
concurrency:
# The concurrency group contains the workflow name and the branch name.
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
unit:
runs-on: ubuntu-latest

strategy:
matrix:
php_version: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1']

name: "Unit Test: PHP ${{ matrix.php_version }}"

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
ini-values: zend.assertions=1, error_reporting=-1, display_errors=On
coverage: none

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-composer-dependencies
- name: Install Composer dependencies
uses: ramsey/composer-install@v2

- name: Run unit tests
run: composer test
37 changes: 0 additions & 37 deletions .travis.yml
Expand Up @@ -12,28 +12,9 @@ branches:
# Also build tags like 1.1.1 or 1.1 for deployment.
- /^\d+\.\d+(\.\d+)?(-\S*)?$/

cache:
directories:
- $HOME/.composer/cache

php:
- '7.0'
- '7.1'
- '7.2'
- '7.3'

env:
- PHPUNIT=1

jobs:
fast_finish: true
include:
- php: '5.6'
env: PHPUNIT=1
- php: '7.4'
env: PHPUNIT=1
- php: '8.0'
env: PHPUNIT=1
- stage: 🚀 deployment
name: "Deploy to Yoast-dist"
php: 7.2
Expand Down Expand Up @@ -66,21 +47,3 @@ jobs:
on:
repo: $TRAVIS_REPO_SLUG
all_branches: true

before_install:
- if [[ "$COVERAGE" != "1" ]]; then phpenv config-rm xdebug.ini || echo 'No xdebug config.'; fi
- travis_retry composer install

before_script:
- export -f travis_fold
- export -f travis_time_start
- export -f travis_time_finish

script:
# PHP Unit Tests
- |
if [[ "$PHPUNIT" == "1" ]]; then
travis_fold start "PHP.tests" && travis_time_start
composer test
travis_time_finish && travis_fold end "PHP.tests"
fi

0 comments on commit 01b316f

Please sign in to comment.