Skip to content

This GitHub action sets up a PHP (composer) environment and runs install, audit, test, and build commands for your PHP project

License

Notifications You must be signed in to change notification settings

alleyinteractive/action-test-php

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Run PHP Tests Action

This GitHub Action sets up a PHP environment (optionally with WordPress via mantle-ci) and runs install, audit, test, and build commands using Composer for your PHP projects.

Usage

Example usage in a workflow:

name: PHP CI

on:
  pull_request:
    branches:
      - main
    types: [opened, synchronize, reopened, ready_for_review]

jobs:
  php-tests:
    if: github.event.pull_request.draft == false
    runs-on: ubuntu-latest
    timeout-minutes: 10

    steps:
    - uses: actions/checkout@v4

    - name: Run PHP Tests in src directory
      uses: alleyinteractive/action-test-php@develop
      with:
        php-version: '8.1'
        working-directory: './src'
        cache-dependency-path: './src/composer.lock'
        audit-skip: 'true'
        test-command: 'composer run-script test:unit'
        build-command: 'composer run-script build:prod'

Out of the box, action-test-php will attempt to install WordPress via the mantle-ci script and rsync your cloned out repository to the WordPress wp-content directory. If you are not a wp-content-based project, you should set skip-wordpress-install to 'true' and ensure your test command properly sets up the environment (install WordPress, rsync the repository, etc).

Inputs

Specify using with keyword.

php-version

  • Specify the PHP version to use.
  • Accepts a string.
  • Defaults to 'latest'.

php-tools

  • Specify the PHP tools to install.
  • Accepts a string, comma-separated.
  • Defaults to 'composer'.

php-extensions

  • Specify additional PHP extensions to install.
  • Accepts a string, comma-separated.
  • Defaults to apcu, bcmath, calendar, ctype, curl, date, dom, exif, filter, ftp, gd, gmp, gnupg, hash, iconv, igbinary, intl, json, libxml, mbstring, memcache, memcached, mysqli, mysqlnd, openssl, pcntl, pcre, pdo, pdo_mysql, pdo_sqlite, phar, posix, reflection, session, shmop, simplexml, soap, sockets, sodium, spl, sqlite3, ssh2, standard, sysvsem, sysvshm, timezonedb, tokenizer, xml, xmlreader, xmlwriter, zip, zlib.

php-coverage

  • Specify coverage support for the PHP application.
  • Accepts a valid coverage support value (xdebug, pcov, none).
  • Defaults to 'none'.

skip-services or services-skip

  • Skip starting Docker-based services (mysql, redis, memcached). If you are using Mantle Testkit, you can safely set this to 'true' if you are using SQLite, or leave it as 'false' if you are using MySQL.
  • Accepts a boolean string ('true' or 'false').
  • Defaults to 'false'.

wordpress-version

  • Specify the WordPress version to use, or 'false' to skip WordPress installation. If you specify a version, test-command will be run from within the context of the WordPress installation's wp-content directory. If you are using Mantle Testkit or are not using WordPress based tests, you should set this to false.
  • Accepts a string.
  • Defaults to 'latest'.

wordpress-multisite

  • Specify whether to enable WordPress multisite.
  • Accepts a boolean string ('true' or 'false').
  • Defaults to 'false'.

wordpress-host

  • Specify the host for the WordPress site.
  • Accepts a string. ('vip', 'pantheon', or 'false')
  • Defaults to 'vip'.

working-directory

  • Specify the directory to run the commands in.
  • Accepts a string.
  • Defaults to ..

cache-dependency-path

  • Specify the path to the dependency file to use for caching.
  • Accepts a string.
  • Defaults to 'composer.lock'.

install-skip or skip-install

  • Determine whether to skip the composer install step.
  • Accepts a boolean string ('true' or 'false').
  • Defaults to 'false'.

install-command

  • Specify the command to run for composer install.
  • Accepts a string.
  • Defaults to 'composer install --prefer-dist --no-interaction --no-progress'.

audit-skip or skip-audit

  • Determine whether to skip the composer audit step.
  • Accepts a boolean string ('true' or 'false').
  • Defaults to 'false'.

audit-command

  • Specify the command to run for composer audit.
  • Accepts a string.
  • Defaults to 'composer audit --no-dev --locked --ansi --no-interaction --ignore-platform-reqs --no-progress'.

test-skip or skip-test

  • Determine whether to skip the test step.
  • Accepts a boolean string ('true' or 'false').
  • Defaults to 'false'.

skip-wordpress-install

  • Skip the installation of WordPress and the rsync-ing of the repository to /tmp/wordpress/wp-content.

    If you are not a wp-content-based project, you should set this to 'true'. Your test command will need to properly set up the environment (install WordPress, rsync the repository, etc).

  • Accepts a boolean string ('true' or 'false').

  • Defaults to 'false'.

