Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test with different versions of PHP in a GitHub Action

I have some PHP code with tests which run using PHPUnit and wanted to test it on GitHub Actions. I could not find a method in their documentation for testing PHP packages. I want to test using different versions of PHP, but they only have the latest one 7.3 installed.

like image 810
joshspeck Avatar asked Sep 16 '19 23:09

joshspeck


1 Answers

You can add the setup-php action in your workflow. If not present, it installs the PHP version(s) you specify with required extensions and various tools like composer. It supports all the virtual environments supported by GitHub Actions and PHP versions >= 5.3.

For example you can have a workflow like this

jobs:
  run:    
    runs-on: ${{ matrix.operating-system }}
    strategy:
      matrix:
        operating-system: [ubuntu-latest, windows-latest, macOS-latest]
        php-versions: ['7.2', '7.3']
    name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Install PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: ${{ matrix.php-versions }}
        extensions: intl #optional
        ini-values: "post_max_size=256M" #optional
    - name: Check PHP Version
      run: php -v

Note: This will setup PHP, you need to add steps to install your dependencies using composer and another step to run your tests using PHPUnit

You can specify the required extensions in the extensions and the php.ini configuration you want in ini-values. Both these inputs are optional and take a CSV as an input. The php-version input is required. In above example it will run the workflow and setup the PHP environment with the versions specified in matrix.php-versions i.e 7.2 and 7.3, you can adjust these as per your requirements.

like image 161
Shivam Mathur Avatar answered Nov 15 '22 02:11

Shivam Mathur