MauroBaptista.com

Setting Github Actions with Pest

PHP Laravel Pest Test

Expected Result

This Github Action is used in https://apinotready.com


Successful Github Action set


Whole Github Action

If you don't want to go further, and just want the action itself. Feel free :)

1name: ApiNotReady.com
2 
3on: [push, pull_request]
4 
5jobs:
6 unit-tests:
7 runs-on: ubuntu-latest
8 steps:
9 - name: Checkout
10 uses: actions/checkout@v2
11 
12 - name: Setup PHP with Xdebug
13 uses: shivammathur/setup-php@v2
14 with:
15 php-version: '7.4'
16 coverage: xdebug
17 
18 - name: Copy .env
19 run: php -r "file_exists('.env') || copy('.env.example', '.env');"
20 
21 - name: Cache dependencies
22 uses: actions/cache@v2
23 with:
24 path: ~/.composer/cache/files
25 key: dependencies-composer-${{ hashFiles('composer.json') }}
26 
27 - name: Install Dependencies
28 run: composer install -q --no-ansi --no-interaction --no-progress --prefer-dist --optimize-autoloader
29 
30 - name: Generate key
31 run: php artisan key:generate
32 
33 - name: Clear Config
34 run: php artisan config:clear
35 
36 - name: Directory Permissions
37 run: chmod -R 777 storage bootstrap/cache
38 
39 - name: Execute tests
40 run: vendor/bin/pest --coverage --min=90

Default from Github

The default action to Laravel from Github only comes with the uses: actions/checkout@v2, but, as we are going to use the coverage from Pest php, we will need to set xdebug, as well.

This is the reason we need to add:

1- name: Setup PHP with Xdebug
2 uses: shivammathur/setup-php@v2
3 with:
4 php-version: '7.4'
5 coverage: xdebug

Error with PHPUnit

When using the default action from Github action, you will receive the following error:

1option --coverage is ambiguous

The issue here is the --no-script when running composer install. You must remove this argument when running it.


Setting minimun coverage

As you can find in Pest documentation, you must add the following arguments when you run the tests:

1- name: Execute tests
2 run: vendor/bin/pest --coverage --min=90