skip-core-test-suite

  • Skip the installation of the WordPress core test suite when installing WordPress. If skip-wordpress-install is set to 'true', this option will be ignored.
  • Accepts a boolean string ('true' or 'false').
  • Defaults to 'false'.

test-command

  • Specify the command to run for tests.
  • Accepts a string.
  • Defaults to 'composer run-script test'.

build-command

  • Specify the command to run for build. Unlike the other steps, the default action is to not run a build command.
  • Accepts a string.
  • Defaults to ''.

github-token

  • Specify the GitHub token to use for Composer authentication (eg. for private repositories).
  • Accepts a string.
  • Defaults to ''.

Examples

Usage on a Plugin/Theme

Alley's create-wordpress-plugin project that uses Mantle Testkit as a testing framework is a common example of a plugin that can use this action and roll its own WordPress installation. Mantle Testkit supports installing WordPress and rsync-ing your project to wherever you need it to be (wp-content, wp-content/plugins, etc).

name: PHP CI

on:
  pull_request:
    branches:
      - main
    types: [opened, synchronize, reopened, ready_for_review]

jobs:
  php-tests:
    if: github.event.pull_request.draft == false
    runs-on: ubuntu-latest
    timeout-minutes: 10

    steps:
    - uses: actions/checkout@v4

    - name: Run PHP Tests in src directory
      uses: alleyinteractive/action-test-php@develop
      with:
        # After installation, the action will run the test command which defaults to `composer test`.
        skip-wordpress-install: 'true'

Using a Rolling WordPress Version

You can test against a rolling number of WordPress versions by setting up the action with a matrix strategy. For example, to test against the last 3 versions of WordPress and roll it up to a single job that can be used for branch protection:

name: "All Pull Request Tests"

on:
  pull_request:
    branches:
      - develop
    types: [opened, synchronize, reopened, ready_for_review]

jobs:
  # Get the last 3 major versions of WordPress for use in the job matrix.
  find-wordpress-versions:
    runs-on: ubuntu-latest
    outputs:
      versions: ${{ steps.get-versions.outputs.versions }}
    steps:
      - name: Get WordPress Versions
        id: get-versions
        run: echo "versions=$(curl -s https://api.wordpress.org/core/version-check/1.7/ | jq -r '.offers[] | select(.response == "autoupdate").version' | head -n 3 | sort -u | sed 's/\.[^.]*$//' | jq -R -s -c 'split("\n")[:-1]')" >> $GITHUB_OUTPUT

  # We use a single job to ensure that all steps run in the same environment and
  # reduce the number of minutes used.
  run-pr-tests:
    needs: find-wordpress-versions
    # Don't run on draft PRs
    if: github.event.pull_request.draft == false
    # Timeout after 10 minutes
    timeout-minutes: 10
    # Define a matrix of PHP/WordPress versions to test against
    strategy:
      fail-fast: false
      matrix:
        php: [8.1, 8.2, 8.3]
        wordpress: ${{fromJson(needs.find-wordpress-versions.outputs.versions)}}
    runs-on: ubuntu-latest
    # Cancel any existing runs of this workflow
    concurrency:
      group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }}-P${{ matrix.php }}-WP${{ matrix.wordpress }}
      cancel-in-progress: true
    # Name the job in the matrix
    name: "PR Tests PHP ${{ matrix.php }} WordPress ${{ matrix.wordpress }}"
    steps:
      - uses: actions/checkout@v4

      - name: Run General Tests
        # See https://github.com/alleyinteractive/action-test-general for more options
        uses: alleyinteractive/action-test-general@develop

      - name: Run Node Tests
        # See https://github.com/alleyinteractive/action-test-node for more options.
        # Defaults to the latest LTS version.
        uses: alleyinteractive/action-test-node@develop

      - name: Run PHP Tests
        # See https://github.com/alleyinteractive/action-test-php for more options
        uses: alleyinteractive/action-test-php@develop
        with:
          php-version: '${{ matrix.php }}'
          wordpress-version: '${{ matrix.wordpress }}'
          skip-wordpress-install: 'true'

  # Check if any of the pr-tests failed and fail the job if so.
  pr-tests:
    needs: run-pr-tests
    runs-on: ubuntu-latest
    steps:
      - name: Check job results
        run: |
          echo "Checking matrix job results..."
          if [ "${{ needs.run-pr-tests.result }}" == "failure" ]; then
            echo "One or more matrix jobs failed."
            exit 1
          else
            echo "All matrix jobs passed."
          fi

Once added, you can use pr-tests as a required check for branch protection without having to worry about updating it when a new version of WordPress is released.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Related Projects ❤️

Credits

This project is actively maintained by Alley Interactive.

License

The GNU General Public License (GPL) license. Please see License File for more information.⏎

About

This GitHub action sets up a PHP (composer) environment and runs install, audit, test, and build commands for your PHP project

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •