17

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.

1 Answer 1

27

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.

5
  • 1
    Thanks, I was looking for something exactly like this.
    – joshspeck
    Sep 17, 2019 at 0:08
  • @Shivam Mathur I've used your package setup-php@v2 and specified 7.3, and then checked the version afterwards. Confirmed is running 7.3, but my composer install step directly afterwards fails and tells me I am on 7.4.10. How is this possible if the previous step php -v tells me 7.3.10?
    – simonw16
    Sep 19, 2020 at 5:55
  • 4
    @simonw16 Are you using another action to install composer dependencies. If yes, it might be a docker based action while setup-php runs on the host container. I would suggest calling the composer command directly. If not, please create an issue here specifying your workflow. github.com/shivammathur/setup-php/issues Sep 20, 2020 at 0:51
  • @simonw16 same for your test suite. For example, if you use PHPunit, call that directly as well. Sep 20, 2020 at 2:21
  • 1
    Thats exactly what the issue was! Took me hours to figure this out. I thought the actions were on the host container, but turns out they're mostly in their own. Running manually solved it. Thanks!
    – simonw16
    Sep 20, 2020 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.