fbpx

Home / Blog

 / 
How to run PHPUnit with a database via GitHub Actions

How to run PHPUnit with a database via GitHub Actions

As of March 2020, GitHub seems to remove the default MySQL installation from its ubuntu-latest container. Now it’s required to set up the database service in order to use it.

Because we at BracketSpace are testing our WordPress plugins with PHPUnit it’s required to run them along with the database.

Let me show you how to set up the GitHub workflow file to use multiple PHP versions.

name: Test

on: push

jobs:
  phpunit:
    name: PHPUnit
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php: ['7.0', '7.1', '7.2', '7.3', '7.4']
    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: wordpress_test
        ports:
          - 3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Install Composer dependencies
      run: composer install -o --no-progress
    - name: Setup PHP
      uses: shivammathur/setup-php@v1
      with:
        php-version: ${{ matrix.php }}
        extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, mysql, mysqli, pdo_mysql, bcmath, soap, intl, gd, exif, iconv, imagick
        coverage: none
    - name: PHPUnit
      run: |
        bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:${{ job.services.mysql.ports['3306'] }} latest true
        vendor/bin/phpunit

A couple of things to keep in mind:

  • MYSQL_DATABASE env variable will setup the database on startup
  • You have to install Composer dependencies before setting the PHP version or extract the build process to a separate job. Otherwise, make sure the Composer will install or add --ignore-platform-reqs
  • You have to connect to MySQL at 127.0.0.1 instead localhost. Otherways it may try to connect via socket, which won’t be available
  • GitHub randomly assigns the external port for services, access it with ${{ job.services.mysql.ports['3306'] }}

Share This Article:

Picture of Kuba Mikita

Kuba Mikita

Share This Article:

Picture of Kuba Mikita

Kuba Mikita

Share This Article:

Latest updates, discounts and more!

Get the most recent updates on WordPress automation, fresh plugins, new features and price cuts.

Related articles

Christmas email templates are a great way to get your audience in the holiday spirit. If you’re a WordPress user, you’re in luck! With the Pretty Email plugin, you can...

BracketSpace Black Friday is here, we’re making it a celebration with two full weeks of unbeatable discounts on our top WordPress plugins. If you’ve been waiting for the right moment...

The Notification plugin just received its third major update, with a complete code refactor to enhance performance and user experience. This rewrite brings improved functionality, so if you’re a Notification...

WordPress Plugins that get the job done

With years of experience creating client websites, we bring our experience to functional WordPress plugins.

Easy to use for everyone

Regardless of experience level, you can easily use our products.

Top-notch Support included

We treat our clients seriously and focus heavily on support. Reviews backs this up!

Extensible and developer friendly

You're the same developer as we are. Our code is clean and extensible, just the way you like.